Newer
Older
"""
Copyright (C) 2010 CNRS
Author: Florent Lamiraux
"""
import wrap
Transform a string of format '[n](x_1,x_2,...,x_n)' into a tuple of numbers.
a = re.match('\[(\d+)\]',vector)
size = int(a.group(1))
format = '\('
for i in range(size):
format += '(.*)'
if i != size-1:
format += ','
format += '\)'
a = re.search(format, vector)
res = []
for i in range (1, size+1):
res.append (float(a.group(i)))
return tuple (res)
def tupleToString (vector) :
"""
Transform a tuple of numbers into a string of format
'[n](x_1, x_2, ..., x_n)'
"""
string = '[%d]('%len (vector)
for x in vector[:-1]:
string += '%f,'%x
string += '%f)'%vector[-1]
return string
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
def stringToMatrix (string) :
"""
Transform a string of format
'[n,m]((x_11,x_12,...,x_1m),...,(x_n1,x_n2,...,x_nm))' into a tuple
of tuple of numbers.
"""
# Find matrix size
a = re.search ('\[(\d+),(\d+)]', string)
nRows = int (a.group(1))
nCols = int (a.group(2))
format = '\('
for row in range (nRows) :
format += '\('
for col in range (nCols) :
format += '(.*)'
if col != nCols-1 :
format += ','
format += '\)'
if row != nRows-1 :
format += ','
format += '\)'
a = re.search (format, string)
m = []
index = 1
for r in range (nRows) :
row = []
for c in range (nCols) :
row.append (float (a.group (index)))
index += 1
m.append (tuple (row))
return tuple (m)
def matrixToString(matrix) :
"""
Transform a tuple of tuple of numbers into a string of format
'[n,m]((x_11,x_12,...,x_1m),...,(x_n1,x_n2,...,x_nm))'.
"""
nRows = len(matrix)
if nRows is 0 :
return '[0,0](())'
nCols = len(matrix[0])
string = '[%d,%d](' % (nRows, nCols)
for r in range (nRows) :
string += '('
for c in range (nCols) :
string += str (float (matrix [r][c]))
if c != nCols-1 :
string += ','
string += ')'
if r != nRows -1 :
string += ','
string += ')'
return string
def objectToString(obj) :
"""
Transform an object to a string. Object is either
- a floating point number,
- an integer,
- a boolean,
- a vector or
- a matrix
"""
if (isinstance(obj, tuple)) :
# matrix or vector
if len(obj) is 0 :
return ""
else :
if (isinstance(obj[0], tuple)) :
#matrix
return matrixToString(obj)
else :
#vector
return tupleToString(obj)
else :
return str(obj)
def stringToObject(string) :
"""
Convert a string into one of the following types
- a matrix (tuple of tuple),
- a vector,
- an integer,
- a floating point number.
Successively attempts conversion in the above order and return
on success. If no conversion fits, the string is returned.
"""
try :
return stringToMatrix(string)
except :
pass
try :
return stringToTuple(string)
except :
pass
try :
return int(string)
except :
pass
try :
return float(string)
except :
return string
class SignalBase (object) :
"""
This class binds dynamicgraph::SignalBase<int> C++ class
"""
def __init__(self, name, object = None) :
"""
Constructor: if not called by a child class, create and store a pointer
to a C++ SignalBase<int> object.
self.object = object
if not self.object :
self.object = wrap.create_signal_base(self, name)
@property
def time(self) :
"""
Get time of signal
"""
return wrap.signalBaseGetTime(self.object)
@property
def value(self) :
Setter and getter for the value of a signal
Binds C++ SignalBase<int>::get() and set() methods. Values are passed
through string streams.
A string is interpreted as respectively:
* a matrix (tuple of tuple) if string fits '[n,m]((x_11,x_12,...,x_1m),...,(x_n1,x_n2,...,x_nm))' format where n and m are integers, x_ij are floating point numbers,
* a tuple if string fits '[n](x_1, x_2, ..., x_n)' format,
* an integer,
* a floating point number.
If string fits none of the above formats, no conversion is performed.
For instance, is s binds a signal of type vector,
>>> s.value = (2.5, .1, 1e2)
will call SignalBase<int>::set("[3](2.5,0.1,100.0)") and
>>> s.value
(2.5, 0.1, 100.0)
string = wrap.signal_base_get_value(self.object)
return stringToObject(string)
@value.setter
def value(self, val) :
"""
Set the signal as a constant signal with given value.
If the signal is plugged, it will be unplugged
"""
string = objectToString(val)
return wrap.signal_base_set_value(self.object, string)