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

Commit

Permalink
refactor: declare str dunder method in suitable models
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNitroG committed Jan 15, 2025
1 parent d035da9 commit a95fe8a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 45 deletions.
48 changes: 5 additions & 43 deletions src/check_phat_nguoi/context/plates/models/plate_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,60 +42,22 @@ def __eq__(self, other: Any):
return False

# TODO: Handle show details later when main updates that option
@override
def __str__(self) -> str:
def create_violation_str(violation: ViolationDetail, index: int) -> str:
violation_str: str = (
f"Lỗi vi phạm thứ {index}:"
+ (f"\nMàu biển: {violation.color}" if violation.color else "")
+ (f"\nThời điểm vi phạm: {violation.date}" if violation.date else "")
+ (
f"\nVị trí vi phạm: {violation.location}"
if violation.location
else ""
)
+ (
f"\nHành vi vi phạm: {violation.violation}"
if violation.violation
else ""
)
+ (
f"\nTrạng thái: {'Chưa xử phạt' if not violation.status else 'Đã xử phạt'}"
if violation.status is not None
else ""
)
+ (
f"\nĐơn vị phát hiện vi phạm: {violation.enforcement_unit}"
if violation.enforcement_unit
else ""
)
)
resolution_offices: str | None = (
"\n"
+ "Nơi giải quyết vụ việc:"
+ "\n"
+ "\n".join(
resolution_office_detail.strip()
for resolution_office_detail in violation.resolution_offices
)
if violation.resolution_offices
else None
)
return violation_str + (resolution_offices if resolution_offices else "")

plate_detail: str = f"Biển số: {self.plate}" + (
f"\nChủ sở hữu: {self.owner}" if self.owner else ""
)

if self.violations:
return (
plate_detail
+ "\n"
+ "\n".join(
create_violation_str(violation, index)
for index, violation in enumerate(self.violations, start=1)
f"Lỗi vi phạm #{order}:\n" + str(violation)
for order, violation in enumerate(self.violations, start=1)
)
)
return plate_detail
else:
return plate_detail


__all__ = ["PlateDetail"]
32 changes: 30 additions & 2 deletions src/check_phat_nguoi/context/plates/models/violation_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from pydantic import BaseModel, ConfigDict, Field

from check_phat_nguoi.types import VehicleTypeEnum
from check_phat_nguoi.types import VehicleTypeEnum, get_vehicle_str_vie


# NOTE: This class is used to store the get data's reponse. So it has None type in each field
Expand Down Expand Up @@ -51,6 +51,34 @@ class ViolationDetail(BaseModel):
default=None,
)

@override
def __str__(self) -> str:
return (
(f"Biển: {self.plate}" if self.plate else "")
+ (f"\nMàu biển: {self.color}" if self.color else "")
+ (f"\nLoại xe: {get_vehicle_str_vie(self.type)}" if self.type else "")
+ (f"\nThời điểm vi phạm: {self.date}" if self.date else "")
+ (f"\nVị trí vi phạm: {self.location}" if self.location else "")
+ (f"\nHành vi vi phạm: {self.violation}" if self.violation else "")
+ (
f"\nTrạng thái: {'Đã xử phạt' if self.status else 'Chưa xử phạt'}"
if self.status
else ""
)
+ (
f"\nĐơn vị phát hiện vi phạm: {self.enforcement_unit}"
if self.enforcement_unit
else ""
)
+ (
"\n" + "\n".join(self.resolution_offices)
if self.resolution_offices
else ""
)
).strip()

@override
def __hash__(self):
return hash(self.date) + hash(self.location)
return (
hash(self.plate) + hash(self.color) + hash(self.date) + hash(self.location)
)
2 changes: 2 additions & 0 deletions src/check_phat_nguoi/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
VehicleTypeEnum,
get_vehicle_enum,
get_vehicle_str,
get_vehicle_str_vie,
)

__all__ = [
Expand All @@ -20,4 +21,5 @@
"VehicleTypeEnum",
"get_vehicle_enum",
"get_vehicle_str",
"get_vehicle_str_vie",
]
17 changes: 17 additions & 0 deletions src/check_phat_nguoi/types/vehicle_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ def get_vehicle_str(type: VehicleTypeEnum | VehicleType | Any) -> VehicleStrType
raise ValueError("Unknown vehicle type")


def get_vehicle_str_vie(type: VehicleTypeEnum | VehicleType | Any) -> VehicleStrVieType:
match type:
case "car" | "Ô tô" | 1 | VehicleTypeEnum.car:
return "Ô tô"
case "motorbike" | "Xe máy" | 2 | VehicleTypeEnum.motorbike:
return "Xe máy"
case (
"electric_motorbike"
| "Xe máy điện"
| 3
| VehicleTypeEnum.electric_motorbike
):
return "Xe máy điện"
case _:
raise ValueError("Unknown vehicle type")


__all__ = [
"VehicleIntType",
"VehicleStrType",
Expand Down

0 comments on commit a95fe8a

Please sign in to comment.