refactor asserts

This commit is contained in:
2025-03-09 07:39:29 -05:00
parent 88b533c27b
commit 9fdf79842d
12 changed files with 84 additions and 64 deletions

View File

@@ -1,5 +1,5 @@
from typing import ClassVar
from pydantic import BaseModel, model_validator
from src.models import KeypadSize
from src.utils import generate_random_nonrepeating_list
@@ -8,20 +8,21 @@ class CustomerAttributes(BaseModel):
attr_vals: list[int]
set_vals: list[int]
keypad_size: KeypadSize
MAX_KEYS: ClassVar[int] = 256
MAX_ATTRS_PER_KEY: ClassVar[int] = 256
@model_validator(mode='after')
def check_keys_vs_attrs(self):
def check_keys_vs_attrs(self) -> 'CustomerAttributes':
if self.keypad_size.is_dispersable:
raise ValueError("number of keys must be less than the number of "
"attributes per key to be dispersion resistant")
return self
@staticmethod
def new(keypad_size: KeypadSize):
assert (keypad_size.numb_of_keys <= 256)
assert (keypad_size.attrs_per_key <= 256)
return CustomerAttributes(
@classmethod
def create(cls, keypad_size: KeypadSize) -> 'CustomerAttributes':
if keypad_size.numb_of_keys > cls.MAX_KEYS or keypad_size.attrs_per_key > cls.MAX_ATTRS_PER_KEY:
raise ValueError(f"Keys and attributes per key must not exceed {cls.MAX_KEYS}")
return cls(
attr_vals=generate_random_nonrepeating_list(keypad_size.numb_of_attrs),
set_vals=generate_random_nonrepeating_list(keypad_size.attrs_per_key),
keypad_size=keypad_size,
@@ -38,5 +39,6 @@ class CustomerAttributes(BaseModel):
return self.set_vals[set_idx]
def get_set_index(self, set_val: int) -> int:
assert (set_val in self.set_vals)
if set_val not in self.set_vals:
raise ValueError(f"Set value {set_val} not found in set values")
return self.set_vals.index(set_val)