Question: ANSWER IN PYTHON 3 LANGUAGE PLEASE! 5. (6 pts) Define a class named bidict (bidirectional dict) derived from the dict class; in addition to being

ANSWER IN PYTHON 3 LANGUAGE PLEASE!

ANSWER IN PYTHON 3 LANGUAGE PLEASE! 5. (6 pts) Define a class

5. (6 pts) Define a class named bidict (bidirectional dict) derived from the dict class; in addition to being a regular dictionary (using inheritance), it also defines an auxiliary/attribute dictionary that uses the bidict's values as keys, associated to a set of the bidict's keys (the keys associated with that value). Remember that multiple keys can associate to the same value, which is why we use a set: since keys are hashable (hashable -immutable) we can store them in sets. Finally, the bidict class stores a class attribute that keeps track of alist of all the objects created from this class, which two static functions manipulate. Define the class bdict with the following methods (some override dict methods); you may also add helper methods: preface them with single underscores): _init__(self,initial -,**kargs): initializes the dict in the base class and also creates an auxiliary dictionary (I used a defaultdict) named_rdict (reversedict: you must use this name for the bsc to work correctly) whose key(s) (the values in the bidict) are associated with a set of values (their keys in the bidict): initialize_rdict by iterating through the newly initialized dictionary For a bidict to work correctly, its keys and their associated values must all be hashable. We define any object as hashable if it (al) has a_hash_ attribute, and (a2) the attribute's value is not None; also (b) if the object is iterable (has aniter_ attribute) then every value iterated over is also hashable (by this same definition). Also note that str is hashable as a special case: if we apply the above definition it will create every value that we iterate over in a str is a str that we can iterate over! Raise a ValueBrror exception if any value is not hashable. Note you can use the hasattr and getattr functions (which I used in a recursive static helper method you should write with the name hashable). Finally, _rdict should never store a key that is associated with an empty set: setitem_ and delitem_must ensure this invariant property (I wrote a helper method to help them do it). For example if we define, bd - bidict(a-1,b-2,c-1) then_rdict stores (1: l'a', 'c'), 2: l'b'11 If I tried to construct bdict (a=[] ) then init-would raise a valueBrror exception. We will continue setitem (self,key, value): set key to associate with value in the dictionary and modify the_rdict to reflect the change. For example, executing bdl'a'] - 2 would result in the_rdict being changed to [1: l'c'), 2: l'a', 'b'1; then executing bd l'c'] - 2 would result in the _rdict being changed to [2: f'a', 'b', 'c'11, with 1 no longer being a key in_rdict because it is no longer a value in the bidict. delitem (self,key): for any key in the dictionary, remove it and modify the_rdict to reflect the change. ._call_ (self,value): returns the set of keys (keys in the bidict) that associate with value: just lookup clear (self): remove all keys (and their associated values) from the bidict modify the_rdict to reflect all_objects 0: a static method that returns alist of all the objects every created by bidict. . forget (object): a static method that forgets the specified bidict object, so al1_objects doesn't retum it

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!