Bodega Stocks¶
Given information about the stocks currently available in a “bodega” we wish to process the daily orders received. Both data are saved in text files: the current stocks file and the daily orders file.
The following conventions apply:
All file names have the
.txtextension.All file contents have not header line and have lines with same data items separated by
','.All dates appearing below have an 8-character format
'dd-mm-yy'whereddare two digits denoting the day of the month,mmare two digits with the month number, andaaare two digits with the year.
The details of their contents are the following:
#. The stocks file has a number of lines corresponding to products in the bodega’s stock. Each line is composed of three items of a product:
Note
There is one line for each PIC at most.
For example:
CLI,12.0,01-11-24 VEN,42.5,01-11-24 VENB,65.0,01-11-24 APR,30.5,01-11-24 ADB,65.5,01-11-24This file can be downloaded from:
stocks.txt.#. The daily orders file contains a number of lines corresponding to orders placed a given date. Each line is composed of two items of an order:
Note
There can be more than one order with the same PIC.
For example:
VENB10.0 CLI,5.0 CLR,2.0 CLN,4.0 VEN,5.5 APR,50.0 ADB,45.5 CLI,5.0 ADB,15.0 ADB,10.0This file can be downloaded from:
orders-01-12-24.txt.
In this context, you are required to deliver the following functions in the module stocks (file stocks.py).
First there is the main function specification followed by ome subfunctions.
Following the steps below is strongly recommended:
Read all function specifications.
Start coding the main one (the first one) bearing the subfunctions in mind.
Code and Test the subfunctions.
Test the main function.
Deliver all functions in the above indicated file.
The main function is:
- stock_update(stkFn, ordFn, date):
such that
given
stkFn, ordFn, two
strwith the names of the stocks and orders files as described abovea
strwith a date (with the format specified above)writes two textfiles, both with the
'.txt'extension:
A file called word
'stocks'concatenated with a'-'to the date and the'.txt'extension. This file must have a line for each product in stkFn modified as follows:
the quantity as been updated by substracting the quantities of the correct orders in ordFn.
if the quantity has changed then the date has also been changed to the current date.
a file called word
'errors'concatenated with a'-'to the date and the'.txt'extension. This file must have a line for each error produced while processing the orders in ordFn. The line must have astrwith the error code followed by' : 'and the order informations separated by','. The error codes to consider are the following:
'OrderError'if the order has not exactly two items: the PIC and the quantity.
'ProdNotFoundError'if the order refers to a product not included in the stocks file.
'StockNotEnoughError'if the quantity of the order is bigger that the stock quantity of that product in the stocks file.returns two integers: the number of orders successfully processed and the number of errors produced.
For example, stock_update applied to the previous input files would return the values
(5, 5)and write two files calledstocks-01-12-24.txtanderrors-01-12-24.txtwith the following contents:CLI,2.0,01-12-24 VEN,37.0,01-12-24 VENB,65.0,01-11-24 APR,30.5,01-11-24 ADB,5.0,01-12-24OrderError: VENB10.0 ProdNotFoundError: CLR,2.0 ProdNotFoundError: CLN,4.0 StockNotEnoughError: APR,30.5,50.0 StockNotEnoughError: ADB,5.0,10.0
Note
Observe that if a product receives more than one order, the orders that do not exceed the product stock are properly served, the quantity available is updates and no error is raised. The error is produced when an order request an quantity higher that the quantity available only without affecting the previous orders for that product.
Doctests are available in the stock_update.test file.
To pass these doctests you need the stocks.txt file as well as the files in the following zip file: stocks_refs.zip.
The first subfunction is:
- gen_stkD(stkFn)¶
such that
given stkFn a
strwith the name of a stocks file as described abovereturns a
dictwhere the key is astrwith the Product Identification Code (PIC) and the associated value is a two-componenttuple: atuplewith the product’s quantity available in stock and astrwith the date of the last update date of that quantity.
For example, given the stocks file shown above
CLI,12.0,01-11-24 VEN,42.5,01-11-24 VENB,65.0,01-11-24 APR,30.5,01-11-24 ADB,65.5,01-11-24
we should have:
>>> D = gen_stkD('stocks.txt') >>> solD = { ... 'CLI': (12.0, '01-11-24'), ... 'VEN': (42.5, '01-11-24'), ... 'VENB': (65.0, '01-11-24'), ... 'APR': (30.5, '01-11-24'), ... 'ADB': (65.5, '01-11-24')}
Doctests are available in the gen_stkD.test file.
To pass these doctests you may need to download the stocks file: stocks.txt.
The second subfunction is:
- upd_stkD(stkD, ordFn, date)¶
such that
given
stkD, a
dictlike the one produced by the previous function gen_stkDordFn the name of a stocks file as described above
date a
strwith a date (in the format specified above)updates stkD according to the orders in ordFn
writes a new errors textfile as described in the stock_update function above
returns two integers: the number of orders successfully processed and the number of errors produced.
For example, given the dict from the previous example:
>>> stkD = { ... 'CLI': (12.0, '01-11-24'), ... 'VEN': (42.5, '01-11-24'), ... 'VENB': (65.0, '01-11-24'), ... 'APR': (30.5, '01-11-24'), ... 'ADB': (65.5, '01-11-24')}
and the orders-01-12-24.txt file above:
VENB10.0 CLI,5.0 CLR,2.0 CLN,4.0 VEN,5.5 APR,50.0 ADB,45.5 CLI,5.0 ADB,15.0 ADB,10.0
the following call:
>>> r = upd_stkD(stkD, 'orders-01-12-24.txt', '01-12-24')
should have the following three outcomes:
update the stkD
dictas follows (notice that both the quantities and the last date update are affected),>>> solD = { ... 'CLI': (2.0, '01-12-24'), ... 'VEN': (37.0, '01-12-24'), ... 'VENB': (65.0, '01-11-24'), ... 'APR': (30.5, '01-11-24'), ... 'ADB': (5.0, '01-12-24')}return the
tuple(5, 5), andwrite a new file named
'errors-01-12-24.txt'with the following contents:OrderError: VENB10.0 ProdNotFoundError: CLR,2.0 ProdNotFoundError: CLN,4.0 StockNotEnoughError: APR,30.5,50.0 StockNotEnoughError: ADB,5.0,10.0
Doctests are available in the upd_stkD.test file.
To pass these doctests you may need to download the following file: errors-01-12-24.ref.
The third subfunction is:
- write_stkD(stkD, date)¶
such that
For example, given the following call
>>> stkD = { ... 'CLI': (2.0, '01-12-24'), ... 'VEN': (37.0, '01-12-24'), ... 'VENB': (65.0, '01-11-24'), ... 'APR': (30.5, '01-11-24'), ... 'ADB': (5.0, '01-12-24')} >>> write_stkD(stkD, '01-12-24')should not change the stkD
dictat all, and a new file named'stocks-01-12-24.txt'should be written with the following contents:CLI,2.0,01-12-24 VEN,37.0,01-12-24 VENB,65.0,01-11-24 APR,30.5,01-11-24 ADB,5.0,01-12-24
Doctests are available in the write_stkD.test file.
To pass these doctests you may need to download the following file: stocks-01-12-24.ref.