Franck Pommereau

added verbose __repr__ on objects

1 +import inspect
2 +from functools import wraps
3 +from yapf.yapflib.yapf_api import FormatCode
4 +
1 class ExecEnv (dict) : 5 class ExecEnv (dict) :
2 _ValueClass = None 6 _ValueClass = None
3 def __init__ (self, *l, **k) : 7 def __init__ (self, *l, **k) :
...@@ -21,6 +25,14 @@ class ExecEnv (dict) : ...@@ -21,6 +25,14 @@ class ExecEnv (dict) :
21 def eval (self, code) : 25 def eval (self, code) :
22 return eval(code, self) 26 return eval(code, self)
23 27
28 +def _repr (obj) :
29 + if inspect.isclass(obj) :
30 + return obj.__name__
31 + elif inspect.isfunction(obj) :
32 + return obj.__name__
33 + else :
34 + return repr(obj)
35 +
24 class CAni (object) : 36 class CAni (object) :
25 _env = ExecEnv() 37 _env = ExecEnv()
26 @property 38 @property
...@@ -40,3 +52,29 @@ class CAni (object) : ...@@ -40,3 +52,29 @@ class CAni (object) :
40 def eval (self, code) : 52 def eval (self, code) :
41 RET = self.RET = self._env.eval(code) 53 RET = self.RET = self._env.eval(code)
42 return RET 54 return RET
55 + _init_args = (None, None)
56 + def __repr__ (self) :
57 + largs, kargs = self._init_args
58 + if largs and kargs :
59 + r = "{}({}, {})".format(self.__class__.__name__,
60 + ", ".join(_repr(l) for l in largs),
61 + ", ".join("{}={}".format(k, _repr(v))
62 + for k, v in kargs.items()))
63 + elif largs :
64 + r = "{}({})".format(self.__class__.__name__,
65 + ", ".join(_repr(l) for l in largs))
66 + elif kargs :
67 + r = "{}({})".format(self.__class__.__name__,
68 + ", ".join("{}={}".format(k, _repr(v))
69 + for k, v in kargs.items()))
70 + else :
71 + r = "{}()".format(self.__class__.__name__)
72 + return FormatCode(r)[0]
73 +
74 +def autorepr (method) :
75 + @wraps(method)
76 + def wrapper (self, *l, **k) :
77 + self.__dict__["_init_args"] = (l, k)
78 + return method(self, *l, **k)
79 + return wrapper
80 +
......
1 from itertools import chain 1 from itertools import chain
2 from inspect import cleandoc 2 from inspect import cleandoc
3 from collections import defaultdict 3 from collections import defaultdict
4 -from . import CAni, ExecEnv 4 +from . import CAni, ExecEnv, autorepr
5 5
6 class dopt (dict) : 6 class dopt (dict) :
7 def __str__ (self) : 7 def __str__ (self) :
...@@ -100,6 +100,7 @@ class CAniTikZ (CAni) : ...@@ -100,6 +100,7 @@ class CAniTikZ (CAni) :
100 code="\n ".join(self.tikz(**tikz).splitlines())) 100 code="\n ".join(self.tikz(**tikz).splitlines()))
101 101
102 class Pointer (CAniTikZ) : 102 class Pointer (CAniTikZ) :
103 + @autorepr
103 def __init__ (self, data) : 104 def __init__ (self, data) :
104 self.__dict__.update(_d=data, nodeid=None) 105 self.__dict__.update(_d=data, nodeid=None)
105 def val (self) : 106 def val (self) :
...@@ -120,6 +121,7 @@ class Pointer (CAniTikZ) : ...@@ -120,6 +121,7 @@ class Pointer (CAniTikZ) :
120 setattr(self._d, key, val) 121 setattr(self._d, key, val)
121 122
122 class Value (CAniTikZ) : 123 class Value (CAniTikZ) :
124 + @autorepr
123 def __init__ (self, init=None, **tikz) : 125 def __init__ (self, init=None, **tikz) :
124 super().__init__(tikz) 126 super().__init__(tikz)
125 self._h = [[init, self.IP, None]] 127 self._h = [[init, self.IP, None]]
...@@ -190,6 +192,7 @@ class Value (CAniTikZ) : ...@@ -190,6 +192,7 @@ class Value (CAniTikZ) :
190 ExecEnv._ValueClass = Value 192 ExecEnv._ValueClass = Value
191 193
192 class Aggregate (CAniTikZ) : 194 class Aggregate (CAniTikZ) :
195 + @autorepr
193 def __init__ (self, init, **tikz) : 196 def __init__ (self, init, **tikz) :
194 super().__init__(tikz) 197 super().__init__(tikz)
195 if isinstance(init, int) : 198 if isinstance(init, int) :
...@@ -315,6 +318,7 @@ class Aggregate (CAniTikZ) : ...@@ -315,6 +318,7 @@ class Aggregate (CAniTikZ) :
315 318
316 class Array (Aggregate) : 319 class Array (Aggregate) :
317 _defaults = {"aggregate": {"index": "north"}} 320 _defaults = {"aggregate": {"index": "north"}}
321 + @autorepr
318 def __init__ (self, init, index=[], **tikz) : 322 def __init__ (self, init, index=[], **tikz) :
319 super().__init__(init, **tikz) 323 super().__init__(init, **tikz)
320 self._o.aggregatescope = self._o.arrayscope 324 self._o.aggregatescope = self._o.arrayscope
...@@ -366,6 +370,7 @@ class Array (Aggregate) : ...@@ -366,6 +370,7 @@ class Array (Aggregate) :
366 class Struct (Aggregate) : 370 class Struct (Aggregate) :
367 _defaults = {"aggregate": {"grow": "south", 371 _defaults = {"aggregate": {"grow": "south",
368 "ticks": "west"}} 372 "ticks": "west"}}
373 + @autorepr
369 def __init__ (self, init, **tikz) : 374 def __init__ (self, init, **tikz) :
370 self.__dict__.update(_d={}, _o=None, _first=None, _last=None, nodeid=None) 375 self.__dict__.update(_d={}, _o=None, _first=None, _last=None, nodeid=None)
371 super().__init__(init, **tikz) 376 super().__init__(init, **tikz)
...@@ -382,6 +387,7 @@ class Struct (Aggregate) : ...@@ -382,6 +387,7 @@ class Struct (Aggregate) :
382 387
383 class Heap (CAniTikZ) : 388 class Heap (CAniTikZ) :
384 _defaults = {"group": {"inner sep": "5mm"}} 389 _defaults = {"group": {"inner sep": "5mm"}}
390 + @autorepr
385 def __init__ (self, **tikz) : 391 def __init__ (self, **tikz) :
386 super().__init__(tikz) 392 super().__init__(tikz)
387 self._alloc = {} 393 self._alloc = {}
...@@ -426,6 +432,7 @@ _flip = {"right": "below", ...@@ -426,6 +432,7 @@ _flip = {"right": "below",
426 "above": "left"} 432 "above": "left"}
427 433
428 class Box (CAniTikZ) : 434 class Box (CAniTikZ) :
435 + @autorepr
429 def __init__ (self, *content, grow="right", distance="15mm", parent=None, **tikz) : 436 def __init__ (self, *content, grow="right", distance="15mm", parent=None, **tikz) :
430 super().__init__(tikz) 437 super().__init__(tikz)
431 self._grow = grow 438 self._grow = grow
......
1 from inspect import Signature, Parameter 1 from inspect import Signature, Parameter
2 2
3 -from . import CAni 3 +from . import CAni, autorepr
4 from .highlight import pygmentize 4 from .highlight import pygmentize
5 5
6 class _CODE (CAni) : 6 class _CODE (CAni) :
7 _fields = [] 7 _fields = []
8 _options = [] 8 _options = []
9 + @autorepr
9 def __init__ (self, *l, **k) : 10 def __init__ (self, *l, **k) :
10 params = [] 11 params = []
11 for i, name in enumerate(self._fields) : 12 for i, name in enumerate(self._fields) :
...@@ -19,7 +20,7 @@ class _CODE (CAni) : ...@@ -19,7 +20,7 @@ class _CODE (CAni) :
19 params.append(Parameter(name, Parameter.POSITIONAL_OR_KEYWORD, default=None)) 20 params.append(Parameter(name, Parameter.POSITIONAL_OR_KEYWORD, default=None))
20 params.append(Parameter("src", Parameter.KEYWORD_ONLY, default=None)) 21 params.append(Parameter("src", Parameter.KEYWORD_ONLY, default=None))
21 sig = Signature(params) 22 sig = Signature(params)
22 - args = sig.bind(*l, **k) 23 + args = self._args = sig.bind(*l, **k)
23 args.apply_defaults() 24 args.apply_defaults()
24 for key, val in args.arguments.items() : 25 for key, val in args.arguments.items() :
25 setattr(self, key, val) 26 setattr(self, key, val)
...@@ -84,6 +85,7 @@ class STMT (_CODE) : ...@@ -84,6 +85,7 @@ class STMT (_CODE) :
84 85
85 class EXPR (_CODE) : 86 class EXPR (_CODE) :
86 _fields = ["expr"] 87 _fields = ["expr"]
88 + @autorepr
87 def __init__ (self, *l, **k) : 89 def __init__ (self, *l, **k) :
88 super().__init__(*l, **k) 90 super().__init__(*l, **k)
89 if self.src is None : 91 if self.src is None :
...@@ -113,6 +115,7 @@ class ENV (_CODE) : ...@@ -113,6 +115,7 @@ class ENV (_CODE) :
113 115
114 class WS (_CODE) : 116 class WS (_CODE) :
115 _fields = [] 117 _fields = []
118 + @autorepr
116 def __init__ (self, src) : 119 def __init__ (self, src) :
117 super().__init__(src=src) 120 super().__init__(src=src)
118 def __call__ (self) : 121 def __call__ (self) :
...@@ -120,6 +123,14 @@ class WS (_CODE) : ...@@ -120,6 +123,14 @@ class WS (_CODE) :
120 def tex (self) : 123 def tex (self) :
121 return self.src 124 return self.src
122 125
126 +class RAW (_CODE) :
127 + _fields = []
128 + @autorepr
129 + def __init__ (self, src) :
130 + super().__init__(src=src)
131 + def __call__ (self) :
132 + pass
133 +
123 class XDECL (_CODE) : 134 class XDECL (_CODE) :
124 _fields = ["*names"] 135 _fields = ["*names"]
125 def __call__ (self) : 136 def __call__ (self) :
...@@ -155,8 +166,7 @@ class DECL (_CODE) : ...@@ -155,8 +166,7 @@ class DECL (_CODE) :
155 value=tail.format(value=value)) 166 value=tail.format(value=value))
156 167
157 class BreakLoop (Exception) : 168 class BreakLoop (Exception) :
158 - def __init__ (self) : 169 + pass
159 - super().__init__()
160 170
161 class BREAK (_CODE) : 171 class BREAK (_CODE) :
162 def __call__ (self) : 172 def __call__ (self) :
...@@ -233,4 +243,3 @@ class FUNC (_CODE) : ...@@ -233,4 +243,3 @@ class FUNC (_CODE) :
233 self.body() 243 self.body()
234 except FunctionReturn as exc : 244 except FunctionReturn as exc :
235 self._env["RET"] = exc.RET 245 self._env["RET"] = exc.RET
236 -
......