Skip to content

device_info

frog.device_info ¤

Provides common dataclasses about devices for using in backend and frontend.

Classes¤

DeviceBaseTypeInfo(name, description, names_short, names_long) dataclass ¤

A generic device type (e.g. stepper motor).

Attributes¤
description instance-attribute ¤

A human-readable name for these types of device.

name instance-attribute ¤

Short name for use in pubsub topics etc.

names_long instance-attribute ¤

A list of names for this type of device (human readable).

names_short instance-attribute ¤

A list of possible names for this type of device.

Functions¤
get_instances_and_descriptions() ¤

Get instances and descriptions.

If there are no possible names for the type, then there will only be one instance, otherwise there will be one per name.

Source code in frog/device_info.py
65
66
67
68
69
70
71
72
73
74
75
76
77
def get_instances_and_descriptions(self) -> Iterable[tuple[DeviceInstanceRef, str]]:
    """Get instances and descriptions.

    If there are no possible names for the type, then there will only be one
    instance, otherwise there will be one per name.
    """
    if not self.names_long:
        yield DeviceInstanceRef(self.name), self.description
        return

    for short, long in zip(self.names_short, self.names_long):
        instance = DeviceInstanceRef(self.name, short)
        yield instance, f"{self.description} ({long})"

DeviceInstanceRef(base_type, name=None) dataclass ¤

Information uniquely describing an instance of a particular device type.

Attributes¤
base_type instance-attribute ¤

The device base type.

name = None class-attribute instance-attribute ¤

The (optional) name of the device.

Used for disambiguating devices where there can be multiple instances.

Functions¤
__str__() ¤

Get the short string representation of this device.

Source code in frog/device_info.py
92
93
94
95
96
97
def __str__(self) -> str:
    """Get the short string representation of this device."""
    s = self.base_type
    if self.name:
        s += f".{self.name}"
    return s
from_str(s) staticmethod ¤

Convert from a string in the format "base_type.name" or "base_type".

Source code in frog/device_info.py
 99
100
101
102
103
@staticmethod
def from_str(s: str) -> DeviceInstanceRef:
    """Convert from a string in the format "base_type.name" or "base_type"."""
    base_type, _, name = s.partition(".")
    return DeviceInstanceRef(base_type, name or None)

DeviceParameter(description, possible_values, default_value=None) dataclass ¤

A parameter that a device needs (e.g. baudrate).

Attributes¤
default_value = None class-attribute instance-attribute ¤

The default value for this parameter.

A value of None indicates that there is no default value.

description instance-attribute ¤

A human-readable description of the parameter.

possible_values instance-attribute ¤

Possible values the parameter can take.

This can either be a Sequence of possible values or a type (e.g. str or float).

Functions¤
__post_init__() ¤

Check that default value is valid.

Source code in frog/device_info.py
26
27
28
29
30
31
32
33
34
35
36
37
def __post_init__(self) -> None:
    """Check that default value is valid."""
    if self.default_value is None:
        return

    if isinstance(self.possible_values, Sequence):
        if self.default_value not in self.possible_values:
            raise RuntimeError(
                f"Default value of {self.default_value} not in possible values"
            )
    elif not isinstance(self.default_value, cast(type, self.possible_values)):
        raise RuntimeError("Default value doesn't match type of possible values")

DeviceTypeInfo(class_name, description, parameters=dict()) dataclass ¤

Description of a device.

Attributes¤
class_name instance-attribute ¤

The name of the device's class including the module name.

description instance-attribute ¤

A human-readable name for the device.

parameters = field(default_factory=dict) class-attribute instance-attribute ¤

The device parameters.