Person - Snailz Docs

Skip to content

# Person

Staff.

## `Person` `dataclass`

Bases: `BaseMixin`

A single person.

Attributes:

Name | Type | Description
--- | --- | ---
`ident` | `str` | unique identifier
`family` | `str` | family name
`personal` | `str` | personal name
`supervisor_id` | `str | None` | ident of supervisor (if any)

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

    13
    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
    84
    85
    86
    @dataclass
    class Person(BaseMixin):
        """
        A single person.

        Attributes:
            ident: unique identifier
            family: family name
            personal: personal name
            supervisor_id: ident of supervisor (if any)
        """

        primary_key: ClassVar[str] = "ident"
        foreign_keys: ForeignKeysType = [("supervisor_id", "person", "ident")]
        nullable_keys: ClassVar[set[str]] = {"supervisor_id"}
        table_name: ClassVar[str] = "person"
        _next_id: IdGeneratorType = id_generator("P", 4)

        ident: str = ""
        family: str = ""
        personal: str = ""
        supervisor_id: str | None = None

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

            Raises:
                ValueError: If validation fails.
            """

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

            self.ident = next(self._next_id)

        @classmethod
        def make(cls, params: Parameters, fake: Faker) -> list[Self]:
            """
            Construct multiple persons, some of whom have other persons
            as supervisors.

            Args:
                params: Parameters object.
                fake: Name generator.

            Returns:
                List of persons.
            """

            num_supervisors = max(1, int(params.supervisor_frac * params.num_persons))
            num_staff = params.num_persons - num_supervisors

            staff = [
                cls(
                    family=fake.last_name(),
                    personal=fake.first_name(),
                )
                for _ in range(num_staff)
            ]

            supervisors = [
                cls(
                    family=fake.last_name(),
                    personal=fake.first_name(),
                )
                for _ in range(num_supervisors)
            ]

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

            return staff + supervisors

### `__post_init__()`

Validate fields and generate unique identifier.

Raises:

Type | Description
--- | ---
`ValueError` | If validation fails.

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

    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    def __post_init__(self):
        """
        Validate fields and generate unique identifier.

        Raises:
            ValueError: If validation fails.
        """

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

        self.ident = next(self._next_id)

### `make(params, fake)` `classmethod`

Construct multiple persons, some of whom have other persons as supervisors.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`params` | `Parameters` | Parameters object. | required
`fake` | `Faker` | Name generator. | required

Returns:

Type | Description
--- | ---
`list[Self]` | List of persons.

Source code in `src/snailz/person.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
    @classmethod
    def make(cls, params: Parameters, fake: Faker) -> list[Self]:
        """
        Construct multiple persons, some of whom have other persons
        as supervisors.

        Args:
            params: Parameters object.
            fake: Name generator.

        Returns:
            List of persons.
        """

        num_supervisors = max(1, int(params.supervisor_frac * params.num_persons))
        num_staff = params.num_persons - num_supervisors

        staff = [
            cls(
                family=fake.last_name(),
                personal=fake.first_name(),
            )
            for _ in range(num_staff)
        ]

        supervisors = [
            cls(
                family=fake.last_name(),
                personal=fake.first_name(),
            )
            for _ in range(num_supervisors)
        ]

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

        return staff + supervisors