Machines - Snailz Docs

Skip to content

# Machines

Laboratory machinery.

## `Machine`

Bases: `BaseModel`

A piece of experimental machinery.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`id` | `str` | machine ID | required
`name` | `str` | machine name | required

Source code in `src/snailz/machines.py`

    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    class Machine(BaseModel):
        """A piece of experimental machinery."""

        id: str = Field(description="machine ID")
        name: str = Field(description="machine name")

        @staticmethod
        def generate(num):
            """Generate a list of machines."""
            assert num <= len(PREFIX) * len(SUFFIX), f"cannot generate {num} machine names"
            pairs = [(p, s) for p in PREFIX for s in SUFFIX]
            return [
                Machine(id=f"M{i:02d}", name=f"{p} {s}")
                for i, (p, s) in enumerate(random.sample(pairs, k=num))
            ]

        @staticmethod
        def to_csv(writer, machines):
            """Produce CSV"""
            writer.writerow(["id", "name"])
            writer.writerows([m.id, m.name] for m in machines)

### `generate(num)` `staticmethod`

Generate a list of machines.

Source code in `src/snailz/machines.py`

    51
    52
    53
    54
    55
    56
    57
    58
    59
    @staticmethod
    def generate(num):
        """Generate a list of machines."""
        assert num <= len(PREFIX) * len(SUFFIX), f"cannot generate {num} machine names"
        pairs = [(p, s) for p in PREFIX for s in SUFFIX]
        return [
            Machine(id=f"M{i:02d}", name=f"{p} {s}")
            for i, (p, s) in enumerate(random.sample(pairs, k=num))
        ]

### `to_csv(writer, machines)` `staticmethod`

Produce CSV

Source code in `src/snailz/machines.py`

    61
    62
    63
    64
    65
    @staticmethod
    def to_csv(writer, machines):
        """Produce CSV"""
        writer.writerow(["id", "name"])
        writer.writerows([m.id, m.name] for m in machines)