-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathirpy.pyx
executable file
·160 lines (114 loc) · 4.39 KB
/
irpy.pyx
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#Handle the execution stack
from collections import defaultdict
d_path = defaultdict(list)
d_last_caller = defaultdict(lambda: None)
def genealogy(obj, _node, direction,degree_max=100):
"""Return the genealogy of a _node.
Direction is $parents or $children, recurse accordingly"""
def sap(_node, direction, visited=set(), degree=0):
visited.add(_node)
try:
s = getattr(obj, "{0}_{1}".format(_node, direction))
except AttributeError:
s = set()
for next_ in s - visited:
if degree < degree_max:
sap(next_, direction, visited, degree+1)
return visited
s = sap(_node, direction) - set([_node])
return s
def addattr(obj, name, value):
try:
s = getattr(obj, name)
except AttributeError:
setattr(obj, name, set([value]))
else:
setattr(obj, name, s | set([value]))
def removeattr(obj, name, value):
try:
s = getattr(obj, name)
except AttributeError:
pass
else:
setattr(obj, name, s - set([value]))
# _
# | \ _ _ _ ._ _. _|_ _ ._
# |_/ (/_ (_ (_) | (_| |_ (_) |
#
class lazy_property(object):
"""
My little Property
My little Property
My little Property... friend
"""
def __init__(self, provider, init_node=False, immutable=True):
"""Provider: If a function who will be used to compute the node
init_node: If the name of the node
immutable: If immutable is set you cannot set the node"""
self.provider = provider
self.init_node = init_node
self.immutable = immutable
if not self.init_node:
name = provider.__name__
else:
name = self.init_node
#Kind of human readable identifier
self._node = "_%s_%s" % (name, id(provider))
def __get__(self, obj, objtype):
"Get the value of the node and handle the genealogy"
_node = self._node
if d_path[obj]:
_caller = d_path[obj][-1]
if _caller != d_last_caller[obj]:
addattr(obj, "%s_parents" % _node, _caller)
addattr(obj, "%s_children" % _caller, _node)
d_last_caller[obj] = _caller
#Wanted: value. Cached or Computed
try:
value = getattr(obj, _node)
except AttributeError:
d_path[obj].append(_node)
value = self.provider(obj)
setattr(obj, _node, value)
d_path[obj].pop()
return value
def __set__(self, obj, value):
"""Set the value of the node
But wait, init_node are "gradual typed" variable! Youpi!
Idea borrowed from the-worst-programming-language-ever (http://bit.ly/13tc6XW)
"""
_node = self._node
if not self.init_node:
if self.immutable:
raise AttributeError("Immutable Node {0}".format(self._node))
#Set the new value
setattr(obj, _node, value)
#Node ancestor need to be recompute is asked
for _parent in genealogy(obj, _node, "parents"):
if hasattr(obj, _parent): delattr(obj, _parent)
#Node abandoned her children,
for _child in genealogy(obj, _node, "children",degree_max=1):
removeattr(obj, "%s_parents" % _child, _node)
#Indeed node is now a leaf
setattr(obj, "%s_children" % _node, set())
else:
setattr(obj, _node, value)
self.init_node = False
def lazy_property_mutable(provider):
"Return a lazy_property mutable"
return lazy_property(provider=provider, immutable=False)
def lazy_property_leaves(mutables=(), immutables=()):
"Set to properties for the __init__ method"
def leaf_decorator(func):
def func_wrapper(self, *args, **kwargs):
for node in set(immutables) | set(mutables):
def provider(self):
return getattr(self, "_%s" % node)
p = lazy_property(provider=provider,
init_node=node,
immutable=node in immutables)
#If this ugly? Yeah... Is this an issue? I don't really know
setattr(self.__class__, node, p)
return func(self, *args, **kwargs)
return func_wrapper
return leaf_decorator