Skip to content

Commit 0998850

Browse files
authored
HW 06 (#4)
* HW 06 * HW 06: update toml project * HW 06: apply black again
1 parent 23b9f79 commit 0998850

26 files changed

+1431
-1
lines changed

.flake8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
[flake8]
22
extend-ignore = E203,E501
3+
exclude =
4+
tests

.github/workflows/main.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,33 @@ jobs:
9191
- name: Run pytest
9292
run: |
9393
cd homework_05
94+
poetry run pytest tests
95+
96+
hw_06:
97+
runs-on: ubuntu-latest
98+
steps:
99+
- uses: actions/checkout@v2
100+
101+
- uses: actions/setup-python@v2
102+
with:
103+
python-version: 3.12
104+
105+
- name: Run image # install poetry
106+
uses: abatilo/[email protected]
107+
with:
108+
poetry-version: 1.8.2
109+
110+
- name: Install dependencies # install all dependencies
111+
run: |
112+
cd homework_06
113+
poetry install --no-root
114+
115+
- name: Run static analyzis
116+
run: |
117+
cd homework_06
118+
poetry run pre-commit run --files *
119+
120+
- name: Run pytest
121+
run: |
122+
cd homework_06
94123
poetry run pytest tests

.pre-commit-config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ repos:
3838
- id: mypy
3939
exclude: 'tests'
4040

41+
4142
# run local pylint in venv
4243
- repo: local
4344
hooks:
@@ -51,7 +52,7 @@ repos:
5152
- --max-line-length=120
5253
- --ignore-imports=yes
5354
- -d duplicate-code
54-
- -d C0111,W0621,R0913,R1705,W0201,W0613
55+
- -d C0111,W0621,R0913,R1705,W0201,R0903,W0613
5556

5657

5758
- repo: https://github.com/asottile/pyupgrade

homework_06/domain/__init__.py

Whitespace-only changes.

homework_06/domain/exceptions.py

Whitespace-only changes.

homework_06/domain/models.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from dataclasses import dataclass, field
2+
from typing import List
3+
4+
5+
@dataclass
6+
class Product:
7+
id: int
8+
name: str
9+
quantity: int
10+
price: float
11+
12+
13+
@dataclass
14+
class Order:
15+
id: int
16+
products: List[Product] = field(default_factory=list)
17+
18+
def add_product(self, product: Product):
19+
self.products.append(product)

homework_06/domain/repositories.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from abc import ABC, abstractmethod
2+
from typing import List
3+
4+
from .models import Order, Product
5+
6+
7+
class ProductRepository(ABC):
8+
@abstractmethod
9+
def add(self, product: Product):
10+
pass
11+
12+
@abstractmethod
13+
def get(self, product_id: int) -> Product:
14+
pass
15+
16+
@abstractmethod
17+
def list(self) -> List[Product]:
18+
pass
19+
20+
21+
class OrderRepository(ABC):
22+
@abstractmethod
23+
def add(self, order: Order):
24+
pass
25+
26+
@abstractmethod
27+
def get(self, order_id: int) -> Order:
28+
pass
29+
30+
@abstractmethod
31+
def list(self) -> List[Order]:
32+
pass

homework_06/domain/services.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import List
2+
3+
from .models import Order, Product
4+
from .repositories import OrderRepository, ProductRepository
5+
6+
7+
def generate_next_id():
8+
num = 1
9+
while True:
10+
yield num
11+
num += 1
12+
13+
14+
class WarehouseService:
15+
def __init__(self, product_repo: ProductRepository, order_repo: OrderRepository):
16+
self.product_repo = product_repo
17+
self.order_repo = order_repo
18+
19+
def create_product(self, name: str, quantity: int, price: float) -> Product:
20+
product = Product(id=generate_next_id(), name=name, quantity=quantity, price=price)
21+
self.product_repo.add(product)
22+
return product
23+
24+
def create_order(self, products: List[Product]) -> Order:
25+
order = Order(id=generate_next_id(), products=products)
26+
self.order_repo.add(order)
27+
return order

homework_06/domain/unit_of_work.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class UnitOfWork(ABC):
5+
@abstractmethod
6+
def __enter__(self):
7+
pass
8+
9+
@abstractmethod
10+
def __exit__(self, exc_type, exc_val, exc_tb):
11+
pass
12+
13+
@abstractmethod
14+
def commit(self):
15+
pass
16+
17+
@abstractmethod
18+
def rollback(self):
19+
pass

homework_06/infrastructure/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)