Skip to content

Machine

Laboratory machinery.

Machine dataclass

Bases: BaseMixin

A piece of experimental machinery.

Attributes:

Name Type Description
ident str

unique identifier

name str

machine name

Source code in src/snailz/machine.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
@dataclass
class Machine(BaseMixin):
    """
    A piece of experimental machinery.

    Attributes:
        ident: unique identifier
        name: machine name
    """

    primary_key: ClassVar[str] = "ident"
    table_name: ClassVar[str] = "machine"
    _next_id: IdGeneratorType = id_generator("M", 4)

    ident: str = ""
    name: str = ""

    def __post_init__(self):
        """Validate fields and generate unique identifier."""

        validate(self.ident == "", "machine ID cannot be set externally")
        validate(len(self.name) > 0, "name cannot be empty")

        self.ident = next(self._next_id)

    @classmethod
    def make(cls, params: Parameters) -> list[Self]:
        """
        Construct multiple machines.

        Returns:
            List of machines.

        Args:
            params: Parameters object.
        """

        assert params.num_machines <= len(PREFIX) * len(SUFFIX), (
            f"cannot generate {params.num_machines} machine names"
        )
        pairs = [(p, s) for p in PREFIX for s in SUFFIX]
        return [
            Machine(name=f"{p} {s}")
            for (p, s) in random.sample(pairs, k=params.num_machines)
        ]

__post_init__()

Validate fields and generate unique identifier.

Source code in src/snailz/machine.py
67
68
69
70
71
72
73
def __post_init__(self):
    """Validate fields and generate unique identifier."""

    validate(self.ident == "", "machine ID cannot be set externally")
    validate(len(self.name) > 0, "name cannot be empty")

    self.ident = next(self._next_id)

make(params) classmethod

Construct multiple machines.

Returns:

Type Description
list[Self]

List of machines.

Parameters:

Name Type Description Default
params Parameters

Parameters object.

required
Source code in src/snailz/machine.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
@classmethod
def make(cls, params: Parameters) -> list[Self]:
    """
    Construct multiple machines.

    Returns:
        List of machines.

    Args:
        params: Parameters object.
    """

    assert params.num_machines <= len(PREFIX) * len(SUFFIX), (
        f"cannot generate {params.num_machines} machine names"
    )
    pairs = [(p, s) for p in PREFIX for s in SUFFIX]
    return [
        Machine(name=f"{p} {s}")
        for (p, s) in random.sample(pairs, k=params.num_machines)
    ]