Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
chore: hash more to differentiate more pydantic model
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNitroG committed Jan 14, 2025
1 parent 5c7db8d commit 50d6e07
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
31 changes: 28 additions & 3 deletions src/check_phat_nguoi/config/models/plate_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from check_phat_nguoi.types import (
ApiEnum,
VehicleType,
get_vehicle_enum,
)


Expand Down Expand Up @@ -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


Expand Down
20 changes: 17 additions & 3 deletions src/check_phat_nguoi/context/plates/models/plate_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 50d6e07

Please sign in to comment.