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