You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.4 KiB

from datetime import date
from typing import Optional, Union
from dataclasses import dataclass
from src.utils.id_card_utils import parse_id_card
@dataclass
class Student:
name: str
id_card: str
stu_id: str
gender: Optional[bool] = None # True for male, False for female
height: Optional[int] = None # cm
weight: Optional[float] = None # kg
enrollment_date: Optional[Union[date, str]] = None
class_name: Optional[str] = None
major: Optional[str] = None
def __post_init__(self):
# 从身份证号解析生日和年龄
self.birthday = parse_id_card.get_birthday(self.id_card)
self.age = parse_id_card.get_age(self.id_card)
# 确保入学日期是date对象
if isinstance(self.enrollment_date, str):
try:
self.enrollment_date = date.fromisoformat(self.enrollment_date)
except ValueError:
raise ValueError("Invalid enrollment date format. Use YYYY-MM-DD")
def to_dict(self):
return {
"name": self.name,
"id_card": self.id_card,
"stu_id": self.stu_id,
"gender": self.gender,
"height": self.height,
"weight": self.weight,
"enrollment_date": self.enrollment_date.isoformat() if self.enrollment_date else None,
"class_name": self.class_name,
"major": self.major
}