attime.py
Go to the documentation of this file.
1 # Define a python class to execute sequencial action during a SOT execution.
2 #
3 # Examples would be:
4 # attime(1800,lambda: sigset(sot.damping,0.9))
5 # attime(1920,lambda: sigset(sot.damping,0.1))
6 # attime(1920,lambda: pop(tw))
7 # attime(850,lambda: sigset(taskSupportSmall.controlGain,0.01))
8 # @attime(400)
9 # def action400():
10 # print 'toto'
11 #
12 
13 
15  None
16 
17 
18 ALWAYS = attimeAlways()
19 
20 
21 class attimeStop:
22  None
23 
24 
25 STOP = attimeStop()
26 
27 
28 class Calendar:
29  def __init__(self):
30  self.events = dict()
31  self.ping = list()
32  # self.periodic=list()
33 
34  def __repr__(self):
35  res = ""
36  for iter, funpairs in sorted(self.events.iteritems()):
37  res += str(iter) + ": \n"
38  for funpair in funpairs:
39  if funpair[1] == "":
40  res += funpair[0] + "\n"
41  else:
42  res += str(funpair[1]) + "\n"
43  return res
44 
45  def stop(self, *args):
46  self.registerEvents(STOP, *args)
47 
48  def registerEvent(self, iter, pairfundoc):
49  # if iter==ALWAYS:
50  # self.periodic.append(pairfundoc)
51  # return
52  if iter == STOP:
53  self.events[ALWAYS].remove(pairfundoc)
54 
55  if iter not in self.events.keys():
56  self.events[iter] = list()
57  self.events[iter].append(pairfundoc)
58 
59  def registerEvents(self, iter, *funs):
60  """
61  3 entry types are possible: 1. only the functor. 2. a pair
62  (functor,doc). 3. a list of pairs (functor,doc).
63  """
64  if len(funs) == 2 and callable(funs[0]) and isinstance(funs[1], str):
65  self.registerEvent(iter, (funs[0], funs[1]))
66  else:
67  for fun in funs:
68  if isinstance(fun, tuple):
69  self.registerEvent(iter, fun)
70  else: # assert iscallable(fun)
71  if "functor" in fun.__dict__:
72  self.registerEvent(iter, (fun.functor, fun.functor.__doc__))
73  else:
74  self.registerEvent(iter, (fun, fun.__doc__))
75 
76  def addPing(self, f):
77  self.ping.append(f)
78 
79  def callPing(self):
80  for f in self.ping:
81  f()
82 
83  def run(self, iter, *args):
84  if ALWAYS in self.events:
85  for fun, doc in self.events[ALWAYS]:
86  fun(*args)
87  if iter in self.events:
88  self.callPing()
89  for fun, doc in self.events[iter]:
90  intro = "At time " + str(iter) + ": "
91  if doc is not None:
92  print(intro, doc)
93  else:
94  if fun.__doc__ is not None:
95  print(intro, fun.__doc__)
96  else:
97  print(intro, "Runing ", fun)
98  fun(*args)
99 
100  def __call__(self, iterarg, *funs):
101  if len(funs) == 0:
102  return self.generatorDecorator(iterarg)
103  else:
104  self.registerEvents(iterarg, *funs)
105 
106  def generatorDecorator(self, iterarg):
107  """
108  This next calling pattern is a little bit strange. Use it to decorate
109  a function definition: @attime(30) def run30(): ...
110  """
111 
112  class calendarDeco:
113  iterRef = iterarg
114  calendarRef = self
115  fun = None
116 
117  def __init__(selfdeco, functer):
118  if functer.__doc__ is None:
119  functer.__doc__ = "No doc fun"
120  if len(functer.__doc__) > 0:
121  selfdeco.__doc__ = functer.__doc__
122  selfdeco.__doc__ += (
123  " (will be run at time " + str(selfdeco.iterRef) + ")"
124  )
125  selfdeco.fun = functer
126  selfdeco.calendarRef.registerEvents(
127  selfdeco.iterRef, functer, functer.__doc__
128  )
129 
130  def __call__(selfdeco, *args):
131  selfdeco.fun(*args)
132 
133  return calendarDeco
134 
135  def fastForward(self, t):
136  for i in range(t + 1):
137  self.run(i)
138 
139 
140 attime = Calendar()
141 
142 
143 def sigset(s, v):
144  return s.__class__.value.__set__(s, v)
145 
146 
147 def refset(mt, v):
148  return mt.__class__.ref.__set__(mt, v)
def registerEvents(self, iter, funs)
Definition: attime.py:59
void f(void)
def registerEvent(self, iter, pairfundoc)
Definition: attime.py:48
def __call__(self, iterarg, funs)
Definition: attime.py:100
dynamicgraph::Vector & fun(dynamicgraph::Vector &res, double)
Definition: test_signal.cpp:33


sot-core
Author(s): Olivier Stasse, ostasse@laas.fr
autogenerated on Wed Jun 21 2023 02:51:26