<s> __author__ = """<STR_LIT>""" <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from os import getcwd <EOL> from os import sys <EOL> sys . path . append ( getcwd ( ) ) <EOL> from MOAL . helpers . display import Section <EOL> from MOAL . helpers . display import print_h2 <EOL> from MOAL . helpers . display import print_h3 <EOL> from MOAL . helpers . display import print_info <EOL> from MOAL . helpers . display import divider <EOL> from MOAL . helpers . text import randchars <EOL> from MOAL . data_structures . abstract . array_and_linked_lists import AssociationList <EOL> from random import randrange as rr <EOL> from random import choice <EOL> from faker import Factory <EOL> DEBUG = True if __name__ == '<STR_LIT:__main__>' else False <EOL> class MapADT ( AssociationList ) : <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> print ( '<STR_LIT:{>' ) <EOL> for node in self : <EOL> print ( '<STR_LIT>' . format ( node . title , node . cargo ) ) <EOL> print ( '<STR_LIT:}>' ) <EOL> return '<STR_LIT>' <EOL> class MultiMap ( MapADT ) : <EOL> """<STR_LIT>""" <EOL> def __setitem__ ( self , key , value ) : <EOL> super ( MultiMap , self ) . append ( key , value ) <EOL> class Enrollment ( MultiMap ) : <EOL> def __init__ ( self , students = [ ] , classes = [ ] , title = '<STR_LIT>' ) : <EOL> self . MAX_CLASSES = <NUM_LIT:4> <EOL> self . classes = classes <EOL> self . students = students <EOL> super ( Enrollment , self ) . __init__ ( title = title ) <EOL> def enroll ( self , student , klass ) : <EOL> super ( Enrollment , self ) . __setitem__ ( student , klass ) <EOL> def enroll_all ( self , schedules ) : <EOL> for student , classes in schedules . iteritems ( ) : <EOL> for klass in classes : <EOL> self . enroll ( student , klass ) <EOL> if DEBUG : <EOL> divider ( atom = '<STR_LIT:.>' ) <EOL> def _classes ( self ) : <EOL> return [ choice ( self . classes ) for _ in range ( self . MAX_CLASSES ) ] <EOL> def schedules ( self ) : <EOL> self . schedules = [ { <EOL> student : self . _classes ( ) } for student in self . students ] <EOL> return self . schedules <EOL> def get_classes ( self , student ) : <EOL> res = super ( Enrollment , self ) . __getitem__ ( student ) <EOL> classes = res . cargo <EOL> return classes <EOL> def __setitem__ ( self , student , klass ) : <EOL> klass = '<STR_LIT>' . format ( choice ( [ '<STR_LIT>' , '<STR_LIT>' ] ) , klass , str ( rr ( <NUM_LIT:100> , <NUM_LIT> ) ) ) <EOL> if DEBUG : <EOL> print ( '<STR_LIT>' . format ( student , klass ) ) <EOL> super ( Enrollment , self ) . __setitem__ ( student , klass ) <EOL> class BidirectionalMap ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> print ( '<STR_LIT:{>' ) <EOL> for key , val in self . items . iteritems ( ) : <EOL> print ( '<STR_LIT>' . format ( key , val ) ) <EOL> print ( '<STR_LIT:}>' ) <EOL> return '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . items = { } <EOL> self . items . update ( * args , ** kwargs ) <EOL> def __contains__ ( self , key ) : <EOL> for _key , val in self . items . iteritems ( ) : <EOL> if _key == key : <EOL> return True <EOL> elif val == key : <EOL> return True <EOL> return False <EOL> def __getitem__ ( self , key , value ) : <EOL> try : <EOL> return self [ key ] <EOL> except KeyError : <EOL> return self [ value ] <EOL> finally : <EOL> pass <EOL> def __setitem__ ( self , key , val ) : <EOL> """<STR_LIT>""" <EOL> keys = self . items . keys ( ) <EOL> values = self . items . values ( ) <EOL> is_both_key = key in keys and key in values <EOL> is_both_val = val in keys and val in values <EOL> if is_both_key or is_both_val : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> super ( BidirectionalMap , self ) . __setitem__ ( key , val ) <EOL> if DEBUG : <EOL> with Section ( '<STR_LIT>' ) : <EOL> map_adt = MapADT ( title = '<STR_LIT>' ) <EOL> for n in range ( <NUM_LIT:6> ) : <EOL> key = '<STR_LIT>' . format ( '<STR_LIT>' . join ( randchars ( <NUM_LIT:8> ) ) ) <EOL> map_adt [ key ] = '<STR_LIT>' . join ( randchars ( <NUM_LIT:4> ) ) <EOL> print ( map_adt ) <EOL> with Section ( '<STR_LIT>' ) : <EOL> faker = Factory . create ( ) <EOL> multimap = MultiMap ( title = '<STR_LIT>' ) <EOL> print_h2 ( '<STR_LIT>' , desc = '<STR_LIT>' ) <EOL> for n in range ( <NUM_LIT:4> ) : <EOL> key = '<STR_LIT>' . format ( n ) <EOL> multimap [ key ] = '<STR_LIT>' . join ( randchars ( <NUM_LIT:4> ) ) <EOL> print ( multimap ) <EOL> print ( multimap [ '<STR_LIT>' ] ) <EOL> print_h2 ( '<STR_LIT>' , desc = '<STR_LIT>' ) <EOL> classes_map = Enrollment ( <EOL> classes = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> students = [ faker . name ( ) for _ in range ( <NUM_LIT:3> ) ] + [ '<STR_LIT>' ] ) <EOL> map ( classes_map . enroll_all , classes_map . schedules ( ) ) <EOL> print_h3 ( '<STR_LIT>' ) <EOL> print ( classes_map ) <EOL> print_h3 ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' . format ( classes_map . get_classes ( '<STR_LIT>' ) ) ) <EOL> del classes_map [ '<STR_LIT>' ] <EOL> assert classes_map [ '<STR_LIT>' ] is None <EOL> print ( classes_map ) <EOL> assert '<STR_LIT>' not in classes_map <EOL> print_h3 ( '<STR_LIT>' , desc = '<STR_LIT>' ) <EOL> bdmap = BidirectionalMap ( <EOL> { <NUM_LIT:1> : '<STR_LIT>' , <NUM_LIT:2> : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> print ( bdmap ) <EOL> try : <EOL> bdmap [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> bdmap [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> except ValueError : <EOL> print_info ( '<STR_LIT>' ) <EOL> print ( bdmap ) <EOL> assert '<STR_LIT>' in bdmap <EOL> assert '<STR_LIT>' in bdmap </s>
