-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbolic.py
515 lines (429 loc) · 16.6 KB
/
symbolic.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
from decorator import decorator
import ast
import types
from inspect import getcallargs
from dispatch import DispatchDict
from itertools import chain
def sym_getattr(sym, attr_name, *default):
try:
return object.__getattribute__(sym, attr_name)
except AttributeError:
if len(default) > 0:
return default[0]
else:
raise
def compile_sym(symbol):
prior = object.__getattribute__(symbol, "prior")
module = [object.__getattribute__(s, "ast") for s in prior]
parent = object.__getattribute__(symbol, "parent")
results = object.__getattribute__(symbol, "ast")
if module:
mode = 'exec'
if parent:
module.append(ast.Expr(results))
results = ast.Module(module)
else:
mode = 'eval'
results = ast.Expression(results)
try:
return compile(ast.fix_missing_locations(results), '<string>', mode)
except TypeError as err:
print ast.dump(object.__getattribute__(symbol, 'ast'))
raise
def ast_self_node(symbol):
return object.__getattribute__(symbol, "ast")
def binary_op(op):
return lambda self, other: ast.BinOp(
object.__getattribute__(self, "ast"),
op(),
ast_obj_nodes(other),
)
def compare(op):
return lambda self, other: ast.Compare(
object.__getattribute__(self, "ast"),
[op()],
[ast_obj_nodes(other)],
)
def aug_assign(op):
return lambda self, other: ast.AugAssign(
object.__getattribute__(self, "ast"),
op(),
ast_obj_nodes(other),
)
def subscript(self, item):
if isinstance(item, types.SliceType):
index = ast.Slice(
item.start and ast_obj_nodes(item.start),
item.stop and ast_obj_nodes(item.stop),
item.step and ast_obj_nodes(item.step)
)
else:
index = ast.Index(ast_obj_nodes(item))
return ast.Subscript(
value=object.__getattribute__(self, "ast"),
slice=index,
ctx=ast.Load()
)
def unary_op(op):
return lambda self: ast.UnaryOp(op(), ast_self_node(self))
def ast_name(name, ctx=ast.Load):
return ast.Name(id=name, ctx=ctx())
def ast_call(*args, **kwargs):
bound_args = getcallargs(object.__getattribute__(type(kwargs["self"]), "__call__"), *args, **kwargs)
return ast.Call(
ast_self_node(bound_args["self"]),
[ast_obj_nodes(v) for v in bound_args["kwargs"]["args"]],
[ast.keyword(k, ast_obj_nodes(v)) for (k, v) in bound_args["kwargs"]["kwargs"].items()],
None,
None
)
def ast_attr(ctx):
def _ast_attr(self, item):
return ast.Attribute(
ast_self_node(self),
item,
ctx()
)
return _ast_attr
def ast_assign(self, item, value):
return ast.Assign(
[
ast.Attribute(
ast_self_node(self),
item,
ast.Store()
)
],
ast_obj_nodes(value)
)
ast_op_nodes = {
"__add__": binary_op(ast.Add),
"__iadd__": aug_assign(ast.Add),
"__getattribute__": ast_attr(ast.Load),
"__and__": binary_op(ast.BitAnd),
"__iand__": aug_assign(ast.BitAnd),
"__or__": binary_op(ast.BitOr),
"__ior__": aug_assign(ast.BitOr),
"__xor__": binary_op(ast.BitXor),
"__ixor__": aug_assign(ast.BitXor),
"__call__": ast_call,
"__div__": binary_op(ast.Div),
"__idiv__": aug_assign(ast.Div),
"__eq__": compare(ast.Eq),
"__floordiv__": binary_op(ast.FloorDiv),
"__ifloordiv__": aug_assign(ast.FloorDiv),
"__gt__": compare(ast.Gt),
"__ge__": compare(ast.GtE),
"__inv__": unary_op(ast.Invert),
"__lshift__": binary_op(ast.LShift),
"__ilshift__": aug_assign(ast.LShift),
"__lt__": compare(ast.Lt),
"__le__": compare(ast.LtE),
"__mod__": binary_op(ast.Mod),
"__imod__": aug_assign(ast.Mod),
"__mul__": binary_op(ast.Mult),
"__imul__": aug_assign(ast.Mult),
"__ne__": compare(ast.NotEq),
"__pow__": binary_op(ast.Pow),
"__ipow__": aug_assign(ast.Pow),
"__rshift__": binary_op(ast.RShift),
"__irshift__": aug_assign(ast.RShift),
"__sub__": binary_op(ast.Sub),
"__isub__": aug_assign(ast.Sub),
"__getitem__": subscript,
"__setitem__": subscript,
"__pos__": unary_op(ast.UAdd),
"__neg__": unary_op(ast.USub),
"__invert__": unary_op(ast.Invert),
"__setattr__": ast_assign
}
ast_stmt_nodes = {
"__iadd__",
"__isub__",
"__imul__",
"__idiv__",
"__ifloordiv__",
"__idivmod__",
"__ipow__",
"__imod__",
"__iand__",
"__ior__",
"__ixor__",
"__ilshift__",
"__irshift__",
"__setattr__"
}
def copy_chainable(obj, **kwargs):
args = kwargs.pop("args", object.__getattribute__(obj, "args"))
state = kwargs.pop("state", object.__getattribute__(obj, "state"))
operation = kwargs.pop("operation", object.__getattribute__(obj, "operation"))
parent = kwargs.pop("parent", object.__getattribute__(obj, "parent"))
name = kwargs.pop("name", object.__getattribute__(obj, "name"))
prior = kwargs.pop("prior", object.__getattribute__(obj, "prior"))
return type(obj)(state, parent, prior, name, operation, args)
@decorator
def chainable(f, *args, **kwargs):
bound_args = getcallargs(f, *args, **kwargs)
self = bound_args["self"]
state = f(**bound_args)
prior = object.__getattribute__(self, "prior")
all_sym = filter(lambda x: x != "self" and isinstance(bound_args[x], Symbol), bound_args)
chainable_args = chain.from_iterable(sym_getattr(bound_args[i], "prior") for i in all_sym)
if chainable_args:
prior += tuple(chainable_args) # Will be empty if no other
name = object.__getattribute__(self, "name")
type_self = type(self)
results = type_self(state, self, prior)
object.__setattr__(results, "operation", f.__name__)
object.__setattr__(results, "args", bound_args)
# If the operation was a statement we need to add the result to previous
# and start a new chain.
if f.__name__ in ast_stmt_nodes:
prior += (results,)
results = type_self(prior=prior, name=name)
return results
@decorator
def pseudochainable(f, *args, **kwargs):
"""
This takes a non-chainable statement on a Symbol and emulates chainable
behavior by inserting nodes between this node and its parent, then
updating the state of the current node to appear as if it were the result
of a chained call.
A visual representation may be somewhat clearer. Letters represent
node identity and numbers represent temporal location:
Before: (A 0) -> (B 1)
After: (A 0) -> (C 1) -> (D 2) -> (B 3)
Node C represents a copy of node B in the before state. Node D represents
the statement operation, which is not chainable. Node B now represents the
start of a new expression, with a prior expression tuple of (*D's priors, D).
"""
# First lets collect all the required values
bound_args = getcallargs(f, *args, **kwargs)
self = bound_args["self"]
# We need to make a copy of this node and insert it between this node
# and its parent.
old_self = copy_chainable(self)
# We then need to create the statement terminating node
terminator = copy_chainable(self, state=f(**bound_args), parent=old_self, operation=f.__name__, args=bound_args)
# Finally we need to update the current chainable to indicate that it
# is now an empty node (save name and priors).
prior = sym_getattr(terminator, "prior") + (terminator,)
object.__setattr__(self, "operation", None)
object.__setattr__(self, "args", None)
object.__setattr__(self, "state", None)
object.__setattr__(self, "parent", None)
object.__setattr__(self, "prior", prior)
# Name should already be set properly, so that is it!
# Note that this is a pointless return as this decorator is
# only used for statement terminating methods...
return self
class Symbol(object):
"""
Base class for Proxy objects.
"""
# def __str__(self):
# pass
#
def __repr__(self):
return ast.dump(object.__getattribute__(self, "ast"))
@property
def ast(self):
parent = object.__getattribute__(self, "parent")
if parent is None:
return ast.Name(object.__getattribute__(self, "name"), ast.Load())
else:
node_func = ast_op_nodes.get(
object.__getattribute__(self, "operation"),
None
)
if node_func:
args = object.__getattribute__(self, "args")
return node_func(**args)
else:
return ast.Name(object.__getattribute__(self, "name"), ast.Load())
def __init__(self, state=None, parent=None, prior=None, name=None, operation=None, args=None):
"""By default, children should get attributes from their parents."""
object.__setattr__(self, "state", state)
object.__setattr__(self, "parent", parent)
object.__setattr__(self, "prior", prior or sym_getattr(parent, "prior", tuple()))
object.__setattr__(self, "name", name or sym_getattr(parent, "name", "symbol"))
object.__setattr__(self, "operation", operation or sym_getattr(parent, "operation", None))
object.__setattr__(self, "args", args or sym_getattr(parent, "args", None))
@chainable
def __getattribute__(self, item):
return lambda: object.__getattribute__(self, item)
@pseudochainable
def __setattr__(self, item, value):
return lambda: object.__setattr__(self, item, value)
@chainable
def __invert__(self):
return lambda:-object.__getattribute__(self, "state")
@chainable
def __neg__(self):
return lambda:-object.__getattribute__(self, "state")
@chainable
def __pos__(self):
return lambda:+object.__getattribute__(self, "state")
@chainable
def __abs__(self):
return lambda: abs(object.__getattribute__(self, "state"))
@chainable
def __call__(self, *args, **kwargs):
return lambda: object.__getattribute__(self, "state")(*args, **kwargs)
@chainable
def __getitem__(self, item):
return lambda: object.__getattribute__(self, "state")[item]
@chainable
def __add__(self, other):
return lambda: object.__getattribute__(self, "state") + other
@chainable
def __sub__(self, other):
return lambda: object.__getattribute__(self, "state") - other
@chainable
def __mul__(self, other):
return lambda: object.__getattribute__(self, "state") * other
@chainable
def __div__(self, other):
return lambda: object.__getattribute__(self, "state") / other
@chainable
def __truediv__(self, other):
return lambda: object.__getattribute__(self, "state").__truediv__(other)
@chainable
def __floordiv__(self, other):
return lambda: object.__getattribute__(self, "state") // other
@chainable
def __mod__(self, other):
return lambda: object.__getattribute__(self, "state") % other
def __divmod__(self, other):
pass
@chainable
def __pow__(self, other):
return lambda: pow(object.__getattribute__(self, "state"))
@chainable
def __lshift__(self, other):
return lambda: object.__getattribute__(self, "state") << other
@chainable
def __rshift__(self, other):
return lambda: object.__getattribute__(self, "state") >> other
@chainable
def __radd__(self, other):
return lambda: other + object.__getattribute__(self, "state")
@chainable
def __rand__(self, other):
return lambda: other & object.__getattribute__(self, "state")
@chainable
def __rdiv__(self, other):
return lambda: other / object.__getattribute__(self, "state")
@chainable
def __rdivmod__(self, other):
return lambda: divmod(other, object.__getattribute__(self, "state"))
@chainable
def __rfloordiv__(self, other):
return lambda: other // object.__getattribute__(self, "state")
@chainable
def __rlshift__(self, other):
return lambda: other << object.__getattribute__(self, "state")
@chainable
def __rmod__(self, other):
return lambda: other % object.__getattribute__(self, "state")
@chainable
def __rmul__(self, other):
return lambda: other * object.__getattribute__(self, "state")
@chainable
def __ror__(self, other):
return lambda: other | object.__getattribute__(self, "state")
@chainable
def __rpow__(self, other):
return lambda: pow(other, object.__getattribute__(self, "state"))
@chainable
def __rrshift__(self, other):
return lambda: other >> object.__getattribute__(self, "state")
@chainable
def __rsub__(self, other):
return lambda: other - object.__getattribute__(self, "state")
@chainable
def __rtruediv__(self, other):
return lambda: object.__getattribute__(self, "state").__rtruediv__(other)
@chainable
def __rxor__(self, other):
return lambda: other ^ object.__getattribute__(self, "state")
@chainable
def __contains__(self, item):
return lambda: item in object.__getattribute__(self, "state")
@chainable
def __eq__(self, other):
return lambda: object.__getattribute__(self, "state") == other
@chainable
def __ne__(self, other):
return lambda: object.__getattribute__(self, "state") != other
@chainable
def __le__(self, other):
return lambda: object.__getattribute__(self, "state") <= other
@chainable
def __lt__(self, other):
return lambda: object.__getattribute__(self, "state") < other
@chainable
def __gt__(self, other):
return lambda: object.__getattribute__(self, "state") > other
@chainable
def __ge__(self, other):
return lambda: object.__getattribute__(self, "state") >= other
@chainable
def __cmp__(self, other):
return lambda: cmp(object.__getattribute__(self, "state"), other)
@chainable
def __and__(self, other):
return lambda: object.__getattribute__(self, "state") & other
@chainable
def __xor__(self, other):
return lambda: object.__getattribute__(self, "state") ^ other
@chainable
def __or__(self, other):
return lambda: object.__getattribute__(self, "state") | other
@chainable
def __iand__(self, other):
return lambda: object.__getattribute__(self, "state").__iand__(other)
@chainable
def __ixor__(self, other):
return lambda: object.__getattribute__(self, "state").__ixor__(other)
@chainable
def __ior__(self, other):
return lambda: object.__getattribute__(self, "state").__ior__(other)
@chainable
def __iadd__(self, other):
return lambda: object.__getattribute__(self, "state").__iadd__(other)
@chainable
def __isub__(self, other):
return lambda: object.__getattribute__(self, "state").__isub__(other)
@chainable
def __imul__(self, other):
return lambda: object.__getattribute__(self, "state").__imul__(other)
@chainable
def __idiv__(self, other):
return lambda: object.__getattribute__(self, "state").__idiv__(other)
@chainable
def __itruediv__(self, other):
return lambda: object.__getattribute__(self, "state").__itruediv__(other)
@chainable
def __ifloordiv__(self, other):
return lambda: object.__getattribute__(self, "state").__ifloordiv__(other)
@chainable
def __imod__(self, other):
return lambda: object.__getattribute__(self, "state").__imod__(other)
@chainable
def __ipow__(self, other):
return lambda: object.__getattribute__(self, "state").__ipow__(other)
@chainable
def __ilshift__(self, other):
return lambda: object.__getattribute__(self, "state").__ilshift__(other)
@chainable
def __irshift__(self, other):
return lambda: object.__getattribute__(self, "state").__irshift__(other)
ast_obj_nodes = DispatchDict()
ast_obj_nodes.update({
int: lambda x: ast.Num(x),
float: lambda x: ast.Num(x),
str: lambda x: ast.Str(x),
Symbol: lambda x: object.__getattribute__(x, "ast"),
tuple: lambda x: ast.Tuple([ast_obj_nodes(y) for y in x], ast.Load())
})