Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

python 3 inheritance Define a class named bidict (bidirectional dict) derived from the dict class; in addition to being a regular dictionary (using inheritance), it

python 3 inheritance

image text in transcribedimage text in transcribed

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 bidicts values as keys, associated to a set of the bidicts 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 a list of all the objects created from this class, which two static functions manipulate.

Define the class bidict 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 thebidict): 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 (a1) has a __hash__ attribute, and (a2) the attributes value is not None; also (b) if the object is iterable (has an __iter__ 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 infinite recursion because every value that we iterate over in a str is a str that we can iterate over! Raise aValueError 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_is_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: {'a', 'c'}, 2: {'b'}}. If I tried to construct bidict(a=[]) then __init__ would raise a ValueError exception. We will continue using this example below.

5. (6 pts) Define a class named bidict (bidirectional dict) derived from the dict class; in addition to being a regular dictionary (using inheritance), t 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 a list of all the objects created from this class, which two static functions manipulate Define the class bidict with the following methods (some override dict methods); you may also add helper methods: preface them with single underscores) .-init-(self, nitial-u , **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 an iter 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 infinite recursion because every value that we iterate over in a str is a str that we can iterate over! Raise a ValueError 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 is_hashable) Finally, _rdict should never store a key that is associated with an empty set-setitem_ and _del item-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: ('a', 'c', 2: ('b'1 If I tried to construct bidict (a=[] ) then-init-would raise a ValueError exception. We will continue using this example below

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic 4 Ole Database And Controls Superbible

Authors: Michael Hatmaker, C. Woody Butler, Ibrahim Malluf, Bill Potter

1st Edition

1571690077, 978-1571690074

Students also viewed these Databases questions

Question

a neglect of quality in relationship to international competitors;

Answered: 1 week ago