Skip to content

Commit

Permalink
issue 373: add more type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkahan committed Jul 27, 2022
1 parent be59b53 commit 9d4170d
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 65 deletions.
25 changes: 14 additions & 11 deletions patterns/behavioral/publish_subscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,50 @@
"""


from __future__ import annotations


class Provider:
def __init__(self):
def __init__(self) -> None:
self.msg_queue = []
self.subscribers = {}

def notify(self, msg):
def notify(self, msg: str) -> None:
self.msg_queue.append(msg)

def subscribe(self, msg, subscriber):
def subscribe(self, msg: str, subscriber: Subscriber) -> None:
self.subscribers.setdefault(msg, []).append(subscriber)

def unsubscribe(self, msg, subscriber):
def unsubscribe(self, msg: str, subscriber: Subscriber) -> None:
self.subscribers[msg].remove(subscriber)

def update(self):
def update(self) -> None:
for msg in self.msg_queue:
for sub in self.subscribers.get(msg, []):
sub.run(msg)
self.msg_queue = []


class Publisher:
def __init__(self, msg_center):
def __init__(self, msg_center: Provider) -> None:
self.provider = msg_center

def publish(self, msg):
def publish(self, msg: str) -> None:
self.provider.notify(msg)


class Subscriber:
def __init__(self, name, msg_center):
def __init__(self, name: str, msg_center: Provider) -> None:
self.name = name
self.provider = msg_center

def subscribe(self, msg):
def subscribe(self, msg: str) -> None:
self.provider.subscribe(msg, self)

def unsubscribe(self, msg):
def unsubscribe(self, msg: str) -> None:
self.provider.unsubscribe(msg, self)

def run(self, msg):
def run(self, msg: str) -> None:
print(f"{self.name} got {msg}")


Expand Down
20 changes: 10 additions & 10 deletions patterns/behavioral/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
Implements state transitions by invoking methods from the pattern's superclass.
"""

from __future__ import annotations

class State:

class State:
"""Base state. This is to share functionality"""

def scan(self):
def scan(self) -> None:
"""Scan the dial to the next station"""
self.pos += 1
if self.pos == len(self.stations):
Expand All @@ -22,43 +23,42 @@ def scan(self):


class AmState(State):
def __init__(self, radio):
def __init__(self, radio: Radio) -> None:
self.radio = radio
self.stations = ["1250", "1380", "1510"]
self.pos = 0
self.name = "AM"

def toggle_amfm(self):
def toggle_amfm(self) -> None:
print("Switching to FM")
self.radio.state = self.radio.fmstate


class FmState(State):
def __init__(self, radio):
def __init__(self, radio: Radio) -> None:
self.radio = radio
self.stations = ["81.3", "89.1", "103.9"]
self.pos = 0
self.name = "FM"

def toggle_amfm(self):
def toggle_amfm(self) -> None:
print("Switching to AM")
self.radio.state = self.radio.amstate


class Radio:

"""A radio. It has a scan button, and an AM/FM toggle switch."""

def __init__(self):
def __init__(self) -> None:
"""We have an AM state and an FM state"""
self.amstate = AmState(self)
self.fmstate = FmState(self)
self.state = self.amstate

def toggle_amfm(self):
def toggle_amfm(self) -> None:
self.state.toggle_amfm()

def scan(self):
def scan(self) -> None:
self.state.scan()


Expand Down
6 changes: 3 additions & 3 deletions patterns/creational/borg.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
class Borg:
_shared_state: Dict[str, str] = {}

def __init__(self):
def __init__(self) -> None:
self.__dict__ = self._shared_state


class YourBorg(Borg):
def __init__(self, state=None):
def __init__(self, state: str = None) -> None:
super().__init__()
if state:
self.state = state
Expand All @@ -52,7 +52,7 @@ def __init__(self, state=None):
if not hasattr(self, "state"):
self.state = "Init"

def __str__(self):
def __str__(self) -> str:
return self.state


Expand Down
20 changes: 10 additions & 10 deletions patterns/creational/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class for a building, where the initializer (__init__ method) specifies the

# Abstract Building
class Building:
def __init__(self):
def __init__(self) -> None:
self.build_floor()
self.build_size()

Expand All @@ -44,24 +44,24 @@ def build_floor(self):
def build_size(self):
raise NotImplementedError

def __repr__(self):
def __repr__(self) -> str:
return "Floor: {0.floor} | Size: {0.size}".format(self)


# Concrete Buildings
class House(Building):
def build_floor(self):
def build_floor(self) -> None:
self.floor = "One"

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


class Flat(Building):
def build_floor(self):
def build_floor(self) -> None:
self.floor = "More than One"

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


Expand All @@ -72,19 +72,19 @@ def build_size(self):


class ComplexBuilding:
def __repr__(self):
def __repr__(self) -> str:
return "Floor: {0.floor} | Size: {0.size}".format(self)


class ComplexHouse(ComplexBuilding):
def build_floor(self):
def build_floor(self) -> None:
self.floor = "One"

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


def construct_building(cls):
def construct_building(cls) -> Building:
building = cls()
building.build_floor()
building.build_size()
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 @@ -28,7 +28,7 @@ class Delegator:
AttributeError: 'Delegate' object has no attribute 'do_anything'
"""

def __init__(self, delegate: Delegate):
def __init__(self, delegate: Delegate) -> None:
self.delegate = delegate

def __getattr__(self, name: str) -> Any | Callable:
Expand All @@ -44,7 +44,7 @@ def wrapper(*args, **kwargs):


class Delegate:
def __init__(self):
def __init__(self) -> None:
self.p1 = 123

def do_something(self, something: str) -> str:
Expand Down
27 changes: 15 additions & 12 deletions patterns/other/blackboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,33 @@
https://en.wikipedia.org/wiki/Blackboard_system
"""
from __future__ import annotations

import abc
import random

from typing import List


class Blackboard:
def __init__(self):
self.experts = []
def __init__(self) -> None:
self.experts: List = []
self.common_state = {
"problems": 0,
"suggestions": 0,
"contributions": [],
"progress": 0, # percentage, if 100 -> task is finished
}

def add_expert(self, expert):
def add_expert(self, expert: AbstractExpert) -> None:
self.experts.append(expert)


class Controller:
def __init__(self, blackboard):
def __init__(self, blackboard: Blackboard) -> None:
self.blackboard = blackboard

def run_loop(self):
def run_loop(self) -> List[str]:
"""
This function is a loop that runs until the progress reaches 100.
It checks if an expert is eager to contribute and then calls its contribute method.
Expand All @@ -44,7 +47,7 @@ def run_loop(self):


class AbstractExpert(metaclass=abc.ABCMeta):
def __init__(self, blackboard):
def __init__(self, blackboard: Blackboard) -> None:
self.blackboard = blackboard

@property
Expand All @@ -59,10 +62,10 @@ def contribute(self):

class Student(AbstractExpert):
@property
def is_eager_to_contribute(self):
def is_eager_to_contribute(self) -> bool:
return True

def contribute(self):
def contribute(self) -> None:
self.blackboard.common_state["problems"] += random.randint(1, 10)
self.blackboard.common_state["suggestions"] += random.randint(1, 10)
self.blackboard.common_state["contributions"] += [self.__class__.__name__]
Expand All @@ -71,10 +74,10 @@ def contribute(self):

class Scientist(AbstractExpert):
@property
def is_eager_to_contribute(self):
def is_eager_to_contribute(self) -> int:
return random.randint(0, 1)

def contribute(self):
def contribute(self) -> None:
self.blackboard.common_state["problems"] += random.randint(10, 20)
self.blackboard.common_state["suggestions"] += random.randint(10, 20)
self.blackboard.common_state["contributions"] += [self.__class__.__name__]
Expand All @@ -83,10 +86,10 @@ def contribute(self):

class Professor(AbstractExpert):
@property
def is_eager_to_contribute(self):
def is_eager_to_contribute(self) -> bool:
return True if self.blackboard.common_state["problems"] > 100 else False

def contribute(self):
def contribute(self) -> None:
self.blackboard.common_state["problems"] += random.randint(1, 2)
self.blackboard.common_state["suggestions"] += random.randint(10, 20)
self.blackboard.common_state["contributions"] += [self.__class__.__name__]
Expand Down
12 changes: 6 additions & 6 deletions patterns/structural/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,30 @@
class TextTag:
"""Represents a base text tag"""

def __init__(self, text):
def __init__(self, text: str) -> None:
self._text = text

def render(self):
def render(self) -> str:
return self._text


class BoldWrapper(TextTag):
"""Wraps a tag in <b>"""

def __init__(self, wrapped):
def __init__(self, wrapped: TextTag) -> None:
self._wrapped = wrapped

def render(self):
def render(self) -> str:
return f"<b>{self._wrapped.render()}</b>"


class ItalicWrapper(TextTag):
"""Wraps a tag in <i>"""

def __init__(self, wrapped):
def __init__(self, wrapped: TextTag) -> None:
self._wrapped = wrapped

def render(self):
def render(self) -> str:
return f"<i>{self._wrapped.render()}</i>"


Expand Down
10 changes: 5 additions & 5 deletions patterns/structural/facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ class CPU:
Simple CPU representation.
"""

def freeze(self):
def freeze(self) -> None:
print("Freezing processor.")

def jump(self, position):
def jump(self, position: str) -> None:
print("Jumping to:", position)

def execute(self):
def execute(self) -> None:
print("Executing.")


Expand All @@ -50,7 +50,7 @@ class Memory:
Simple memory representation.
"""

def load(self, position, data):
def load(self, position: str, data: str) -> None:
print(f"Loading from {position} data: '{data}'.")


Expand All @@ -59,7 +59,7 @@ class SolidStateDrive:
Simple solid state drive representation.
"""

def read(self, lba, size):
def read(self, lba: str, size: str) -> str:
return f"Some data from sector {lba} with size {size}"


Expand Down
Loading

0 comments on commit 9d4170d

Please sign in to comment.