Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: align formatting and linting with legacy project requirements
  • Loading branch information
Jorgeotero1998 committed Apr 16, 2026
commit 2acb096d629520fbe701940f7b6c74e9ab416c59
4 changes: 4 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[mypy]
python_version = 3.12
ignore_missing_imports = True
check_untyped_defs = False
4 changes: 1 addition & 3 deletions patterns/behavioral/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
during initialization. Uses a single dictionary instead of multiple conditions.
"""


__author__ = "Ibrahim Diop <ibrahim@sikilabs.com>"


class Catalog:
"""catalog of multiple static methods that are executed depending on an init parameter
"""
"""catalog of multiple static methods that are executed depending on an init parameter"""

def __init__(self, param: str) -> None:
# dictionary that will be used to determine which static method is
Expand Down
2 changes: 2 additions & 0 deletions patterns/behavioral/chaining_method.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations
from typing import Self


class Person:
def __init__(self, name: str) -> None:
self.name = name
Expand All @@ -17,6 +18,7 @@ def set_age(self, age: int) -> Self:
def __str__(self) -> str:
return f"Name: {self.name}, Age: {self.age}"


if __name__ == "__main__":
person = Person("Jorge").set_age(28).set_name("Jorge Otero")
print(person)
3 changes: 3 additions & 0 deletions patterns/behavioral/iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

T = TypeVar("T")


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


class WordsCollection(Iterable[T], Generic[T]):
def __init__(self, collection: List[T] = []) -> None:
self._collection = collection
Expand All @@ -26,6 +28,7 @@ def __iter__(self) -> AlphabeticalOrderIterator[T]:
def add_item(self, item: T) -> None:
self._collection.append(item)


if __name__ == "__main__":
collection = WordsCollection[str]()
collection.add_item("First")
Expand Down
5 changes: 4 additions & 1 deletion patterns/behavioral/memento.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
from dataclasses import dataclass
from typing import List


@dataclass(frozen=True)
class Memento:
state: str


class Originator:
def __init__(self, state: str) -> None:
self._state = state
Expand All @@ -21,13 +23,14 @@ def set_state(self, state: str) -> None:
print(f"Originator: Setting state to: {state}")
self._state = state


if __name__ == "__main__":
originator = Originator("Initial State")
caretaker: List[Memento] = []

caretaker.append(originator.save())
originator.set_state("State #1")

caretaker.append(originator.save())
originator.set_state("State #2")

Expand Down
6 changes: 6 additions & 0 deletions patterns/behavioral/observer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations
from typing import List, Protocol


class Observer(Protocol):
def update(self, subject: Subject) -> None: ...


class Subject:
def __init__(self) -> None:
self._observers: List[Observer] = []
Expand All @@ -22,6 +24,7 @@ def notify(self) -> None:
for observer in self._observers:
observer.update(self)


class Data(Subject):
def __init__(self, name: str = "") -> None:
super().__init__()
Expand All @@ -37,14 +40,17 @@ def data(self, value: int) -> None:
self._data = value
self.notify()


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


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


if __name__ == "__main__":
data1 = Data("Data 1")
data1.attach(HexViewer())
Expand Down
5 changes: 5 additions & 0 deletions patterns/behavioral/state.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations
from abc import ABC, abstractmethod


class State(ABC):
@abstractmethod
def scan(self) -> None: ...


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


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


class Radio:
def __init__(self) -> None:
self.am_state = AmState(self)
Expand All @@ -37,6 +41,7 @@ def toggle_am_fm(self) -> None:
def scan(self) -> None:
self.state.scan()


if __name__ == "__main__":
radio = Radio()
actions = [radio.scan] * 2 + [radio.toggle_am_fm] + [radio.scan] * 2
Expand Down
3 changes: 3 additions & 0 deletions patterns/behavioral/template.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations
from abc import ABC, abstractmethod


class AbstractClass(ABC):
def template_method(self) -> None:
self.base_operation1()
Expand All @@ -19,10 +20,12 @@ def required_operations1(self) -> None: ...

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


class ConcreteClass(AbstractClass):
def required_operations1(self) -> None:
print("ConcreteClass: Implemented Operation1")


if __name__ == "__main__":
template = ConcreteClass()
template.template_method()
1 change: 1 addition & 0 deletions patterns/behavioral/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
which is then being used e.g. in tools like `pyflakes`.
- `Black` formatter tool implements it's own: https://github.com/ambv/black/blob/master/black.py#L718
"""

from typing import Union


Expand Down
26 changes: 20 additions & 6 deletions patterns/creational/abstract_factory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations
from typing import Type


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


class Dog:
def speak(self) -> str: return "woof"
def __str__(self) -> str: return "Dog"
def speak(self) -> str:
return "woof"

def __str__(self) -> str:
return "Dog"


class Cat:
def speak(self) -> str: return "meow"
def __str__(self) -> str: return "Cat"
def speak(self) -> str:
return "meow"

def __str__(self) -> str:
return "Cat"


class DogFactory:
@staticmethod
def get_pet() -> Dog: return Dog()
def get_pet() -> Dog:
return Dog()


class CatFactory:
@staticmethod
def get_pet() -> Cat: return Cat()
def get_pet() -> Cat:
return Cat()


if __name__ == "__main__":
shop = PetShop(DogFactory)
Expand Down
12 changes: 10 additions & 2 deletions patterns/creational/builder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations
from typing import Any


class Director:
def __init__(self) -> None:
self.builder: Any = None
Expand All @@ -13,16 +14,22 @@ def construct_building(self) -> None:
def get_building(self) -> Any:
return self.builder.building


class Builder:
def __init__(self) -> None:
self.building: Any = None

def new_building(self) -> None:
self.building = Building()


class BuilderHouse(Builder):
def build_floor(self) -> None: self.building.floor = "One"
def build_size(self) -> None: self.building.size = "Big"
def build_floor(self) -> None:
self.building.floor = "One"

def build_size(self) -> None:
self.building.size = "Big"


class Building:
def __init__(self) -> None:
Expand All @@ -32,6 +39,7 @@ def __init__(self) -> None:
def __repr__(self) -> str:
return f"Floor: {self.floor} | Size: {self.size}"


if __name__ == "__main__":
director = Director()
director.builder = BuilderHouse()
Expand Down
4 changes: 4 additions & 0 deletions patterns/creational/factory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations
from typing import Dict, Type


class GreekGetter:
def __init__(self) -> None:
self.trans: Dict[str, str] = {
Expand All @@ -11,17 +12,20 @@ def __init__(self) -> None:
def get(self, msg: str) -> str:
return self.trans.get(msg, msg)


class EnglishGetter:
def get(self, msg: str) -> str:
return msg


def get_localizer(language: str = "English") -> GreekGetter | EnglishGetter:
languages: Dict[str, Type[GreekGetter | EnglishGetter]] = {
"English": EnglishGetter,
"Greek": GreekGetter,
}
return languages[language]()


if __name__ == "__main__":
for msg in ["dog", "cat", "bird"]:
f = get_localizer("English")
Expand Down
1 change: 1 addition & 0 deletions patterns/creational/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*TL;DR
Stores a set of initialized objects kept ready to use.
"""

from queue import Queue
from types import TracebackType
from typing import Union
Expand Down
7 changes: 5 additions & 2 deletions patterns/creational/prototype.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import copy
from typing import Any, Dict


class Prototype:
def __init__(self) -> None:
self._objects: Dict[str, Any] = {}
Expand All @@ -19,12 +20,14 @@ def clone(self, name: str, **attrs: Any) -> Any:
obj.__dict__.update(attrs)
return obj


class A:
def __str__(self) -> str:
return "I am A"


if __name__ == "__main__":
prototype = Prototype()
prototype.register_object('a', A())
b = prototype.clone('a', name='I am B')
prototype.register_object("a", A())
b = prototype.clone("a", name="I am B")
print(b.name)
12 changes: 10 additions & 2 deletions patterns/structural/adapter.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
from __future__ import annotations
from typing import Any, Callable


class Dog:
def __init__(self) -> None:
self.name = "Dog"
def bark(self) -> str: return "woof!"

def bark(self) -> str:
return "woof!"


class Cat:
def __init__(self) -> None:
self.name = "Cat"
def meow(self) -> str: return "meow!"

def meow(self) -> str:
return "meow!"


class Adapter:
def __init__(self, obj: Any, **adapted_methods: Callable) -> None:
Expand All @@ -19,6 +26,7 @@ def __init__(self, obj: Any, **adapted_methods: Callable) -> None:
def __getattr__(self, attr: str) -> Any:
return getattr(self.obj, attr)


if __name__ == "__main__":
objects = []
dog = Dog()
Expand Down
Loading