From 50d6e07d111678080d53a001443fa88c85bd6114 Mon Sep 17 00:00:00 2001 From: Kevin Nitro Date: Wed, 15 Jan 2025 01:27:57 +0700 Subject: [PATCH] chore: hash more to differentiate more pydantic model --- .../config/models/plate_info.py | 31 +++++++++++++++++-- .../context/plates/models/plate_detail.py | 20 ++++++++++-- .../context/plates/models/violation_detail.py | 2 +- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/check_phat_nguoi/config/models/plate_info.py b/src/check_phat_nguoi/config/models/plate_info.py index eef94ee..632cc01 100644 --- a/src/check_phat_nguoi/config/models/plate_info.py +++ b/src/check_phat_nguoi/config/models/plate_info.py @@ -7,6 +7,7 @@ from check_phat_nguoi.types import ( ApiEnum, VehicleType, + get_vehicle_enum, ) @@ -43,13 +44,37 @@ class PlateInfo(BaseModel): ) @override - def __hash__(self): - return hash(self.plate) + def __hash__(self) -> int: + return ( + hash(self.plate) + + hash(self.type) + + hash(self.enabled) + + hash(self.api) + + hash(self.owner) + ) @override def __eq__(self, other: Any): if isinstance(other, PlateInfo): - return self.plate == other.plate + return ( + self.plate == other.plate + and get_vehicle_enum(self.type) == get_vehicle_enum(other.type) + and self.enabled == other.enabled + and self.owner == other.owner + and ( + all( + x == y + for x, y in zip( + (self.api,) if isinstance(self.api, ApiEnum) else self.api, + (other.api,) + if isinstance(other.api, ApiEnum) + else other.api, + ) + ) + if self.api and other.api + else (not self.api and not other.api) + ) + ) return False diff --git a/src/check_phat_nguoi/context/plates/models/plate_detail.py b/src/check_phat_nguoi/context/plates/models/plate_detail.py index ff03bd8..4148d51 100644 --- a/src/check_phat_nguoi/context/plates/models/plate_detail.py +++ b/src/check_phat_nguoi/context/plates/models/plate_detail.py @@ -4,7 +4,7 @@ from pydantic import BaseModel, Field -from check_phat_nguoi.types import VehicleTypeEnum +from check_phat_nguoi.types import VehicleTypeEnum, get_vehicle_enum from .violation_detail import ViolationDetail @@ -19,12 +19,26 @@ class PlateDetail(BaseModel): @override def __hash__(self): - return hash(self.plate) + return ( + hash(self.plate) + + hash(self.owner) + + hash(self.type) + + hash(self.violations) + ) @override def __eq__(self, other: Any): if isinstance(other, PlateDetail): - return self.plate == other.plate + return ( + self.plate == other.plate + and self.owner == other.owner + and get_vehicle_enum(self.type) == get_vehicle_enum(other.type) + and ( + all(x == y for x, y in zip(self.violations, other.violations)) + if self.violations and other.violations + else (not self.violations and not other.violations) + ) + ) return False def __str__(self): diff --git a/src/check_phat_nguoi/context/plates/models/violation_detail.py b/src/check_phat_nguoi/context/plates/models/violation_detail.py index 7d666bd..572b84c 100644 --- a/src/check_phat_nguoi/context/plates/models/violation_detail.py +++ b/src/check_phat_nguoi/context/plates/models/violation_detail.py @@ -39,4 +39,4 @@ class ViolationDetail(BaseModel): @override def __hash__(self): - return hash(f"{self.date}{self.location}") + return hash(self.date) + hash(self.location)