introduced Datatype.copy
as Datatype got mutable, it has to be copied when inherited. Params.copy must call the introduced method Datatype.copy. in addition: - fixed bugs in ScaledInteger.__repr__ and datatypes.DATATYPES['struct'] - do not export unit from Parameters Change-Id: Id552c33843b1b2bedffc68d1bd909705dcfb5605 Reviewed-on: https://forge.frm2.tum.de/review/20324 Tested-by: JenkinsCodeReview <bjoern_pedersen@frm2.tum.de> Reviewed-by: Enrico Faulhaber <enrico.faulhaber@frm2.tum.de> Reviewed-by: Markus Zolliker <markus.zolliker@psi.ch>
This commit is contained in:
@@ -104,6 +104,13 @@ class DataType(object):
|
||||
value = default
|
||||
setattr(self, key, func(value))
|
||||
|
||||
def copy(self):
|
||||
"""make a deep copy of the datatype"""
|
||||
|
||||
# looks like the simplest way to make a deep copy
|
||||
return get_datatype(self.export_datatype())
|
||||
|
||||
|
||||
class FloatRange(DataType):
|
||||
"""Restricted float type"""
|
||||
|
||||
@@ -243,6 +250,8 @@ class ScaledInteger(DataType):
|
||||
raise ValueError(u'absolute_resolution MUST be >=0')
|
||||
if self.relative_resolution < 0:
|
||||
raise ValueError(u'relative_resolution MUST be >=0')
|
||||
# Remark: Datatype.copy() will round min, max to a multiple of self.scale
|
||||
# this should be o.k.
|
||||
|
||||
@property
|
||||
def as_json(self):
|
||||
@@ -272,7 +281,7 @@ class ScaledInteger(DataType):
|
||||
def __repr__(self):
|
||||
hints = self.as_json[1]
|
||||
hints.pop('scale')
|
||||
items = [self.scale]
|
||||
items = ['%g' % self.scale]
|
||||
for k,v in hints.items():
|
||||
items.append(u'%s=%r' % (k,v))
|
||||
return u'ScaledInteger(%s)' % (', '.join(items))
|
||||
@@ -307,6 +316,10 @@ class EnumType(DataType):
|
||||
kwds.pop(u'members')
|
||||
self._enum = Enum(enum_or_name, **kwds)
|
||||
|
||||
def copy(self):
|
||||
# as the name is not exported, we have to implement copy ourselfs
|
||||
return EnumType(self._enum)
|
||||
|
||||
@property
|
||||
def as_json(self):
|
||||
return [u'enum'] + [{u"members":dict((m.name, m.value) for m in self._enum.members)}]
|
||||
@@ -752,7 +765,7 @@ DATATYPES = dict(
|
||||
array =lambda min, max, members: ArrayOf(get_datatype(members), minsize=min, maxsize=max),
|
||||
tuple =lambda members=[]: TupleOf(*map(get_datatype, members)),
|
||||
enum =lambda members={}: EnumType('', members=members),
|
||||
struct =lambda members: StructOf(
|
||||
struct =lambda optional=None, members=None: StructOf(optional,
|
||||
**dict((n, get_datatype(t)) for n, t in list(members.items()))),
|
||||
command = lambda argument, result: CommandType(get_datatype(argument), get_datatype(result)),
|
||||
)
|
||||
|
||||
@@ -44,7 +44,7 @@ class Accessible(CountedObj):
|
||||
['%s=%r' % (k, v) for k, v in sorted(self.__dict__.items())]))
|
||||
|
||||
def copy(self):
|
||||
# return a copy of ourselfs
|
||||
'''return a copy of ourselfs'''
|
||||
props = self.__dict__.copy()
|
||||
return type(self)(**props)
|
||||
|
||||
@@ -146,11 +146,15 @@ class Parameter(Accessible):
|
||||
if ctr is not None:
|
||||
self.ctr = ctr
|
||||
|
||||
def copy(self):
|
||||
'''return a copy of ourselfs'''
|
||||
result = Accessible.copy(self)
|
||||
result.datatype = result.datatype.copy()
|
||||
return result
|
||||
|
||||
def for_export(self):
|
||||
# used for serialisation only
|
||||
res = self.exported_properties()
|
||||
if self.unit:
|
||||
res['unit'] = self.unit
|
||||
return res
|
||||
|
||||
def export_value(self):
|
||||
|
||||
Reference in New Issue
Block a user