Skip to content

Commit 419efb2

Browse files
saran-talimuldal
authored andcommitted
Make a readonly copy of each argument of WalkerPose, and turn WalkerPose into a class.
PiperOrigin-RevId: 246039614
1 parent 1218f3f commit 419efb2

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

  • dm_control/locomotion/walkers

dm_control/locomotion/walkers/base.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,27 @@
3535
_RANGEFINDER_SCALE = 10.0
3636
_TOUCH_THRESHOLD = 1e-3
3737

38-
_WalkerPose = collections.namedtuple('WalkerPose', ('qpos', 'xpos', 'xquat'))
3938

39+
def _make_readonly_float64_copy(value):
40+
if np.isscalar(value):
41+
return np.float64(value)
42+
else:
43+
out = np.array(value, dtype=np.float64)
44+
out.flags.writeable = False
45+
return out
46+
47+
48+
class WalkerPose(collections.namedtuple(
49+
'WalkerPose', ('qpos', 'xpos', 'xquat'))):
50+
51+
__slots__ = ()
4052

41-
def WalkerPose(qpos=0.0, xpos=(0, 0, 0), xquat=(1, 0, 0, 0)): # pylint: disable=invalid-name
42-
return _WalkerPose(qpos=qpos,
43-
xpos=np.array(xpos, dtype=np.float64),
44-
xquat=np.array(xquat, dtype=np.float64))
53+
def __new__(cls, qpos=0.0, xpos=(0, 0, 0), xquat=(1, 0, 0, 0)):
54+
return super(WalkerPose, cls).__new__(
55+
cls,
56+
qpos=_make_readonly_float64_copy(qpos),
57+
xpos=_make_readonly_float64_copy(xpos),
58+
xquat=_make_readonly_float64_copy(xquat))
4559

4660

4761
@six.add_metaclass(abc.ABCMeta)

0 commit comments

Comments
 (0)