Package Bio :: Module PropertyManager
[hide private]
[frames] | no frames]

Source Code for Module Bio.PropertyManager

 1  """Stores properties associated with the class of an object (DEPRECATED). 
 2   
 3  This module is deprecated, and is expected to be removed in the next release. 
 4  If you use this module, please contact the Biopython developers via the 
 5  mailing lists. 
 6  """ 
 7  #NOTE - Adding a deprecation warning would affect Bio.Alphabet.IUPAC 
 8   
 9   
10  # Would it be nice to have support for more than one resolver per 
11  # class?  In the meanwhile, they could collude using a dispatch 
12  # object. 
13   
14  # Do you need access to the actual resolver? 
15   
16  # Resolvers get the sequence because they may do a per-object lookup. 
17   
18  # Could cache search results for better performance. 
19   
20   
21  # Dictionary which creates dictionary elements, so lookups never fail. 
22  # The new elements are always dictionaries. 
23 -class CreateDict(dict):
24 - def __getitem__(self, key):
25 return self.setdefault(key,{})
26
27 -class PropertyManager:
28 - def __init__(self):
29 self.class_property = CreateDict() 30 self.class_property_resolver = CreateDict() 31 self.class_resolver = {}
32
33 - def resolve(self, obj, property):
34 try: 35 klass = obj.__class__ 36 except AttributeError: 37 raise KeyError("built-in instance") 38 39 return self.resolve_class(klass, property)
40
41 - def resolve_class(self, klass, property):
42 # Hopefully, we'll find the hit right away 43 try: 44 return self.class_property[klass][property] 45 except KeyError: 46 pass 47 48 # Is there a property resolver? 49 try: 50 return self.class_property_resolver[klass][property]( 51 self, klass, property) 52 except KeyError: 53 pass 54 55 # What about the class resolver? 56 try: 57 return self.class_resolver[klass](self, klass, property) 58 except KeyError: 59 pass 60 61 # That failed, so we walk up the class tree, depth-first and 62 # left-to-right (same as Python). For each class, check if 63 # the property exists, then check if the property resolver 64 # exists, and finally, check for the class resolver. 65 66 bases = list(klass.__bases__) 67 while bases: 68 base = bases.pop() 69 try: 70 return self.class_property[base][property] 71 except KeyError: 72 pass 73 try: 74 return self.class_property_resolver[base][property]( 75 self, klass, property) 76 except KeyError: 77 pass 78 try: 79 return self.class_resolver[base](self, klass, property) 80 except KeyError: 81 pass 82 83 # this is why the search is depth-first/right-left 84 bases[:0] = list(base.__bases__) 85 raise KeyError("cannot find property %s for class %s" \ 86 % (property, klass))
87 88 89 default_manager = PropertyManager() 90