|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import random |
| 4 | +from typing import Any |
| 5 | + |
| 6 | + |
| 7 | +class MovieModel: |
| 8 | + _data: list[MovieModel] = [] # 데이터를 저장할 리스트 |
| 9 | + _id_counter = 1 # ID 자동 증가 |
| 10 | + |
| 11 | + def __init__(self, title: str, playtime: int, genre: list[str]) -> None: |
| 12 | + self.id = MovieModel._id_counter |
| 13 | + self.title = title |
| 14 | + self.playtime = playtime |
| 15 | + self.genre = genre |
| 16 | + |
| 17 | + # 데이터를 리스트에 추가 |
| 18 | + MovieModel._data.append(self) |
| 19 | + MovieModel._id_counter += 1 |
| 20 | + |
| 21 | + @classmethod |
| 22 | + def create(cls, title: str, playtime: int, genre: list[str]) -> MovieModel: |
| 23 | + """새로운 영화 추가""" |
| 24 | + return cls(title, playtime, genre) |
| 25 | + |
| 26 | + @classmethod |
| 27 | + def get(cls, **kwargs: Any) -> MovieModel | None: |
| 28 | + """조건에 맞는 단일 영화 반환 (없으면 None)""" |
| 29 | + for movie in cls._data: |
| 30 | + if all(getattr(movie, key) == value for key, value in kwargs.items()): |
| 31 | + return movie |
| 32 | + return None |
| 33 | + |
| 34 | + @classmethod |
| 35 | + def filter(cls, **kwargs: Any) -> list[MovieModel]: |
| 36 | + """조건에 맞는 모든 영화 리스트 반환""" |
| 37 | + return [movie for movie in cls._data if all(getattr(movie, key) == value for key, value in kwargs.items())] |
| 38 | + |
| 39 | + def update(self, **kwargs: Any) -> None: |
| 40 | + """영화 정보 업데이트""" |
| 41 | + for key, value in kwargs.items(): |
| 42 | + if hasattr(self, key): |
| 43 | + if value is not None: |
| 44 | + setattr(self, key, value) |
| 45 | + |
| 46 | + def delete(self) -> None: |
| 47 | + """현재 인스턴스를 _data 리스트에서 삭제""" |
| 48 | + if self in MovieModel._data: |
| 49 | + MovieModel._data.remove(self) |
| 50 | + |
| 51 | + @classmethod |
| 52 | + def all(cls) -> list[MovieModel]: |
| 53 | + """전체 영화 리스트 반환""" |
| 54 | + return cls._data |
| 55 | + |
| 56 | + @classmethod |
| 57 | + def create_dummy(cls) -> None: |
| 58 | + for i in range(1, 11): |
| 59 | + cls.create( |
| 60 | + title=f"dummy_movie {i}", |
| 61 | + playtime=random.randint(100, 300), |
| 62 | + genre=random.choices(["SF", "Romantic", "Adventure", "Action", "Comedy", "Horror"]), |
| 63 | + ) |
| 64 | + |
| 65 | + def __repr__(self) -> str: |
| 66 | + return f"MovieModel(id={self.id}, title='{self.title}', playtime={self.playtime}, genre='{self.genre}')" |
| 67 | + |
| 68 | + def __str__(self) -> str: |
| 69 | + return self.title |
0 commit comments