Question: PYTHON Details of read_db: The read_db function takes an open-file as an argument; it returns a dictionary (dict or defaultdict) associating a date (str) with
PYTHON
Details of read_db: The read_db function takes an open-file as an argument; it returns a dictionary (dict or defaultdict) associating a date (str) with a another dictionary: this inner dictionary associates a drug name (str) with a tuple of int representing all its transactions (non-0 int values) on that date. The input file will consist of some number of lines; each line contains non-0 transactions for a unique date/drug; unique means the same date/drug will not appear on multiple lines. All these different items are separated by colons (:). For one example, the w21-1.txt file contains the following lines. 2016-04-01:Azor:100:-3:-3:1 2016-04-01:Benz:4:2:-3 2016-04-02:Azor:-3:-3:2 2016-04-02:Caml:6:-2:-3 2016-04-02:Depr:4:-7 Here is what each of the lines mean: 1. On 2016-04-01 the Azor drug had 4 transactions: receive 100, distribute 3, distribute 3, and receive 1. 2. On 2016-04-01 the Benz drug had 3 transactions: receive 4, receive 2, and distribute 3. 3. On 2016-04-02 the Azor drug had 3 transactions: distribute 3, distribute 3, and receive 2. 4. On 2016-04-02 the Caml drug had 3 transactions: receive 6, distribute 2, and distribute 3. 5. On 2016-04-02 the Depr drug had 2 transactions: receive 4 and distribute 7
def read_db(file : open) -> {str:{str:(int,)}}:
if prompt.for_bool('Test read_db?', True): print(' argument = w21-1.txt') answer = read_db(open('w21-1.txt')) print(' answer =', answer) check(answer, {'2016-04-01': {'Benz': (4, 2, -3), 'Azor': (100, -3, -3, 1)}, '2016-04-02': {'Depr': (4, -7), 'Azor': (-3, -3, 2), 'Caml': (6, -2, -3)}})
print(' argument = w21-2.txt') answer = read_db(open('w21-2.txt')) print(' answer =', answer) check(answer, {'2016-04-02': {'Efos': (-3, 20, -7), 'Azor': (-5, -2, 3, -1, -7), 'Caml': (4, 6, -2, 20, -8, -3)}, '2016-04-03': {'Efos': (4, 1, 30), 'Azor': (-4,), 'Caml': (-2, -2, -3, -2), 'Benz': (-4, 16)}, '2016-04-01': {'Efos': (4, -7), 'Depr': (30, -2, -5, -10, -2), 'Caml': (4, 6, -2, 1, -8, -3), 'Benz': (4, 2, -3, 20), 'Azor': (20, -3, -3, 1)}})
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
