Skip to content

Add typed models for specific lookup tables #133

@olliesilvester

Description

@olliesilvester

In #129, we added a GenericLut model which is a data structure which will work for any lookup table. This is good because it's generic, but it would be quite clunky to actually use when converting values. We'd need to do quite a lot of fiddling around with lists/indices - and there isn't type safety which makes sure you're referring to a valid column name.

Ideally the lookup table would have a function like

convert_values(starting_unit: UnitName, starting_value: Num, desired_unit: UnitName) -> Unit:

@jacob720 came up with

COLUMN_NAMES = Literal["angle_deg", "gap_mm"]


class SpecificLut(BaseModel):
    rows: list[list[int | float]]

    @model_validator(mode="after")
    def make_column_names(self):
        self._column_names: list[COLUMN_NAMES] = list(get_args(COLUMN_NAMES))
        return self

    def get(
        self,
        column_name: COLUMN_NAMES,
        value: int | float,
        target_column_name: COLUMN_NAMES,
    ):
        column_index = self._column_names.index(column_name)
        target_column_index = self._column_names.index(target_column_name)
        for row in enumerate(self.rows):
            if row[column_index] == value:
                return row[target_column_index]
        raise ValueError(f"Value not found in table for column {column_name}")


my_lut = SpecificLut(rows=[[]])
my_lut.get(column_name="not_accepted", value=5, target_column_name="gap_mm")

Which looks good to me

Acceptance Criteria

  • Something like the above is implemented

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions