Persons - Snailz Docs

Skip to content

# Persons

Generate random persons.

## `Person`

Bases: `BaseModel`

A single person.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`id` | `str` | unique identifier | required
`family` | `str` | family name | required
`personal` | `str` | personal name | required

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

    12
    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
    class Person(BaseModel):
        """A single person."""

        id: str = Field(description="unique identifier")
        family: str = Field(description="family name")
        personal: str = Field(description="personal name")

        model_config = {"extra": "forbid"}

        _id_generator: ClassVar = generic_id_generator(lambda i: f"P{i:02d}")

        @staticmethod
        def generate(locale, num):
            """Generate random persons.

            Parameters:
                locale (str): Faker locale
                num (int): number of persons to generate

            Returns:
                (list[Person]): generated persons.
            """
            fake = faker.Faker(locale)
            fake.seed_instance(random.randint(0, 1_000_000))
            return [
                Person(
                    id=next(Person._id_generator),
                    family=fake.last_name(),
                    personal=fake.first_name(),
                )
                for _ in range(num)
            ]

        @staticmethod
        def to_csv(writer, persons):
            """Convert to CSV.

            Parameters:
                writer (stream): where to write
                persons (list[Person]): what to write
            """
            writer.writerow(["id", "family", "personal"])
            writer.writerows([p.id, p.family, p.personal] for p in persons)

### `generate(locale, num)` `staticmethod`

Generate random persons.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`locale` | `str` | Faker locale | required
`num` | `int` | number of persons to generate | required

Returns:

Type | Description
--- | ---
`list[Person]` | generated persons.

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

    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    @staticmethod
    def generate(locale, num):
        """Generate random persons.

        Parameters:
            locale (str): Faker locale
            num (int): number of persons to generate

        Returns:
            (list[Person]): generated persons.
        """
        fake = faker.Faker(locale)
        fake.seed_instance(random.randint(0, 1_000_000))
        return [
            Person(
                id=next(Person._id_generator),
                family=fake.last_name(),
                personal=fake.first_name(),
            )
            for _ in range(num)
        ]

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

Convert to CSV.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`writer` | `stream` | where to write | required
`persons` | `list[Person]` | what to write | required

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

    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    @staticmethod
    def to_csv(writer, persons):
        """Convert to CSV.

        Parameters:
            writer (stream): where to write
            persons (list[Person]): what to write
        """
        writer.writerow(["id", "family", "personal"])
        writer.writerows([p.id, p.family, p.personal] for p in persons)