Package parser_course :: Package small_parsers :: Module multivalued_dictionary
[hide private]
[frames] | no frames]

Source Code for Module parser_course.small_parsers.multivalued_dictionary

 1  try: 
 2      import collections 
 3      defaultdict = collections.defaultdict 
 4  except AttributeError: 
 5      print '*Warning*: Using local class definition of defaultdict' 
 6      print '           collections.defaultdict not available until Python 2.5' 
7 - class defaultdict(dict):
8 """Subclass of dict which sets key to a default value 9 when key is unknown. defaultvalue supplied by callable 10 default_factory supplied when dict instance is created. 11 default_factory must be callable with no args."""
12 - def __init__(self,default_factory, *a, **k):
13 dict.__init__(self, *a, **k) 14 self.default_factory = default_factory
15
16 - def __getitem__ (self, key):
17 if key not in self and self.default_factory is not None: 18 self[key] = self.default_factory() 19 return dict.__getitem__(self,key)
20
21 -class multivaluedDictionary(defaultdict):
22
23 - def __init__(self, *a, **k):
24 defaultdict.__init__(self,list,*a,**k)
25