1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| class User: def __init__(self, code, username, email, date): self.code = code self.username = username self.email = email self.date = date
def __repr__(self): return f"User(code={self.code}, username={self.username}, email={self.email}, date={self.date})"
def __eq__(self, other): return self.code == other.code and self.username == other.username and self.email == other.email and self.date == other.date
def __hash__(self): return hash((self.code, self.username, self.email, self.date))
def to_dict(self): return {"code": self.code, "username": self.username, "email": self.email, "date": self.date}
@staticmethod def from_dict(obj): assert isinstance(obj, dict) code = obj.get("code") username = obj.get("username") email = obj.get("email") date = obj.get("date") return User(code, username, email, date)
def to_json(self): return json.dumps(self.to_dict(), indent=2)
@staticmethod def from_json(json_str): return User.from_dict(json.loads(json_str))
|