Person - Snailz Docs

Skip to content

# Person

Represent scientific staff.

## `Person`

Bases: `BaseModel`

A single person.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`person_id` | `str` | unique identifier | required
`family` | `str` | family name | required
`personal` | `str` | personal name | required
`supervisor_id` | `str | None` | supervisor identifier | `None`

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

    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    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
    class Person(BaseModel):
        """A single person."""

        id_stem: ClassVar[str] = "P"
        id_digits: ClassVar[int] = 4

        model_config = ConfigDict(
            json_schema_extra={
                "foreign_key": {
                    "supervisor_id": ("person", "person_id"),
                }
            }
        )

        person_id: str = Field(
            min_length=1,
            description="unique identifier",
            json_schema_extra={"primary_key": True},
        )
        family: str = Field(min_length=1, description="family name")
        personal: str = Field(min_length=1, description="personal name")
        supervisor_id: str | None = Field(default=None, description="supervisor identifier")

        @staticmethod
        def make(params):
            """Make persons."""

            utils.ensure_id_generator(Person)
            if not hasattr(Person, "_fake"):
                Person._fake = faker.Faker(params.locale)
                Person._fake.seed_instance(random.randint(0, 1_000_000))

            staff = [
                Person(
                    person_id=next(Person._id_gen),
                    family=Person._fake.last_name(),
                    personal=Person._fake.first_name(),
                )
                for _ in range(params.num_persons)
            ]

            num_supervisors = max(1, params.num_persons // SUPERVISOR_RATIO)
            supervisors = [
                Person(
                    person_id=next(Person._id_gen),
                    family=Person._fake.last_name(),
                    personal=Person._fake.first_name(),
                )
                for _ in range(num_supervisors)
            ]

            for person in staff:
                person.supervisor_id = random.choice(supervisors).person_id

            if num_supervisors > 1:
                for person in supervisors[:-1]:
                    person.supervisor_id = supervisors[-1].person_id

            return staff + supervisors

        @staticmethod
        def csv_header():
            """Generate header for CSV file."""

            return "person_id,family,personal"

        def __str__(self):
            """Convert to CSV string."""

            return f"{self.person_id},{self.family},{self.personal}"

### `make(params)` `staticmethod`

Make persons.

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

    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    @staticmethod
    def make(params):
        """Make persons."""

        utils.ensure_id_generator(Person)
        if not hasattr(Person, "_fake"):
            Person._fake = faker.Faker(params.locale)
            Person._fake.seed_instance(random.randint(0, 1_000_000))

        staff = [
            Person(
                person_id=next(Person._id_gen),
                family=Person._fake.last_name(),
                personal=Person._fake.first_name(),
            )
            for _ in range(params.num_persons)
        ]

        num_supervisors = max(1, params.num_persons // SUPERVISOR_RATIO)
        supervisors = [
            Person(
                person_id=next(Person._id_gen),
                family=Person._fake.last_name(),
                personal=Person._fake.first_name(),
            )
            for _ in range(num_supervisors)
        ]

        for person in staff:
            person.supervisor_id = random.choice(supervisors).person_id

        if num_supervisors > 1:
            for person in supervisors[:-1]:
                person.supervisor_id = supervisors[-1].person_id

        return staff + supervisors

### `csv_header()` `staticmethod`

Generate header for CSV file.

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

    74
    75
    76
    77
    78
    @staticmethod
    def csv_header():
        """Generate header for CSV file."""

        return "person_id,family,personal"

### `__str__()`

Convert to CSV string.

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

    80
    81
    82
    83
    def __str__(self):
        """Convert to CSV string."""

        return f"{self.person_id},{self.family},{self.personal}"