Inventory (3 points)

The pasta company stores their produced pasta types in a warehouse and manages the warehouse inventory with a registration file. Every movement in the warehouse is registered in one line, which contains the following information: a tag indicating if the package was produced (IN) or if it was sold away (OUT), the name of the pasta type and the weight of the package in kilograms, separated by a space.

For example, you can download file registration.txt with the following content, that is used in the first test:

IN RAVIOLI 20
OUT SPAGHETTI 10
IN MACARONI 10
OUT SPAGHETTI 20
OUT RAVIOLI 40
IN SPAGHETTI 50
OUT MACARONI 15

Write a modifier function inventory() that takes three parameters. The first one is a dictionary containing as key the name of each produced pasta type (str) and as value the corresponding stock in kg (int) at the beginning of the day. The next two parameters are names of files (str), one for the given registration file and one for the sold out output file that must be created.

Automatize the inventory at the end of the day based on the movements provided in the registration file, by modifying the provided dictionary in the following way. For IN registration lines: add the amount of kg to the corresponding pasta type stock; for OUT registration lines: subtract the amount of kg to the corresponding stock. There can be negative numbers.

Moreover, all pasta type names with a stock less than or equal to 0 kg at the end of the day must be written in the sold out output file (in any order and one per line). This file must be created always, even if it empty.

Example based on the registration file given above:

>>> stock = {'SPAGHETTI': 30, 'RAVIOLI': 20, 'MACARONI': 3}
>>> r = inventory(stock, 'registration.txt', 'soldout.txt')
>>> print(r)
None
>>> if (stock != {'SPAGHETTI': 50, 'RAVIOLI': 0, 'MACARONI': -2}):
...     print(stock)
>>> with open('soldout.txt', 'r') as out:
...     data = out.readlines()
>>> sorted(data)
['MACARONI\n', 'RAVIOLI\n']

We can see how the stock dictionary has been modified. Moreover, a file named soldout.txt has been created, with the following content:

RAVIOLI
MACARONI

Save this function to file p2.py.

Note

More tests are provided in file test-inventory.txt.