Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 8 additions & 5 deletions patterns/behavioral/chaining_method.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
from __future__ import annotations


class Person:
def __init__(self, name, action):
def __init__(self, name: str, action: Action) -> None:
self.name = name
self.action = action

def do_action(self):
def do_action(self) -> Action:
print(self.name, self.action.name, end=" ")
return self.action


class Action:
def __init__(self, name):
def __init__(self, name: str) -> None:
self.name = name

def amount(self, val):
def amount(self, val: str) -> Action:
print(val, end=" ")
return self

def stop(self):
def stop(self) -> None:
print("then stop")


Expand Down
7 changes: 4 additions & 3 deletions patterns/behavioral/iterator_alt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*TL;DR
Traverses a container and accesses the container's elements.
"""
from __future__ import annotations


class NumberWords:
Expand All @@ -17,14 +18,14 @@ class NumberWords:
"five",
)

def __init__(self, start, stop):
def __init__(self, start: int, stop: int) -> None:
self.start = start
self.stop = stop

def __iter__(self): # this makes the class an Iterable
def __iter__(self) -> NumberWords: # this makes the class an Iterable
return self

def __next__(self): # this makes the class an Iterator
def __next__(self) -> str: # this makes the class an Iterator
if self.start > self.stop or self.start > len(self._WORD_MAP):
raise StopIteration
current = self.start
Expand Down
6 changes: 3 additions & 3 deletions patterns/behavioral/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from __future__ import annotations

from contextlib import suppress
from typing import List, Optional, Protocol
from typing import Protocol


# define a generic observer type
Expand All @@ -23,7 +23,7 @@ def update(self, subject: Subject) -> None:

class Subject:
def __init__(self) -> None:
self._observers: List[Observer] = []
self._observers: list[Observer] = []

def attach(self, observer: Observer) -> None:
if observer not in self._observers:
Expand All @@ -33,7 +33,7 @@ def detach(self, observer: Observer) -> None:
with suppress(ValueError):
self._observers.remove(observer)

def notify(self, modifier: Optional[Observer] = None) -> None:
def notify(self, modifier: Observer | None = None) -> None:
for observer in self._observers:
if modifier != observer:
observer.update(self)
Expand Down
2 changes: 1 addition & 1 deletion patterns/behavioral/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def scan(self):
self.pos += 1
if self.pos == len(self.stations):
self.pos = 0
print("Scanning... Station is {} {}".format(self.stations[self.pos], self.name))
print(f"Scanning... Station is {self.stations[self.pos]} {self.name}")


class AmState(State):
Expand Down
4 changes: 2 additions & 2 deletions patterns/behavioral/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from __future__ import annotations

from typing import Callable, Type
from typing import Callable


class DiscountStrategyValidator: # Descriptor class for check perform
Expand All @@ -36,7 +36,7 @@ def __set__(self, obj: Order, value: Callable = None) -> None:
else:
setattr(obj, self.private_name, None)

def __get__(self, obj: object, objtype: Type = None):
def __get__(self, obj: object, objtype: type = None):
return getattr(obj, self.private_name)


Expand Down
7 changes: 4 additions & 3 deletions patterns/creational/prototype.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@
*TL;DR
Creates new object instances by cloning prototype.
"""
from __future__ import annotations

from typing import Any, Dict
from typing import Any


class Prototype:
def __init__(self, value: str = "default", **attrs: Any) -> None:
self.value = value
self.__dict__.update(attrs)

def clone(self, **attrs: Any) -> "Prototype":
def clone(self, **attrs: Any) -> Prototype:
"""Clone a prototype and update inner attributes dictionary"""
# Python in Practice, Mark Summerfield
# copy.deepcopy can be used instead of next line.
Expand All @@ -42,7 +43,7 @@ class PrototypeDispatcher:
def __init__(self):
self._objects = {}

def get_objects(self) -> Dict[str, Prototype]:
def get_objects(self) -> dict[str, Prototype]:
"""Get all objects"""
return self._objects

Expand Down
4 changes: 2 additions & 2 deletions patterns/fundamental/delegation_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from __future__ import annotations

from typing import Any, Callable, Union
from typing import Any, Callable


class Delegator:
Expand All @@ -31,7 +31,7 @@ class Delegator:
def __init__(self, delegate: Delegate):
self.delegate = delegate

def __getattr__(self, name: str) -> Union[Any, Callable]:
def __getattr__(self, name: str) -> Any | Callable:
attr = getattr(self.delegate, name)

if not callable(attr):
Expand Down