Objects File

We consider a textfile with information about some objects marked with different prices.

The format of these files is as follows:

  • The fist line contains the header.

  • Each of the following lines contains the info about one object and a price found. The same object might appear several times with different colors and prices. Each line has the following infos separated by coma (','):

    • The name (str)

    • The identification number (int)

    • The color (str)

    • The price (float)

The file objects14.csv is an example:

NAME,ID,COLOR,UNITS,PRICE
box,11,red,2.0
tree,23,yellow,1.60
table,31,blue,1.45
lamp,44,red,2.25
chair,51,blue,0.5
bed,69,blue,3.5
tree,23,yellow,1.85
spoon,77,red,3.55
tree,23,yellow,1.8
box,11,red,2.5
chair,51,blue,0.75
table,31,blue,1.05
chair,51,blue,0.25
bed,69,blue,4.0

You are required to delivered the following function in the module objects_file (file objects_file.py):

read_objects(filename)

such that

given filename, str, the name of a textfile of objects with the format described above.

returns a dict with the information in that file with the following format:

  • Keys: 2-component tuple with:

    • The name (str).

    • The id number (int) of the object.

  • Values: 3-component list with:

    • The color (str) which can be any color of the instances of this object.

    • The number of instances (int) of that object found in the file (with its same name and id number).

    • The average price (float) rounded to 2 decimals.

For example the call read_objects('objects14.csv') over the previous file should produce the following dict:


>>> solD = {
... ('box', 11): ['red', 2, 2.25],
... ('tree', 23): ['yellow', 3, 1.75], 
... ('table', 31): ['blue', 2, 1.25],
... ('lamp', 44): ['red', 1, 2.25],
... ('chair', 51): ['blue', 3, 0.5],
... ('bed', 69): ['blue', 2, 3.75],
... ('spoon', 77): ['red', 1, 3.55],
... }

Find the doctest file at read_objects.test.