Skip to content

Commit 2acb096

Browse files
fix: align formatting and linting with legacy project requirements
1 parent 1409365 commit 2acb096

26 files changed

+144
-39
lines changed

mypy.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[mypy]
2+
python_version = 3.12
3+
ignore_missing_imports = True
4+
check_untyped_defs = False

patterns/behavioral/catalog.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
during initialization. Uses a single dictionary instead of multiple conditions.
44
"""
55

6-
76
__author__ = "Ibrahim Diop <[email protected]>"
87

98

109
class Catalog:
11-
"""catalog of multiple static methods that are executed depending on an init parameter
12-
"""
10+
"""catalog of multiple static methods that are executed depending on an init parameter"""
1311

1412
def __init__(self, param: str) -> None:
1513
# dictionary that will be used to determine which static method is

patterns/behavioral/chaining_method.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22
from typing import Self
33

4+
45
class Person:
56
def __init__(self, name: str) -> None:
67
self.name = name
@@ -17,6 +18,7 @@ def set_age(self, age: int) -> Self:
1718
def __str__(self) -> str:
1819
return f"Name: {self.name}, Age: {self.age}"
1920

21+
2022
if __name__ == "__main__":
2123
person = Person("Jorge").set_age(28).set_name("Jorge Otero")
2224
print(person)

patterns/behavioral/iterator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
T = TypeVar("T")
55

6+
67
class AlphabeticalOrderIterator(Iterator[T], Generic[T]):
78
def __init__(self, collection: List[T]) -> None:
89
self._collection = collection
@@ -16,6 +17,7 @@ def __next__(self) -> T:
1617
raise StopIteration()
1718
return value
1819

20+
1921
class WordsCollection(Iterable[T], Generic[T]):
2022
def __init__(self, collection: List[T] = []) -> None:
2123
self._collection = collection
@@ -26,6 +28,7 @@ def __iter__(self) -> AlphabeticalOrderIterator[T]:
2628
def add_item(self, item: T) -> None:
2729
self._collection.append(item)
2830

31+
2932
if __name__ == "__main__":
3033
collection = WordsCollection[str]()
3134
collection.add_item("First")

patterns/behavioral/memento.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
from dataclasses import dataclass
33
from typing import List
44

5+
56
@dataclass(frozen=True)
67
class Memento:
78
state: str
89

10+
911
class Originator:
1012
def __init__(self, state: str) -> None:
1113
self._state = state
@@ -21,13 +23,14 @@ def set_state(self, state: str) -> None:
2123
print(f"Originator: Setting state to: {state}")
2224
self._state = state
2325

26+
2427
if __name__ == "__main__":
2528
originator = Originator("Initial State")
2629
caretaker: List[Memento] = []
2730

2831
caretaker.append(originator.save())
2932
originator.set_state("State #1")
30-
33+
3134
caretaker.append(originator.save())
3235
originator.set_state("State #2")
3336

patterns/behavioral/observer.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from __future__ import annotations
22
from typing import List, Protocol
33

4+
45
class Observer(Protocol):
56
def update(self, subject: Subject) -> None: ...
67

8+
79
class Subject:
810
def __init__(self) -> None:
911
self._observers: List[Observer] = []
@@ -22,6 +24,7 @@ def notify(self) -> None:
2224
for observer in self._observers:
2325
observer.update(self)
2426

27+
2528
class Data(Subject):
2629
def __init__(self, name: str = "") -> None:
2730
super().__init__()
@@ -37,14 +40,17 @@ def data(self, value: int) -> None:
3740
self._data = value
3841
self.notify()
3942

43+
4044
class HexViewer:
4145
def update(self, subject: Data) -> None:
4246
print(f"HexViewer: Subject {subject.name} has data 0x{subject.data:x}")
4347

48+
4449
class DecimalViewer:
4550
def update(self, subject: Data) -> None:
4651
print(f"DecimalViewer: Subject {subject.name} has data {subject.data}")
4752

53+
4854
if __name__ == "__main__":
4955
data1 = Data("Data 1")
5056
data1.attach(HexViewer())

patterns/behavioral/state.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from __future__ import annotations
22
from abc import ABC, abstractmethod
33

4+
45
class State(ABC):
56
@abstractmethod
67
def scan(self) -> None: ...
78

9+
810
class AmState(State):
911
def __init__(self, radio: Radio) -> None:
1012
self.radio = radio
@@ -15,6 +17,7 @@ def scan(self) -> None:
1517
self.pos = (self.pos + 1) % len(self.stations)
1618
print(f"Scanning... Station is {self.stations[self.pos]} AM")
1719

20+
1821
class FmState(State):
1922
def __init__(self, radio: Radio) -> None:
2023
self.radio = radio
@@ -25,6 +28,7 @@ def scan(self) -> None:
2528
self.pos = (self.pos + 1) % len(self.stations)
2629
print(f"Scanning... Station is {self.stations[self.pos]} FM")
2730

31+
2832
class Radio:
2933
def __init__(self) -> None:
3034
self.am_state = AmState(self)
@@ -37,6 +41,7 @@ def toggle_am_fm(self) -> None:
3741
def scan(self) -> None:
3842
self.state.scan()
3943

44+
4045
if __name__ == "__main__":
4146
radio = Radio()
4247
actions = [radio.scan] * 2 + [radio.toggle_am_fm] + [radio.scan] * 2

patterns/behavioral/template.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22
from abc import ABC, abstractmethod
33

4+
45
class AbstractClass(ABC):
56
def template_method(self) -> None:
67
self.base_operation1()
@@ -19,10 +20,12 @@ def required_operations1(self) -> None: ...
1920

2021
def hook1(self) -> None: ...
2122

23+
2224
class ConcreteClass(AbstractClass):
2325
def required_operations1(self) -> None:
2426
print("ConcreteClass: Implemented Operation1")
2527

28+
2629
if __name__ == "__main__":
2730
template = ConcreteClass()
2831
template.template_method()

patterns/behavioral/visitor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
which is then being used e.g. in tools like `pyflakes`.
1515
- `Black` formatter tool implements it's own: https://github.com/ambv/black/blob/master/black.py#L718
1616
"""
17+
1718
from typing import Union
1819

1920

patterns/creational/abstract_factory.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22
from typing import Type
33

4+
45
class PetShop:
56
def __init__(self, animal_factory: Type[DogFactory | CatFactory]) -> None:
67
self.pet_factory = animal_factory
@@ -10,21 +11,34 @@ def show_pet(self) -> None:
1011
print(f"We have a lovely {pet}")
1112
print(f"It says {pet.speak()}")
1213

14+
1315
class Dog:
14-
def speak(self) -> str: return "woof"
15-
def __str__(self) -> str: return "Dog"
16+
def speak(self) -> str:
17+
return "woof"
18+
19+
def __str__(self) -> str:
20+
return "Dog"
21+
1622

1723
class Cat:
18-
def speak(self) -> str: return "meow"
19-
def __str__(self) -> str: return "Cat"
24+
def speak(self) -> str:
25+
return "meow"
26+
27+
def __str__(self) -> str:
28+
return "Cat"
29+
2030

2131
class DogFactory:
2232
@staticmethod
23-
def get_pet() -> Dog: return Dog()
33+
def get_pet() -> Dog:
34+
return Dog()
35+
2436

2537
class CatFactory:
2638
@staticmethod
27-
def get_pet() -> Cat: return Cat()
39+
def get_pet() -> Cat:
40+
return Cat()
41+
2842

2943
if __name__ == "__main__":
3044
shop = PetShop(DogFactory)

0 commit comments

Comments
 (0)