Skip to content

Commit 319cff8

Browse files
authored
Merge pull request #1 from OZ-Coding-School/day2
Day2
2 parents 4bdcbf9 + afcc3f3 commit 319cff8

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed

.github/workflows/ci.yml

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
static-analysis: # mypy, black, ruff 등 정적 분석만 실행
10+
runs-on: ubuntu-22.04 # 실제 프로덕션 환경에서는 lastest 대신 버젼을 고정하는 것이 좋다. 업데이트 사항에 따라 예상치 못한 에러가 발생할 수 있기 때문.
11+
steps:
12+
- name: Check out the codes
13+
uses: actions/checkout@v2 # 가장 최신의 커밋을 가져온다.
14+
15+
- name: Setup python environment
16+
id: setup-python
17+
uses: actions/setup-python@v2
18+
with:
19+
python-version: "3.13" # 파이썬 버젼 역시 개발환경에 맞는 버젼으로 고정으로 사용한다.
20+
21+
- name: Cache Poetry
22+
id: cache-poetry
23+
uses: actions/cache@v3
24+
with:
25+
key: poetry-2.0.1
26+
path: ~/.local/ # poetry 의 실제 설치 경로를 통째로 캐시함.
27+
28+
- name: Install Poetry
29+
if: steps.cache-poetry.outputs.cache-hit != 'true'
30+
run: | # 개행을 하여 여러줄로 명령어를 사용하고 싶을때 파이프라인 기호 | 를 사용한다.
31+
curl -sSL https://install.python-poetry.org | python3 - --version 2.0.1
32+
33+
- name: Register Poetry bin # 포에트리 환경변수를 깃허브 패스(.zshrc 같은 쉘 설정파일)에 기입하여 포에트리 명령어를 사용할 수 있게 한다.
34+
run: echo "${HOME}/.poetry/bin" >> $GITHUB_PATH
35+
36+
- name: Cache dependencies
37+
id: cache-venv
38+
uses: actions/cache@v3
39+
with:
40+
# hashFiles를 사용하여 파일의 해시값을 비교하여 파이썬 버전이 바뀌었을 때, lock 파일이나 toml 파일이 바뀌었을때 종속성을 재설치합니다.
41+
# 바뀐 것이 없는 경우에는 캐싱된 종속성을 사용합니다.
42+
key: python-${{ steps.setup-python.outputs.python-version }}-poetry-lock-${{ hashFiles('poetry.lock') }}-toml-${{ hashFiles('pyproject.toml') }}-poetry-1.8.5
43+
path: /home/runner/.cache/pypoetry/virtualenvs/
44+
45+
- name: Install dependencies # pytest 를 dev 종속성으로 설치하였기 때문에 only-main 옵션은 사용하지 않는다.
46+
if: steps.cache-venv.outputs.cache-hit != 'true'
47+
run: poetry install --no-root
48+
49+
- name: Run Black
50+
run: poetry run black . --check # check 옵션을 걸게되면 리포매팅을 하지 않는다.
51+
52+
- name: Run Ruff
53+
run: | # Isort 를 먼저 체크하고 기본 lint 규칙을 지켰는지 검사한다.
54+
poetry run ruff check --select I
55+
poetry run ruff check
56+
57+
- name: Run Mypy # mypy 타입 정적 검사를 실행한다.
58+
run: poetry run mypy .
59+
60+
test: # mypy와 직렬로 실행하게되면 시간이 너무 오래 걸려서 test와 정적 타입분석은 병렬로 실행
61+
runs-on: ubuntu-22.04
62+
63+
steps:
64+
- name: Check out the codes
65+
uses: actions/checkout@v2
66+
67+
- name: Setup python environment
68+
id: setup-python
69+
uses: actions/setup-python@v2
70+
with:
71+
python-version: "3.12"
72+
73+
- name: Cache Poetry
74+
id: cache-poetry
75+
uses: actions/cache@v3
76+
with:
77+
key: poetry-2.0.1
78+
path: ~/.local/ # poetry 의 실제 설치 경로를 통째로 캐시함.
79+
80+
- name: Install Poetry
81+
if: steps.cache-poetry.outputs.cache-hit != 'true'
82+
run: |
83+
curl -sSL https://install.python-poetry.org | python3 - --version 2.0.1
84+
85+
- name: Register Poetry bin
86+
run: echo "${HOME}/.poetry/bin" >> $GITHUB_PATH
87+
88+
- name: Cache dependencies
89+
id: cache-venv
90+
uses: actions/cache@v3
91+
with:
92+
key: python-${{ steps.setup-python.outputs.python-version }}-poetry-lock-${{ hashFiles('poetry.lock') }}-toml-${{ hashFiles('pyproject.toml') }}-poetry-1.8.5
93+
path: /home/runner/.cache/pypoetry/virtualenvs/
94+
95+
- name: Install dependencies
96+
if: steps.cache-venv.outputs.cache-hit != 'true'
97+
run: poetry install --no-root
98+
99+
- name: Run tests # 커버리지로 pytest를 실행하고 결과 report를 출력한다.
100+
run: |
101+
poetry run coverage run -m pytest .
102+
poetry run coverage report -m

src/models/movies.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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

Comments
 (0)