-
Notifications
You must be signed in to change notification settings - Fork 7k
Add more type hints #397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add more type hints #397
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
|
@@ -22,43 +23,42 @@ def scan(self): | |
|
||
|
||
class AmState(State): | ||
def __init__(self, radio): | ||
def __init__(self, radio: Radio) -> None: | ||
faif marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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: | ||
faif marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pointless |
||
"""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() | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,12 +38,12 @@ | |
class Borg: | ||
_shared_state: Dict[str, str] = {} | ||
|
||
def __init__(self): | ||
def __init__(self) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pointless |
||
self.__dict__ = self._shared_state | ||
|
||
|
||
class YourBorg(Borg): | ||
def __init__(self, state=None): | ||
def __init__(self, state: str = None) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pointless |
||
super().__init__() | ||
if state: | ||
self.state = state | ||
|
@@ -52,7 +52,7 @@ def __init__(self, state=None): | |
if not hasattr(self, "state"): | ||
self.state = "Init" | ||
|
||
def __str__(self): | ||
def __str__(self) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pointless |
||
return self.state | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pointless |
||
self.build_floor() | ||
self.build_size() | ||
|
||
|
@@ -44,24 +44,24 @@ def build_floor(self): | |
def build_size(self): | ||
raise NotImplementedError | ||
|
||
def __repr__(self): | ||
def __repr__(self) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pointless |
||
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" | ||
|
||
|
||
|
@@ -72,19 +72,19 @@ def build_size(self): | |
|
||
|
||
class ComplexBuilding: | ||
def __repr__(self): | ||
def __repr__(self) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pointless |
||
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() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pointless |
||
self.delegate = delegate | ||
|
||
def __getattr__(self, name: str) -> Any | Callable: | ||
|
@@ -44,7 +44,7 @@ def wrapper(*args, **kwargs): | |
|
||
|
||
class Delegate: | ||
def __init__(self): | ||
def __init__(self) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pointless |
||
self.p1 = 123 | ||
|
||
def do_something(self, something: str) -> str: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,13 +8,14 @@ | |
|
||
https://en.wikipedia.org/wiki/Blackboard_system | ||
""" | ||
from __future__ import annotations | ||
|
||
import abc | ||
import random | ||
|
||
|
||
class Blackboard: | ||
def __init__(self): | ||
def __init__(self) -> None: | ||
alexkahan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.experts = [] | ||
self.common_state = { | ||
"problems": 0, | ||
|
@@ -23,12 +24,12 @@ def __init__(self): | |
"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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pointless "-> None" in init() |
||
self.blackboard = blackboard | ||
|
||
def run_loop(self): | ||
|
@@ -44,7 +45,7 @@ def run_loop(self): | |
|
||
|
||
class AbstractExpert(metaclass=abc.ABCMeta): | ||
def __init__(self, blackboard): | ||
def __init__(self, blackboard: Blackboard) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pointless "-> None" in init() |
||
self.blackboard = blackboard | ||
|
||
@property | ||
|
@@ -59,10 +60,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__] | ||
|
@@ -71,10 +72,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__] | ||
|
@@ -83,10 +84,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__] | ||
|
Uh oh!
There was an error while loading. Please reload this page.