Source code for pylab_ml.dummy

"""
Basic Dummy class for instance.

:Date: |today|
:Author: Semi-ATE <info@Semi-ATE.org>
"""


[docs] class Dummy(object): """ Dummy object for an communication-instance. :Date: |today| :Author: Semi-ATE <info@Semi-ATE.org> Usable if you have no real serial device, but you want avoid an Exception if you make access to this device """
[docs] def __init__(self, parent, logger, **kwargs): """ Initialise the Dummy instance. Parameters ---------- parent : object Parent object logger : object Logger instance kwargs : dict Additional keyword arguments """ self.parent = parent self.logger = logger # kwargs = {"addr": addr, "interface": interface, "backend": backend, "identify": identify, "instName": instName} if 'message' in kwargs: self.logger.error(kwargs['message']) else: self.logger.error(f'{self.parent.instName}.inst: Device or no connection found, use Dummy instead') self._lastcmd = '' self.bytes_in_buffer = 0
[docs] def query(self, cmd): """ Query the Dummy instance. Parameters ---------- cmd : str Command to query Returns ------- str or int Response from the Dummy instance """ if cmd == '*IDN?': return f'{self.__class__}\r' cmd = cmd[:cmd.find('?')] try: value = super(__class__, self).__getattribute__(cmd) try: value = int(value) except Exception: pass self.logger.debug(f'Dummy {self.parent.instName} query {cmd} == {value}') except Exception: value = 0xdeadbeef self.logger.debug(f'Dummy {self.parent.instName} query {cmd} == {hex(value)}') return value
[docs] def write(self, cmd): """ Write a command to the Dummy instance. Parameters ---------- cmd : str Command to write """ self._lastcmd = cmd[:cmd.find('?')] if cmd.find('?') > -1 else '' cmd = cmd.split(' ') if len(cmd) > 1: object.__setattr__(self, cmd[0], cmd[1]) self.logger.debug(f'Dummy {self.parent.instName} write {cmd}')
[docs] def read(self): """ Read a value from the Dummy instance. Returns ------- value : str or int Value read from the Dummy instance """ # value = 0xdeadbeef value = '-2' self.logger.debug(f'Dummy {self.parent.instName} read value') return value
def flush(self, arg=None): pass def __getattribute__(self, name): """ Get an attribute from the Dummy instance. Parameters ---------- name : str Name of the attribute Returns ------- value : str or int Value of the attribute """ try: value = super(__class__, self).__getattribute__(name) except Exception: # value = 0xdeadbeef value = '-2' return value
[docs] def close(self): """Close the Dummy instance.""" self.logger.debug(f'Dummy {self.parent.instName} close Dummy instance')