Rating - Snailz Docs

Skip to content

# Rating

Ratings on machinery.

## `Rating`

Bases: `BaseModel`

A person's rating on a kind of machine.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`person_id` | `str` | person ID | required
`machine_id` | `str` | machine ID | required
`rating` | `int | None` | rating | required

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

    10
    11
    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
    class Rating(BaseModel):
        """A person's rating on a kind of machine."""

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

        person_id: str = Field(description="person ID")
        machine_id: str = Field(description="machine ID")
        rating: int | None = Field(description="rating")

        @staticmethod
        def make(persons, machines):
            """Generate ratings."""

            pairs = [(p, m) for p in persons for m in machines]
            num_ratings = int(RATINGS_FRACTION * len(pairs))
            ratings = [None, 1, 1, 1, 1, 2, 2, 2, 3, 3]
            return [
                Rating(
                    person_id=p.person_id,
                    machine_id=m.machine_id,
                    rating=random.choice(ratings),
                )
                for p, m in random.sample(pairs, k=num_ratings)
            ]

### `make(persons, machines)` `staticmethod`

Generate ratings.

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

    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    @staticmethod
    def make(persons, machines):
        """Generate ratings."""

        pairs = [(p, m) for p in persons for m in machines]
        num_ratings = int(RATINGS_FRACTION * len(pairs))
        ratings = [None, 1, 1, 1, 1, 2, 2, 2, 3, 3]
        return [
            Rating(
                person_id=p.person_id,
                machine_id=m.machine_id,
                rating=random.choice(ratings),
            )
            for p, m in random.sample(pairs, k=num_ratings)
        ]