def __init__(self, name, value=Q(0), categories=None): super(GlobalVariable, self).__init__() self.decimation = StaticDecimation(Q(10, 's')) self.history = deque(maxlen=10) self._value = value self._name = name self.categories = categories
def __init__(self, pulser, config, configName, globalDict, parent=None): self.isSetup = False dacBase.__init__(self, parent) dacForm.__init__(self) self.config = config self.dac = DAC(pulser) self.channelsConfigName = '{0}.dacExpressionChannels'.format( configName) self.autoApplyConfigName = '{0}.autoApply'.format(configName) self.guiStateConfigName = '{0}.guiState'.format(configName) self.dacChannels = self.config.get(self.channelsConfigName) if not self.dacChannels or len( self.dacChannels) != self.dac.numChannels: self.dacChannels = [ DACChannelSetting(globalDict=globalDict) for _ in range(self.dac.numChannels) ] for index, channel in enumerate(self.dacChannels): channel.globalDict = globalDict channel.onChange = partial(self.onChange, index) self.autoApply = self.config.get(self.autoApplyConfigName, True) self.decimation = defaultdict(lambda: StaticDecimation(Q(30, 's'))) self.persistence = DBPersist() self.globalDict = globalDict self.pulser = pulser
def __init__(self, pulser, config, configName='DDSUi', globalDict=dict(), parent=None): DDSBase.__init__(self, parent) DDSForm.__init__(self) if pulser.pulserConfiguration(): self.channelInfo = sorted(list( pulser.pulserConfiguration().ddsChannels.values()), key=lambda x: x.channel) else: self.channelInfo = [] self.numChannels = len(self.channelInfo) self.config = config self.channelConfigName = '{0}.ddsChannels'.format(configName) self.autoApplyConfigName = '{0}.autoApply'.format(configName) self.guiStateConfigName = '{0}.guiState'.format(configName) self.ad9912 = Ad9912.Ad9912(pulser) oldDDSChannels = self.config.get(self.channelConfigName, []) self.ddsChannels = [ oldDDSChannels[i] if i < len(oldDDSChannels) else DDSChannelSettings() for i in range(self.numChannels) ] self.autoApply = self.config.get(self.autoApplyConfigName, True) self.decimation = defaultdict(lambda: StaticDecimation(Q(30, 's'))) self.persistence = DBPersist() self.globalDict = globalDict self.pulser = pulser self.persistSpace = 'DDS' for index, channelinfo in enumerate(self.channelInfo): self.ddsChannels[index].channel = channelinfo.channel self.ddsChannels[index].shutter = channelinfo.shutter
def __setstate__(self, state): super(GlobalVariable, self).__init__() self.decimation = StaticDecimation(Q(10, 's')) self._name, self._value, self.categories, self.history = state
class GlobalVariable(QtCore.QObject): """Class for encapsulating a global variable. Attributes: valueChanged (PyQt signal): emitted when value is changed, with signature (name, new-value, origin-of-change) decimation (StaticDecimation): takes care of saving globals to database every 10 seconds history (deque): history of the last 10 values the global has been set to _value (magnitude): the global's value, accessed via 'value' property _name (str): the global's name, accessed via 'name' property categories (list[str]): the global's cateogires """ valueChanged = QtCore.pyqtSignal(object, object, object) persistSpace = 'globalVar' persistence = DBPersist() def __init__(self, name, value=Q(0), categories=None): super(GlobalVariable, self).__init__() self.decimation = StaticDecimation(Q(10, 's')) self.history = deque(maxlen=10) self._value = value self._name = name self.categories = categories @property def value(self): return self._value @value.setter def value(self, newvalue): """set the value and record the change""" if isinstance(newvalue, tuple): v, o = newvalue else: v, o = newvalue, None if self._value != v or not (type(self._value) is type(v)) or ( is_Q(v) and (type(self._value.m) is type(v.m))): self._value = v self.valueChanged.emit(self.name, v, o) self.history.appendleft((v, time.time(), o)) if o is not None: self.persistCallback((time.time(), v, None, None)) else: self.decimation.decimate(time.time(), v, self.persistCallback) @property def name(self): return self._name @name.setter def name(self, newname): """set the name and record the change in the database""" self._name, oldname = newname, self._name self.persistence.rename(self.persistSpace, oldname, newname) def __getstate__(self): return self._name, self._value, self.categories, self.history def __setstate__(self, state): super(GlobalVariable, self).__init__() self.decimation = StaticDecimation(Q(10, 's')) self._name, self._value, self.categories, self.history = state def persistCallback(self, data): time, value, minval, maxval = data unit = None if is_Q(value): value, unit = value.m, "{:~}".format(value.units) self.persistence.persist(self.persistSpace, self.name, time, value, minval, maxval, unit) def toXmlElement(self, element): e = ElementTree.SubElement( element, "GlobalVariable", attrib={ 'type': 'Magnitude', 'name': self._name, 'categories': ", ".join(self.categories) if self.categories else '' }) e.text = repr(self._value) @classmethod def fromXmlElement(cls, element): categories = [ s.strip(" ") for s in element.attrib['categories'].split(",") ] return GlobalVariable(element.attrib['name'], parse(element.text), categories)