Skip to content

Commit 58482d3

Browse files
committedSep 12, 2013
Add support to Python 3 with a single codebase
1 parent 3033eb1 commit 58482d3

File tree

6 files changed

+60
-16
lines changed

6 files changed

+60
-16
lines changed
 

‎.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
language: python
22
python:
33
- "2.7"
4+
- "3.3"
45
before_install:
56
- pip install sympy --use-mirrors
67
- pip install numpy --use-mirrors
@@ -9,4 +10,4 @@ install:
910
before_script:
1011
- cd sympybotics
1112
script:
12-
- ../run_tests.sh
13+
- ../run_tests.sh

‎sympybotics/_compatibility_.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
""" Python 2/3 compatibility utils """
2+
3+
import sys
4+
pyversion = sys.version_info[0]
5+
6+
# exec (from https://bitbucket.org/gutworth/six/):
7+
if pyversion is 3:
8+
import builtins
9+
exec_ = getattr(builtins, "exec")
10+
del builtins
11+
elif pyversion is 2:
12+
def exec_(_code_, _globs_=None, _locs_=None):
13+
"""Execute code in a namespace."""
14+
if _globs_ is None:
15+
frame = sys._getframe(1)
16+
_globs_ = frame.f_globals
17+
if _locs_ is None:
18+
_locs_ = frame.f_locals
19+
del frame
20+
elif _locs_ is None:
21+
_locs_ = _globs_
22+
exec("""exec _code_ in _globs_, _locs_""")

‎sympybotics/dynamics/regressor.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def regressor(rbtdef, geom, ifunc=None):
2727
for p, parm in enumerate(dynparms):
2828

2929
for i in range(rbtdef.dof):
30-
rbtdeftmp.Le[i] = map(
31-
lambda x: 1 if x == parm else 0, rbtdef.Le[i])
30+
rbtdeftmp.Le[i] = list(map(
31+
lambda x: 1 if x == parm else 0, rbtdef.Le[i]))
3232
rbtdeftmp.l[i] = Matrix(rbtdef.l[i]).applyfunc(
3333
lambda x: 1 if x == parm else 0)
3434
rbtdeftmp.m[i] = 1 if rbtdef.m[i] == parm else 0

‎sympybotics/robotmodel.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .kinematics import Kinematics
77
from .dynamics import Dynamics
88
from .symcode import Subexprs, code_to_func
9+
from ._compatibility_ import exec_
910

1011
def _fprint(x):
1112
print(x)
@@ -99,11 +100,15 @@ def calc_base_parms(self, verbose=False):
99100
func_def_regressor = code_to_func(
100101
'python', self.H_code, 'regressor_func', ['q', 'dq', 'ddq'],
101102
q_subs)
103+
102104
global sin, cos, sign
103105
sin = numpy.sin
104106
cos = numpy.cos
105107
sign = numpy.sign
106-
exec(func_def_regressor)
108+
109+
l = locals()
110+
exec_(func_def_regressor, globals(), l)
111+
regressor_func = l['regressor_func']
107112

108113
if verbose:
109114
_fprint('calculating base parameters and regressor code')

‎sympybotics/symcode/subexprs.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def collect(self, exprs):
170170
prep_exprs = [preprocess_for_cse(e, self._optimizations)
171171
for e in exprs]
172172

173-
out_exprs = map(self._parse, prep_exprs)
173+
out_exprs = list(map(self._parse, prep_exprs))
174174

175175
if is_single_expr:
176176
return out_exprs[0]
@@ -193,7 +193,7 @@ def get(self, exprs=None, symbols=None):
193193

194194
# Find all of the repeated subexpressions.
195195

196-
ivar_se = {iv: se for se, iv in self._subexp_iv.iteritems()}
196+
ivar_se = {iv: se for se, iv in self._subexp_iv.items()}
197197

198198
used_ivs = set()
199199
repeated = set()

‎sympybotics/tests/test_results.py

+26-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
from math import cos, sin
44

55
import sympybotics
6+
from sympybotics._compatibility_ import exec_
7+
8+
9+
610

711

812
def test_scara_dh_sym_geo_kin():
@@ -121,11 +125,17 @@ def test_puma_dh_num_geo_kin_dyn():
121125
H_func_def = sympybotics.robotcodegen.dyn_code_to_func(
122126
'python', puma560.H_code, 'H_puma560', 2, puma560.dof)
123127

124-
exec(M_func_def)
125-
exec(c_func_def)
126-
exec(g_func_def)
127-
exec(tau_func_def)
128-
exec(H_func_def)
128+
l = locals()
129+
exec_(M_func_def, globals(), l)
130+
exec_(c_func_def, globals(), l)
131+
exec_(g_func_def, globals(), l)
132+
exec_(tau_func_def, globals(), l)
133+
exec_(H_func_def, globals(), l)
134+
tau_puma560 = l['tau_puma560']
135+
g_puma560 = l['g_puma560']
136+
c_puma560 = l['c_puma560']
137+
M_puma560 = l['M_puma560']
138+
H_puma560 = l['H_puma560']
129139

130140
tau = tau_puma560(dynparm_test, q_test, dq_test, ddq_test)
131141
tau = numpy.matrix(tau).T.astype(numpy.float64)
@@ -307,11 +317,17 @@ def test_puma_mdh_num_geo_kin_dyn():
307317
H_func_def = sympybotics.robotcodegen.dyn_code_to_func(
308318
'python', puma560.H_code, 'H_puma560', 2, puma560.dof)
309319

310-
exec(M_func_def)
311-
exec(c_func_def)
312-
exec(g_func_def)
313-
exec(tau_func_def)
314-
exec(H_func_def)
320+
l = locals()
321+
exec_(M_func_def, globals(), l)
322+
exec_(c_func_def, globals(), l)
323+
exec_(g_func_def, globals(), l)
324+
exec_(tau_func_def, globals(), l)
325+
exec_(H_func_def, globals(), l)
326+
tau_puma560 = l['tau_puma560']
327+
g_puma560 = l['g_puma560']
328+
c_puma560 = l['c_puma560']
329+
M_puma560 = l['M_puma560']
330+
H_puma560 = l['H_puma560']
315331

316332
tau = tau_puma560(dynparm_test, q_test, dq_test, ddq_test)
317333
tau = numpy.matrix(tau).T.astype(numpy.float64)

0 commit comments

Comments
 (0)
Please sign in to comment.