Skip to content

Commit 68ea8e4

Browse files
committed
Add Distinction with Python-Patterns
1 parent 7a1f58b commit 68ea8e4

File tree

11 files changed

+147
-0
lines changed

11 files changed

+147
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ so on.
1313
Each pattern has an independent README linked below with a guide to highlight
1414
differences between implementation details.
1515

16+
## Why This repo?
17+
18+
The [python-patterns repo](https://github.com/faif/python-patterns) already
19+
provides a succint and clear way to implement idiomatic patterns in Python.
20+
21+
This repo focuses on showing different ways to implement the same pattern,
22+
including the traditional Java/C/C# like implementation to provide the reader
23+
with a comparison. Moreover metrics are added to each implementation to better
24+
contrast solutions.
25+
1626
## Patterns
1727

1828
### Behavioural

patterns/behavioural/publish_subscribe/problem_01/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .customer import Customer
2+
3+
__all__ = ["Customer"]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from dataclasses import dataclass
2+
from typing import List
3+
4+
from ..store import Store
5+
6+
from ..product import Product
7+
8+
9+
@dataclass
10+
class Customer:
11+
name: str
12+
interests: List[Product]
13+
satisfied: bool = False
14+
15+
def ask_for_interests(self, store: Store) -> None:
16+
for product in self.interests:
17+
if not store.is_available(product):
18+
return
19+
20+
self.satisfied = True
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import time
2+
from .customer import Customer
3+
from .product import Product
4+
from .store import Store
5+
6+
7+
def main() -> None:
8+
cellphone = Product("MobileX", "TechS", 300)
9+
couch = Product("GiantSofa", "AllComfort", 800)
10+
mug = Product("CoffeeM", "IndustrialTea", 20)
11+
12+
customers = [
13+
Customer(name="John", interests=[cellphone]),
14+
Customer(name="Mary", interests=[couch, mug]),
15+
Customer(name="Alicia", interests=[cellphone, mug]),
16+
]
17+
18+
late_customer = Customer(name="bob", interests=[cellphone, couch, mug])
19+
late_customer_arrival = 3
20+
21+
store = Store(name="AllYouNeed")
22+
23+
initial_time = time.time()
24+
25+
products = [cellphone, couch, mug]
26+
estimated_time_arrivals = [1, 2, 4]
27+
28+
notification_period = 1
29+
30+
while True:
31+
current_time = time.time() - initial_time
32+
33+
for product, eta in zip(products, estimated_time_arrivals):
34+
if current_time >= eta and product not in store.producers:
35+
store.add_product(product)
36+
37+
if current_time >= late_customer_arrival and late_customer not in customers:
38+
customers.append(late_customer)
39+
40+
for customer in customers:
41+
customer.ask_for_interests(store)
42+
43+
if all(customer.satisfied for customer in customers):
44+
break
45+
46+
time.sleep(notification_period)
47+
48+
49+
if __name__ == "__main__":
50+
main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .producer import Producer
2+
3+
__all__ = ["Producer"]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from dataclasses import dataclass, field
2+
from typing import List, Set
3+
4+
from ..store import Store
5+
from ..product import Product
6+
7+
8+
@dataclass
9+
class Producer:
10+
name: str
11+
products: Set[Product] = field(default_factory=set)
12+
stores: List[Store] = field(default_factory=list)
13+
14+
def is_available(self, product: Product) -> bool:
15+
if product in self.products:
16+
print(f"The product {product.name} is available at {self.name}")
17+
return True
18+
19+
print(f"The product {product.name} is not available at {self.name}")
20+
return False
21+
22+
def release_product(self, name: str, price: float) -> None:
23+
new_product = Product(name=name, brand=self.name, price=price)
24+
self.products.add(new_product)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .product import Product
2+
3+
__all__ = ["Product"]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import Any, NamedTuple
2+
3+
4+
class Product(NamedTuple):
5+
name: str
6+
brand: str
7+
price: float
8+
9+
def __eq__(self, other: Any):
10+
if not isinstance(other, Product):
11+
return False
12+
return other.name == self.name and other.brand == self.brand
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .store import Store
2+
3+
__all__ = ["Store"]

0 commit comments

Comments
 (0)