script_shortcuts.py
Go to the documentation of this file.
1 # Defines the following shortcuts:
2 # signal.name -> return the shortname
3 # signal -> display nicely the content of the signal
4 # signal(3) -> recompute the signal at time 3, and display nicely
5 # signal +1 -> increment the signal time of 1, recompute, and display.
6 # signal.deps -> display the graph dependancy up to the default value (3)
7 # signal.deps(6) -> same, but with depth = 6.
8 # entity -> same as print(entity)
9 # change the prompt to be '%'
10 
11 from __future__ import print_function
12 
13 # Changing prompt
14 import sys
15 
16 from .entity import Entity
17 from .signal_base import SignalBase
18 
19 
20 # Enables shortcut "name"
21 def sig_short_name(self):
22  return self.getName().split(":")[-1]
23 
24 
25 setattr(SignalBase, "name", property(sig_short_name))
26 
27 
28 # Enables shortcuts "m"
29 # This code implements a pseudo function 'm' in the class signal_base,
30 # with no args, or optional args. Three calls can be made:
31 # - sig.m : print the current value.
32 # - sig.m(time): recompute at given <time>, and display the current value
33 # - sig.m +time: recompute at <time> after current time, and display.
35  sig = None
36 
37  def __init__(self, sig):
38  self.sig = sig
39 
40  def __str__(self):
41  return self.sig.name + " = " + str(self.sig.value)
42 
43  def __repr__(self):
44  return str(self)
45 
46  def __call__(self, iter):
47  self.sig.recompute(iter)
48  return self
49 
50  def __add__(self, iter):
51  self.sig.recompute(self.sig.time + iter)
52  return self
53 
54 
55 def sigMatPrint(sig):
56  return PrettySignalPrint(sig)
57 
58 
59 setattr(SignalBase, "m", property(PrettySignalPrint))
60 
61 
62 # Enable the same as 'm', but directly on the signal object.
63 def sigRepr(self):
64  return self.name + " = " + str(self.value)
65 
66 
67 def sigCall(sig, iter):
68  sig.recompute(iter)
69  print(sigRepr(sig))
70 
71 
72 def sigTimeIncr(sig, iter):
73  sig.recompute(sig.time + iter)
74  print(sigRepr(sig))
75 
76 
77 setattr(SignalBase, "__repr__", sigRepr)
78 setattr(SignalBase, "__call__", sigCall)
79 setattr(SignalBase, "__add__", sigTimeIncr)
80 
81 
82 # Enables shortcut "deps"
83 # Implements the peudo function 'deps', that can be called without arg,
84 # or specifying a specific depth to be printed.
86  defaultDepth = 2
87  sig = None
88 
89  def __init__(self, sig):
90  self.sig = sig
91 
92  def __repr__(self):
93  return self.sig.displayDependencies(self.defaultDepth)
94 
95  def __call__(self, depth):
96  self.defaultDepth = depth
97  return self
98 
99 
100 setattr(SignalBase, "deps", property(SignalDepPrint))
101 
102 setattr(Entity, "sigs", property(Entity.displaySignals))
103 setattr(Entity, "__repr__", Entity.__str__)
104 
105 sys.ps1 = "% "
106 
107 
108 # Enable function that can be call without()def optionalparentheses(f):
110  class decoclass:
111  def __init__(self, f):
112  self.functor = f
113 
114  def __repr__(self):
115  res = self.functor()
116  if isinstance(res, str):
117  return res
118  else:
119  return ""
120 
121  def __call__(self, *arg):
122  return self.functor(*arg)
123 
124  return decoclass(f)


dynamic-graph-python
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Sun Jun 25 2023 02:55:50