Source code for iCalibrationDB.util

import copy
import struct


[docs]def float_from_integer(integer): return struct.unpack('!d', struct.pack('!Q', integer))[0]
[docs]def integer_from_float(double): return struct.unpack('!Q', struct.pack('!d', double))[0]
[docs]class DictConvertable: """ A class whose properties are convertible to a `dict`. Overwrite the `mandatory` list in derived classes to specify mandatory properties. Overwrite the `retrieve_optionals` in derived classes to specify which parameters out of mandatory are optional for retrieve queries. """ mandatory = [] retrieve_optionals = [] def __init__(self): self._datadict = dict()
[docs] def to_dict(self, mode="send", silent=True): """ Convert properties of class to to `dict`. """ # verification if necessary params are set mandatory = copy.copy(self.mandatory) if mode != "send": for rem in self.retrieve_optionals: mandatory.remove(rem) valid = all(c in self._datadict for c in mandatory) if not valid: # msg = "Not all parameters have been set!\n" msg += "\n".join(["{}: {}".format(c, c in self._datadict) for c in mandatory]) raise AttributeError(msg) d = {} for k, v in self._datadict.items(): if hasattr(v, "to_dict"): if not silent: print("Converting {} to dict".format(k)) d[k] = v.to_dict(mode) else: if isinstance(v, list): dl = [i.to_dict(mode) if hasattr(i, "to_dict") else i for i in v] elif isinstance(v, tuple): dl = (i.to_dict(mode) if hasattr(i, "to_dict") else i for i in v) else: dl = v d[k] = dl return d