devctrl.data.data.DataAttributes#

class DataAttributes(writer: str | None = None, writer_version: str | None = None, data_type: str | None = None, data_version: str | None = None, time_created: float | None = None, time_modified: float | None = None, series: str | None = None)[source]#

Bases: SettingsClass

Methods

as_dict

Return all fields of the settings class as a dictionary.

copy

Convenience method for copy.deepcopy(self)

fields

Return a tuple describing the fields of this dataclass.

from_data_class

Build attributes from the class constants of a Data subclass.

from_dict

Recursively construct a SettingsClass from a dictionary.

get_dict

Return the settings as a dictionary.

load_yaml

read

Read the data attributes from the root of an HDF5 file, without raising.

save_yaml

stamp

Write these attributes as root attributes of an HDF5 file.

Attributes

data_type

Canonical type label of the data, value of the root data_type HDF5 attribute.

data_version

Version string "<major>.<minor>" of the data format that is saved.

series

Name of the measurement series the data belongs to (optional root series attribute).

time_created

Unix timestamp from when this data was initially written.

time_modified

Unix timestamp from when this data was last modified.

writer

Name of the software module which wrote this data.

writer_version

Version of the software module which wrote this data.

__annotations__ = {'data_type': str | None, 'data_version': str | None, 'series': str | None, 'time_created': float | None, 'time_modified': float | None, 'writer': str | None, 'writer_version': str | None}#
__dataclass_fields__ = {'data_type': Field(name='data_type',type=str | None,default=None,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'data_version': Field(name='data_version',type=str | None,default=None,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'series': Field(name='series',type=str | None,default=None,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'time_created': Field(name='time_created',type=float | None,default=None,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'time_modified': Field(name='time_modified',type=float | None,default=None,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'writer': Field(name='writer',type=str | None,default=None,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'writer_version': Field(name='writer_version',type=str | None,default=None,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.data.data', '__firstlineno__': 28, '__annotations__': {'writer': str | None, 'writer_version': str | None, 'data_type': str | None, 'data_version': str | None, 'time_created': float | None, 'time_modified': float | None, 'series': str | None}, 'writer': None, 'writer_version': None, 'data_type': None, 'data_version': None, 'time_created': None, 'time_modified': None, 'series': None, 'stamp': <function DataAttributes.stamp>, 'read': <classmethod(<function DataAttributes.read>)>, 'from_data_class': <classmethod(<function DataAttributes.from_data_class>)>, '__static_attributes__': (), '__doc__': 'DataAttributes(writer: str | None = None, writer_version: str | None = None, data_type: str | None = None, data_version: str | None = None, time_created: float | None = None, time_modified: float | None = None, series: str | None = None)', '__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__': {'writer': Field(name='writer',type=str | None,default=None,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'writer_version': Field(name='writer_version',type=str | None,default=None,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'data_type': Field(name='data_type',type=str | None,default=None,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'data_version': Field(name='data_version',type=str | None,default=None,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'time_created': Field(name='time_created',type=float | None,default=None,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'time_modified': Field(name='time_modified',type=float | None,default=None,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'series': Field(name='series',type=str | None,default=None,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 DataAttributes.__init__>, '__repr__': <function DataAttributes.__repr__>, '__eq__': <function DataAttributes.__eq__>, '__match_args__': ('writer', 'writer_version', 'data_type', 'data_version', 'time_created', 'time_modified', 'series')})#
__eq__(other)#

Return self==value.

__firstlineno__ = 28#
__hash__ = None#
__init__(writer: str | None = None, writer_version: str | None = None, data_type: str | None = None, data_version: str | None = None, time_created: float | None = None, time_modified: float | None = None, series: str | None = None) → None#
__match_args__ = ('writer', 'writer_version', 'data_type', 'data_version', 'time_created', 'time_modified', 'series')#
__module__ = 'devctrl.data.data'#
__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().

_reorder_dict(d: dict) → dict#

Reorder the keys of a dictionary created by dataclasses.asdict() to the order of fields().

Walks the fields of self in parallel to d and recurses into nested SettingsClass instances. All other values are kept as-is.

Parameters:

d – Dictionary as returned by dataclasses.asdict() for this instance.

Returns:

Dictionary with the same values, but keys ordered like fields().

as_dict() → dict#

Return all fields of the settings class as a dictionary.

Like dataclasses.asdict(), but the keys are ordered like fields(): also the keys of nested SettingsClass instances (as field values and inside lists, tuples and dicts) are ordered by their fields. This way save_yaml() and other YAML exports write the fields in the same order as they are defined in the class (or reordered by metadata["order"], like in the GUI).

Returns:

Instance converted to dictionary.

copy() → Self#

Convenience method for copy.deepcopy(self)

data_type: str | None = None#

Canonical type label of the data, value of the root data_type HDF5 attribute.

data_version: str | None = None#

Version string "<major>.<minor>" of the data format that is saved.

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_data_class(data_class: type[Data]) → DataAttributes[source]#

Build attributes from the class constants of a Data subclass.

Uses DATA_TYPE_NAME, DATA_FORMAT_VERSION, WRITER_NAME and WRITER_VERSION; constants that are None (unset) are kept as None.

Parameters:

data_class – The Data subclass.

Returns:

The attributes describing the data written by data_class.

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.

classmethod load_yaml(yaml_path: str, loader=<class 'yaml.loader.Loader'>)#
classmethod read(file: File | Group | str | Path) → DataAttributes[source]#

Read the data attributes from the root of an HDF5 file, without raising.

Missing attributes are None, see read_data_attrs().

Parameters:

file – Open h5py.File/h5py.Group or a path to an HDF5 file.

Returns:

The attributes read from the file.

save_yaml(yaml_path: str)#
series: str | None = None#

Name of the measurement series the data belongs to (optional root series attribute).

stamp(file: File | Group | str | Path) → None[source]#

Write these attributes as root attributes of an HDF5 file.

Thin wrapper around stamp_data_attrs(), which is idempotent: an already stored time_created is kept and time_modified is refreshed.

Parameters:

file – Open h5py.File/h5py.Group or a path to an HDF5 file.

time_created: float | None = None#

Unix timestamp from when this data was initially written.

time_modified: float | None = None#

Unix timestamp from when this data was last modified.

writer: str | None = None#

Name of the software module which wrote this data.

writer_version: str | None = None#

Version of the software module which wrote this data.