Skip to content

Commit

Permalink
Automatic property type conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinStrohalm committed Dec 3, 2021
1 parent 2b05fe4 commit 6609445
Show file tree
Hide file tree
Showing 5 changed files with 353 additions and 4 deletions.
59 changes: 59 additions & 0 deletions pyeds/eds/prop.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,29 @@ def RawValue(self):
return self._raw_value


def SetValue(self, value):
"""
Sets given value to property.
Args:
value: ?
Value to set.
"""

# convert naive value
value = self._create_value(value)

# skip if same
if value == self._value:
return

# set raw value
self._raw_value = self._revert_value(value)

# set new value
self._value = value


def _convert_value(self, value):
"""Converts raw value to final type."""

Expand All @@ -133,3 +156,39 @@ def _convert_value(self, value):
value = self._type.ValueTypeConverter.Convert(value)

return value


def _revert_value(self, value):
"""Reverts final value to raw type."""

# apply specific converter (e.g. traces, spectra)
if self._type.ValueTypeConverter is not None:
value = self._type.ValueTypeConverter.Revert(value)

# convert value from special value type (e.g. enum, ddmap)
if self._type.SpecialValueType is not None:
value = self._type.SpecialValueType.Revert(value)

# convert value from custom data type (e.g. string, int, binary)
if self._type.CustomDataType is not None:
value = self._type.CustomDataType.Revert(value)

return value


def _create_value(self, value):
"""Creates final value from naive data."""

# apply specific converter (e.g. traces, spectra)
if self._type.ValueTypeConverter is not None:
return self._type.ValueTypeConverter.Create(value)

# convert value to special value type (e.g. enum, ddmap)
if self._type.SpecialValueType is not None:
return self._type.SpecialValueType.Create(value)

# convert value to custom data type (e.g. string, int, binary)
if self._type.CustomDataType is not None:
return self._type.CustomDataType.Create(value)

return value
90 changes: 90 additions & 0 deletions pyeds/report/cdtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,96 @@ def Convert(self, value):
raise TypeError(message)


def Revert(self, value):
"""
Reverts current type value back to original database value.
This method is not intended to be used by user. It is used automatically
by the library itself.
Args:
value: ?
Current type value to be converted.
Returns:
?
Value converted into original database type.
"""

if value is None:
return None

if self.Name == 'Int':
return int(value)

if self.Name == 'Int64':
return int(value)

if self.Name == 'Double':
return float(value)

if self.Name == 'String':
return str(value)

if self.Name == 'Boolean':
return int(value)

if self.Name == 'Binary':
return value.Value

if self.Name == 'Object':
return value

message = "'%s' is not known custom data type!" % self.Name
raise TypeError(message)


def Create(self, value):
"""
Creates current type value from naive data.
This method is not intended to be used by user. It is used automatically
by the library itself.
Args:
value: ?
Naive value to be converted.
Returns:
?
Value converted into current type.
"""

if value is None:
return None

if self.Name == 'Int':
return int(value)

if self.Name == 'Int64':
return int(value)

if self.Name == 'Double':
return float(value)

if self.Name == 'String':
return str(value)

if self.Name == 'Boolean':
return bool(value)

if self.Name == 'Binary':
if not isinstance(value, Binary):
return Binary(value)
return value

if self.Name == 'Object':
return value

message = "'%s' is not known custom data type!" % self.Name
raise TypeError(message)


@staticmethod
def FromDBData(data):
"""
Expand Down
32 changes: 32 additions & 0 deletions pyeds/report/converters/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,35 @@ def Convert(self, value):
"""

raise NotImplementedError()


def Revert(self, value):
"""
Reverts final property value back to its original database type.
Args:
value: ?
Final property value to convert.
Returns:
?
Value converted into original database type.
"""

raise NotImplementedError()


def Create(self, value):
"""
Creates final property value from naive data.
Args:
value: ?
Naive value to be converted.
Returns:
?
Final property value.
"""

raise NotImplementedError()
96 changes: 96 additions & 0 deletions pyeds/report/ddmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,102 @@ def Convert(self, value):
return val


def Revert(self, value):
"""
Reverts data distribution value back to binary value.
This method is not intended to be used by user. It is used automatically
by the library itself.
Args:
value: pyeds.DataDistributionValue or None
Data distribution value.
Returns:
pyeds.Binary or None
Value converted into original database type.
"""

# check value
if value is None:
return None

# check type
if not isinstance(value, DataDistributionValue):
message = "Value must be of type pyeds.DataDistributionValue! -> %s" % (type(value))
raise TypeError(message)

# get size and format
size = len(self.Boxes)
f = _FORMAT[self.CustomDataType.Name]

# prepare data
data = [0] * (2*size)
for i, val in enumerate(value.Values):
if val is None or val is False:
data[2*i] = 0
data[2*i+1] = False
else:
data[2*i] = val
data[2*i+1] = True

# pack data
buffer = struct.pack("<"+f*size, *data)

# create binary
binary = Binary(buffer)

return binary


def Create(self, value):
"""
Creates data distribution value from naive data:
This method is not intended to be used by user. It is used automatically
by the library itself.
Args:
value: pyeds.DataDistributionValue, tuple or list
Naive value to be converted.
Returns:
pyeds.DataDistributionValue or None
Parsed data distribution value.
"""

# check None
if value is None:
return True

# get values from ddmap
if isinstance(value, DataDistributionValue):
value = value.Values

# check type
if not isinstance(value, (list, tuple)):
message = "Value must be of type pyeds.DataDistributionValue, tuple or list! -> %s" % (type(value),)
raise TypeError(message)

# check size
if len(value) != len(self._boxes):
message = "Value has incorrect length!"
raise ValueError(message)

# check values
t = _TYPE[self.CustomDataType.Name]
for item in value:
if item is not None and not isinstance(item, (t,)):
message = "Values must be of type %s! -> %s" % (t, type(item))
raise TypeError(message)

# create value
val = DataDistributionValue(self, value)
val.Lock()

return val


@staticmethod
def FromDBData(data):
"""
Expand Down
Loading

0 comments on commit 6609445

Please sign in to comment.