devctrl.devices.device_role.DeviceRole#

class DeviceRole(key: str, name: str, global_varname: str, device_type: type = None, connect_priority: int = -1)[source]#

Bases: SettingsClass

Describes the ‘role’ a device plays in a measurement setup.

Used both for device management (see DeviceManager) and for modules to declare the devices they need on the combined GUI shell (see devctrl.gui.app.blueprint.GuiModule.device_roles).

A DeviceRole provides information about a device instance that is optionally managed by a DeviceManager and can be seen as describing the ‘role’ of a device.

Parameters:
  • key – Device key. This is used with the DeviceManager and in config files. Should not contain spaces.

  • name – A descriptive device name, like Shutter for the blue laser.

  • global_varname – Name of a global variable that the connected device can be assigned to. Must be a valid python variable name.

  • device_type – The subclass of Device that the device belongs to.

  • connect_priority – A higher priority means the device should be connected before others (eg. a power switch) and disconnected after others.

Methods

as_dict

Convenience wrapper for dataclasses.asdict(self)

copy

Convenience method for copy.deepcopy(self)

fields

Return a tuple describing the fields of this dataclass.

from_dict

Recursively construct a SettingsClass from a dictionary.

get_dict

Return the settings as a dictionary.

load_yaml

save_yaml

Attributes

__annotations__ = {'connect_priority': <class 'int'>, 'device_type': <class 'type'>, 'global_varname': <class 'str'>, 'key': <class 'str'>, 'name': <class 'str'>}#
__dataclass_fields__ = {'connect_priority': Field(name='connect_priority',type=<class 'int'>,default=-1,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'device_type': Field(name='device_type',type=<class 'type'>,default=None,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'global_varname': Field(name='global_varname',type=<class 'str'>,default=<dataclasses._MISSING_TYPE object>,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'key': Field(name='key',type=<class 'str'>,default=<dataclasses._MISSING_TYPE object>,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'name': Field(name='name',type=<class 'str'>,default=<dataclasses._MISSING_TYPE object>,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD)}#
__dataclass_params__ = _DataclassParams(init=True,repr=True,eq=True,order=False,unsafe_hash=False,frozen=False,match_args=True,kw_only=False,slots=False,weakref_slot=False)#
__dict__ = mappingproxy({'__module__': 'devctrl.devices.device_role', '__firstlineno__': 6, '__annotations__': {'key': <class 'str'>, 'name': <class 'str'>, 'global_varname': <class 'str'>, 'device_type': <class 'type'>, 'connect_priority': <class 'int'>}, '__doc__': "Describes the 'role' a device plays in a measurement setup.\n\nUsed both for device management (see :class:`~devctrl.devices.DeviceManager`)\nand for modules to declare the devices they need on the combined GUI shell\n(see :attr:`devctrl.gui.app.blueprint.GuiModule.device_roles`).\n\nA ``DeviceRole`` provides information about a device instance that is\noptionally managed by a :class:`~devctrl.devices.DeviceManager` and can be\nseen as describing the 'role' of a device.\n\n:param key: Device key. This is used with the :class:`DeviceManager` and in config files. Should not contain spaces.\n:param name: A descriptive device name, like `Shutter for the blue laser`.\n:param global_varname: Name of a global variable that the connected device can be assigned to. Must be a valid python variable name.\n:param device_type: The subclass of :class:`Device` that the device belongs to.\n:param connect_priority: A higher priority means the device should be connected before others (eg. a power switch) and disconnected after others.\n", 'device_type': None, 'connect_priority': -1, '__static_attributes__': (), '__dataclass_params__': _DataclassParams(init=True,repr=True,eq=True,order=False,unsafe_hash=False,frozen=False,match_args=True,kw_only=False,slots=False,weakref_slot=False), '__dataclass_fields__': {'key': Field(name='key',type=<class 'str'>,default=<dataclasses._MISSING_TYPE object>,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'name': Field(name='name',type=<class 'str'>,default=<dataclasses._MISSING_TYPE object>,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'global_varname': Field(name='global_varname',type=<class 'str'>,default=<dataclasses._MISSING_TYPE object>,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'device_type': Field(name='device_type',type=<class 'type'>,default=None,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'connect_priority': Field(name='connect_priority',type=<class 'int'>,default=-1,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD)}, '__replace__': <function _replace>, '__hash__': None, '__init__': <function DeviceRole.__init__>, '__repr__': <function DeviceRole.__repr__>, '__eq__': <function DeviceRole.__eq__>, '__match_args__': ('key', 'name', 'global_varname', 'device_type', 'connect_priority')})#
__eq__(other)#

Return self==value.

__firstlineno__ = 6#
__hash__ = None#
__init__(key: str, name: str, global_varname: str, device_type: type = None, connect_priority: int = -1) → None#
__match_args__ = ('key', 'name', 'global_varname', 'device_type', 'connect_priority')#
__module__ = 'devctrl.devices.device_role'#
__or__(other)#

Merge with another settings class or a dictionary. Unknown keys in a dictionary are ignored.

__replace__(**changes)#
__repr__()#

Return repr(self).

__static_attributes__ = ()#
__weakref__#

list of weak references to the object

_get_dict(remove_empty_containers: bool = False, remove_None: bool = False, remove_if_None: list[str] | None = None) → dict#

Return the settings as a dictionary, optionally cleaning up the output.

Base implementation for get_dict(). Subclasses may call it with different arguments to configure the output.

Parameters:
  • remove_empty_containers – If True, remove empty dict, list, tuple or set values.

  • remove_None – If True, remove all keys with a None value.

  • remove_if_None – If given, remove these keys only if their value is None (absent keys are ignored).

Returns:

Instance converted to dictionary, see as_dict().

as_dict() → dict#

Convenience wrapper for dataclasses.asdict(self)

Returns all fields of the settings class. For a dictionary where certain fields may be skipped, see get_dict().

connect_priority: int = -1#
copy() → Self#

Convenience method for copy.deepcopy(self)

device_type: type = None#
fields()#

Return a tuple describing the fields of this dataclass.

Adapted version of dataclass.fields that applies additional sorting of the fields according to the field’s metadata[“order”] parameter.

Accepts a dataclass or an instance of one. Tuple elements are of type Field.

classmethod from_dict(data: dict, strict: bool = False) → Self#

Recursively construct a SettingsClass from a dictionary.

If the value of a field is another SettingsClass, from_dict() will be called for that field as well. Handled are:

  • SettingsClass

  • list[SettingsClass]

  • dict[<any>, SettingsClass]

  • all of the above as Optional (Union with None)

  • a Union with any handled types

Parameters:

strict – If True, raise a KeyError on keys in data that are not field names of cls. Unknown keys are ignored otherwise.

Raises:
  • ValueError – If data contains a value that has the wrong type for the field of cls (ONLY if the type is a subclass of SettingsClass! Other types are not enforced)

  • KeyError – If strict is True and data contains a key that is not a field name of cls

get_dict() → dict#

Return the settings as a dictionary.

as_dict() returns the settings class with all fields, while get_dict() may be configured by subclasses to skip certain fields in the output. The default implementation returns all fields, like as_dict(). :return: Instance converted to dictionary.

global_varname: str#
key: str#
classmethod load_yaml(yaml_path: str, loader=<class 'yaml.loader.Loader'>)#
name: str#
save_yaml(yaml_path: str)#