Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How to do this problem in python! 3: Details of read db The read _db function ta

ID: 3881225 • Letter: H

Question

How to do this problem in python!

3: Details of read db The read _db function takes an open-file as an argument; as a result, it returns a dictionary (you may return a dict or defaultdict, which print differently but compare for equality correctly based on their contents): the dictionary's keys are strs (the movie name) whose associated values are 2-tuples (year made, and 3-tuple of reviewer, score, year reviewed) The input file will consist of some number of lines. Each line contains information about one movie (and that movie will not appear on any other lines): the name of the movie, followed by the year it is made, followed by a sequence of all the reviewers and their information. The movie name, year made, and its reviews are separated by semicolons (), with the reviewer names, scores, and years reviewed separated by colons (:) For one example, the moviel.txt file contains Matewan; 1987;Alan: 5:1987;Barb:1:1986;Cal:3:1986 Zelig;1983;Barb:3:2001;Diane:4:1982; Ethan:1:2016; Fran: 4:2012 Twilight;2008;Diane :4:2009;Greg:3:2010 Frozen;2013;Barb:2:2012; Fran: 2:201s Here is what the first line means Matewan was made in 1987 and has 3 reviewers: 1. Alan gave it a score of 5, reviewing it in 1987 2. Barb gave it a score of 1, reviewing it in 1986 (before 1987, a previewer) 3. Cal gave it a score of 3, reviewing it in 1986 (before 1987, a previewer) Each line should initialize/update information in the dictionary for the movie and all its reviewers. The four-line file above returns a dictionary whose contents are: Frozen' (2013, (('Barb', 2, 2012), 'Fran', 2, 2015)), Matewan' (1987, f('Alan', 5, 1987), Barb', 1, 1986), (Cal', 3, 1986))), Twilight': (2008, f('Diane', 4, 2009), ('Greg', 3, 2010)}), 'Zelig:(1983, f('Barb', 3, 2001), 'Diane', 4, 1982), ('Ethan', 1, 2016), ('Fran', 4, 2012)) Of course, dictionaries and sets can print in any order, because they are not ordered. Note that the years and scores read must be translated into int values If you use/return a defaultdict it will show other information when printed, but its contents (keys and values) will show equivalently (and will test for equality correctly against dicts)

Explanation / Answer

#this function will take a file object opened in 'r' or 'r+' mode as argument "openFile"

def read_db(openFile):

mainDict={} # this is the dictionary that will contain the whole Movies with there respective data

#the loop below will evaluate each line one by one in the file

for line in openFile:

reviewList=[] # this list will maintain the list of all reviewers

data=line.split(";") #here seperate the line read from the file on the basis of semicolon ";"

#this loop will leave the first two data members as they are the movie name and movie release year

#respectively and will iterate on the rest of the data members as they are the data of reviews and will

#populate reviewList

for reviewer in data[2:]:

reviewData=reviewer.split(":") #here we seperate each data member on the basis of colon ":"

#in the line below we add an tuple to review list whose first element contains the name of

#reviewer the second and third element contain review score and date of review respectively

#both translated to there integer values

reviewList.append(reviewData[0],int(reviewData[1]),int(reviewData[2]))

#in the line below we add an tuple to Main dictionary whose key element contains the name of

#Movie and the first element of tuple is release date of movie translated to its integer value and

# the second element is the review list obtained from above

mainDict[data[0]]=(int(data[1]),reviewList)

return mainDict

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote