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 .txt extension.

  • 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' where dd are two digits denoting the day of the month, mm are two digits with the month number, and aa are 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:

  • the Product Identification Code PIC (str)

  • the quantity available (float)

  • the last date of the quantity update (str)

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-24

This 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:

  • a str with the PIC

  • a float of the quantity requested by the 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.0

This 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:

  1. Read all function specifications.

  2. Start coding the main one (the first one) bearing the subfunctions in mind.

  3. Code and Test the subfunctions.

  4. Test the main function.

  5. Deliver all functions in the above indicated file.


The main function is:

stock_update(stkFn, ordFn, date):

such that

given

  • stkFn, ordFn, two str with the names of the stocks and orders files as described above

  • a str with 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 a str with 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 called stocks-01-12-24.txt and errors-01-12-24.txt 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
OrderError: 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 str with the name of a stocks file as described above

returns a dict where the key is a str with the Product Identification Code (PIC) and the associated value is a two-component tuple: a tuple with the product’s quantity available in stock and a str with 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 dict like the one produced by the previous function gen_stkD

  • ordFn the name of a stocks file as described above

  • date a str with 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:

  1. update the stkD dict as 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')}
    
    
  2. return the tuple (5, 5), and

  3. write 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

given

  • stkD a dict as the one produced by the previous function gen_stkD

  • date a str with a date (in the format specified above)

writes

writes a new stocks textfile as described in the stock_update function above.

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 dict at 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.