Repository navigation
Expand file tree
/
Copy pathcore.py
More file actions
170 lines (126 loc) · 3.9 KB
/
Copy pathcore.py
File metadata and controls
170 lines (126 loc) · 3.9 KB
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
161
162
163
164
165
166
167
168
169
170
from __future__ import print_function, absolute_import, division
import functools
import weakref
import numpy
class State(object):
"""A state of a value.
It can be deferred, that it contains a transformation from other states.
It can be manifested, that it contains the value.
A deferred state can also be owned/free.
The transformation in a deferred-free-state that is not owned can be
composed with other deferred-free-states. So that temporary
sub-expressions can be eliminated, while the content
of deferred-owned-states are manifested into usable concrete form.
A manifested state must be owned.
"""
@staticmethod
def deferred(trans):
return State(trans=trans)
@staticmethod
def manifested(value):
return State(value=value)
def __init__(self, trans=None, value=None):
assert trans is None or value is None
assert not (trans is None and value is None)
self.trans = trans
self.value = value
self.owners = set()
def force(self):
"""Force evaluation of the value if it is deferred.
No-op if it is manifested.
Returns
-------
manifested value
"""
if self.is_deferred:
self.value = self.trans.apply()
self.trans = None
assert self.is_manifested
return self.value
@property
def is_deferred(self):
return self.value is None
@property
def is_manifested(self):
return not self.is_deferred
@property
def is_owned(self):
return bool(self.owners)
def __repr__(self):
if self.is_manifested:
return "<manifested %s>" % (type(self.value),)
else:
owned = "owned" if self.is_owned else "free"
return "<deferred %s %r>" % (owned, self.trans)
class Transformation(object):
def __init__(self, *args):
self.args = args
def __repr__(self):
return "<%s nargs=%d>" % (type(self).__name__, len(self.args))
def apply(self):
"""Subtype should implement this method.
"""
return NotImplementedError
class AddOp(Transformation):
def apply(self):
left, right = self.args
return left.force() + right.force()
class SubOp(Transformation):
def apply(self):
left, right = self.args
return left.force() - right.force()
def binop(trans):
"""Decorator for binary operators to bind to a transformation class.
"""
def wrap(fn):
@functools.wraps(fn)
def impl(self, other):
state = State.deferred(trans(self.state, other.state))
return Array(state=state)
return impl
return wrap
class Array(object):
"""A deferred array object.
"""
def __init__(self, state):
self.state = state
self._ref = weakref.ref(self)
self.state.owners.add(self._ref)
def __del__(self):
self.state.owners.discard(self._ref)
@binop(AddOp)
def __add__(self, other):
pass
@binop(SubOp)
def __sub__(self, other):
pass
def force(self):
self.state.force()
def __repr__(self):
return "<Array %s>" % self.state
def __str__(self):
self.force()
return str(self.state.value)
def describe_state(state, _p="", _suppress=False):
if not _suppress:
print("Describe state dependencies")
print(_p, '-', state)
if state.is_deferred:
for arg in state.trans.args:
describe_state(arg, _p=_p + " |", _suppress=True)
def from_numpy(arr):
return Array(state=State.manifested(arr))
# -------------
# Test
def test():
print("# Build Array")
arr = from_numpy(numpy.arange(10))
describe_state(arr.state)
print("# Build Expr")
sum = arr + arr + arr
describe_state(sum.state)
print("# Force")
print(sum)
describe_state(sum.state)
if __name__ == '__main__':
test()