Skip to content

Commit cdefb80

Browse files
authored
Merge pull request faif#343 from rednafi/master
Added type hint to command pattern
2 parents 3514739 + 51e83c5 commit cdefb80

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+419
-242
lines changed

makefile

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# REDNAFI
2+
# This only works with embedded venv not virtualenv
3+
# Install venv: python3.8 -m venv venv
4+
# Activate venv: source venv/bin/activate
5+
6+
# Usage (line =black line length, path = action path, ignore= exclude folders)
7+
# ------
8+
# make pylinter [make pylinter line=88 path=.]
9+
# make pyupgrade
10+
11+
path := .
12+
line := 88
13+
ignore := *env
14+
15+
all:
16+
@echo
17+
18+
.PHONY: checkvenv
19+
checkvenv:
20+
# raises error if environment is not active
21+
ifeq ("$(VIRTUAL_ENV)","")
22+
@echo "Venv is not activated!"
23+
@echo "Activate venv first."
24+
@echo
25+
exit 1
26+
endif
27+
28+
.PHONY: pyupgrade
29+
pyupgrade: checkvenv
30+
# checks if pip-tools is installed
31+
ifeq ("$(wildcard venv/bin/pip-compile)","")
32+
@echo "Installing Pip-tools..."
33+
@pip install pip-tools
34+
endif
35+
36+
ifeq ("$(wildcard venv/bin/pip-sync)","")
37+
@echo "Installing Pip-tools..."
38+
@pip install pip-tools
39+
endif
40+
41+
# pip-tools
42+
@pip-compile --upgrade requirements-dev.txt
43+
@pip-compile --upgrade requirements.txt
44+
@pip-sync requirements-dev.txt requirements.txt
45+
46+
47+
.PHONY: pylinter
48+
pylinter: checkvenv
49+
# checks if black is installed
50+
ifeq ("$(wildcard venv/bin/black)","")
51+
@echo "Installing Black..."
52+
@pip install black
53+
endif
54+
55+
# checks if isort is installed
56+
ifeq ("$(wildcard venv/bin/isort)","")
57+
@echo "Installing Isort..."
58+
@pip install isort
59+
endif
60+
61+
# checks if flake8 is installed
62+
ifeq ("$(wildcard venv/bin/flake8)","")
63+
@echo -e "Installing flake8..."
64+
@pip install flake8
65+
@echo
66+
endif
67+
68+
# black
69+
@echo "Applying Black"
70+
@echo "----------------\n"
71+
@black --line-length $(line) --exclude $(ignore) $(path)
72+
@echo
73+
74+
# isort
75+
@echo "Applying Isort"
76+
@echo "----------------\n"
77+
@isort --atomic --profile black $(path)
78+
@echo
79+
80+
# flake8
81+
@echo "Applying Flake8"
82+
@echo "----------------\n"
83+
@flake8 --max-line-length "$(line)" \
84+
--max-complexity "18" \
85+
--select "B,C,E,F,W,T4,B9" \
86+
--ignore "E203,E266,E501,W503,F403,F401,E402" \
87+
--exclude ".git,__pycache__,old, build, \
88+
dist, venv" $(path)

patterns/behavioral/chain_of_responsibility.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from abc import ABC, abstractmethod
2222
from typing import Callable, Optional, Tuple, TypeVar
2323

24-
2524
T = TypeVar("T")
2625

2726

@@ -115,4 +114,5 @@ def main():
115114

116115
if __name__ == "__main__":
117116
import doctest
117+
118118
doctest.testmod(optionflags=doctest.ELLIPSIS)

patterns/behavioral/chaining_method.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ def __init__(self, name, action):
44
self.action = action
55

66
def do_action(self):
7-
print(self.name, self.action.name, end=' ')
7+
print(self.name, self.action.name, end=" ")
88
return self.action
99

1010

@@ -13,11 +13,11 @@ def __init__(self, name):
1313
self.name = name
1414

1515
def amount(self, val):
16-
print(val, end=' ')
16+
print(val, end=" ")
1717
return self
1818

1919
def stop(self):
20-
print('then stop')
20+
print("then stop")
2121

2222

2323
def main():
@@ -29,6 +29,7 @@ def main():
2929
"""
3030

3131

32-
if __name__ == '__main__':
32+
if __name__ == "__main__":
3333
import doctest
34+
3435
doctest.testmod()

patterns/behavioral/command.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,55 +20,57 @@
2020
https://docs.djangoproject.com/en/2.1/ref/request-response/#httprequest-objects
2121
"""
2222

23+
from typing import Union
24+
2325

2426
class HideFileCommand:
2527
"""
2628
A command to hide a file given its name
2729
"""
2830

29-
def __init__(self):
31+
def __init__(self) -> None:
3032
# an array of files hidden, to undo them as needed
3133
self._hidden_files = []
3234

33-
def execute(self, filename):
34-
print(f'hiding {filename}')
35+
def execute(self, filename: str) -> None:
36+
print(f"hiding {filename}")
3537
self._hidden_files.append(filename)
3638

37-
def undo(self):
39+
def undo(self) -> None:
3840
filename = self._hidden_files.pop()
39-
print(f'un-hiding {filename}')
41+
print(f"un-hiding {filename}")
4042

4143

4244
class DeleteFileCommand:
4345
"""
4446
A command to delete a file given its name
4547
"""
4648

47-
def __init__(self):
49+
def __init__(self) -> None:
4850
# an array of deleted files, to undo them as needed
4951
self._deleted_files = []
5052

51-
def execute(self, filename):
52-
print(f'deleting {filename}')
53+
def execute(self, filename: str) -> None:
54+
print(f"deleting {filename}")
5355
self._deleted_files.append(filename)
5456

55-
def undo(self):
57+
def undo(self) -> None:
5658
filename = self._deleted_files.pop()
57-
print(f'restoring {filename}')
59+
print(f"restoring {filename}")
5860

5961

6062
class MenuItem:
6163
"""
6264
The invoker class. Here it is items in a menu.
6365
"""
6466

65-
def __init__(self, command):
67+
def __init__(self, command: Union[HideFileCommand, DeleteFileCommand]) -> None:
6668
self._command = command
6769

68-
def on_do_press(self, filename):
70+
def on_do_press(self, filename: str) -> None:
6971
self._command.execute(filename)
7072

71-
def on_undo_press(self):
73+
def on_undo_press(self) -> None:
7274
self._command.undo()
7375

7476

@@ -101,4 +103,5 @@ def main():
101103

102104
if __name__ == "__main__":
103105
import doctest
106+
104107
doctest.testmod()

patterns/behavioral/iterator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ def main():
4040

4141
if __name__ == "__main__":
4242
import doctest
43+
4344
doctest.testmod()

patterns/behavioral/iterator_alt.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
class NumberWords:
1010
"""Counts by word numbers, up to a maximum of five"""
11+
1112
_WORD_MAP = (
12-
'one',
13-
'two',
14-
'three',
15-
'four',
16-
'five',
13+
"one",
14+
"two",
15+
"three",
16+
"four",
17+
"five",
1718
)
1819

1920
def __init__(self, start, stop):
@@ -33,6 +34,7 @@ def __next__(self): # this makes the class an Iterator
3334

3435
# Test the iterator
3536

37+
3638
def main():
3739
"""
3840
# Counting to two...

patterns/behavioral/mediator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def main():
4545
"""
4646

4747

48-
if __name__ == '__main__':
48+
if __name__ == "__main__":
4949
import doctest
50+
5051
doctest.testmod()

patterns/behavioral/memento.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
Provides the ability to restore an object to its previous state.
66
"""
77

8-
from copy import copy
9-
from copy import deepcopy
8+
from copy import copy, deepcopy
109

1110

1211
def memento(obj, deep=False):
@@ -67,14 +66,14 @@ def __init__(self, value):
6766
self.value = value
6867

6968
def __repr__(self):
70-
return '<%s: %r>' % (self.__class__.__name__, self.value)
69+
return "<%s: %r>" % (self.__class__.__name__, self.value)
7170

7271
def increment(self):
7372
self.value += 1
7473

7574
@Transactional
7675
def do_stuff(self):
77-
self.value = '1111' # <- invalid value
76+
self.value = "1111" # <- invalid value
7877
self.increment() # <- will fail and rollback
7978

8079

@@ -134,4 +133,5 @@ def main():
134133

135134
if __name__ == "__main__":
136135
import doctest
136+
137137
doctest.testmod(optionflags=doctest.ELLIPSIS)

patterns/behavioral/observer.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def notify(self, modifier=None):
3131

3232

3333
class Data(Subject):
34-
def __init__(self, name=''):
34+
def __init__(self, name=""):
3535
Subject.__init__(self)
3636
self.name = name
3737
self._data = 0
@@ -48,12 +48,14 @@ def data(self, value):
4848

4949
class HexViewer:
5050
def update(self, subject):
51-
print('HexViewer: Subject {} has data 0x{:x}'.format(subject.name, subject.data))
51+
print(
52+
"HexViewer: Subject {} has data 0x{:x}".format(subject.name, subject.data)
53+
)
5254

5355

5456
class DecimalViewer:
5557
def update(self, subject):
56-
print('DecimalViewer: Subject %s has data %d' % (subject.name, subject.data))
58+
print("DecimalViewer: Subject %s has data %d" % (subject.name, subject.data))
5759

5860

5961
def main():
@@ -97,4 +99,5 @@ def main():
9799

98100
if __name__ == "__main__":
99101
import doctest
102+
100103
doctest.testmod()

patterns/behavioral/publish_subscribe.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,5 @@ def main():
8989

9090
if __name__ == "__main__":
9191
import doctest
92+
9293
doctest.testmod()

patterns/behavioral/registry.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ def main():
4242

4343
if __name__ == "__main__":
4444
import doctest
45+
4546
doctest.testmod(optionflags=doctest.ELLIPSIS)

patterns/behavioral/specification.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ def __init__(self, one, other):
4747
self._other = other
4848

4949
def is_satisfied_by(self, candidate):
50-
return bool(self._one.is_satisfied_by(candidate) and self._other.is_satisfied_by(candidate))
50+
return bool(
51+
self._one.is_satisfied_by(candidate)
52+
and self._other.is_satisfied_by(candidate)
53+
)
5154

5255

5356
class OrSpecification(CompositeSpecification):
@@ -59,7 +62,10 @@ def __init__(self, one, other):
5962
self._other = other
6063

6164
def is_satisfied_by(self, candidate):
62-
return bool(self._one.is_satisfied_by(candidate) or self._other.is_satisfied_by(candidate))
65+
return bool(
66+
self._one.is_satisfied_by(candidate)
67+
or self._other.is_satisfied_by(candidate)
68+
)
6369

6470

6571
class NotSpecification(CompositeSpecification):
@@ -84,7 +90,7 @@ def is_satisfied_by(self, candidate):
8490

8591
class SuperUserSpecification(CompositeSpecification):
8692
def is_satisfied_by(self, candidate):
87-
return getattr(candidate, 'super_user', False)
93+
return getattr(candidate, "super_user", False)
8894

8995

9096
def main():
@@ -105,6 +111,7 @@ def main():
105111
"""
106112

107113

108-
if __name__ == '__main__':
114+
if __name__ == "__main__":
109115
import doctest
116+
110117
doctest.testmod()

patterns/behavioral/state.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def main():
8383
"""
8484

8585

86-
if __name__ == '__main__':
86+
if __name__ == "__main__":
8787
import doctest
88+
8889
doctest.testmod()

patterns/behavioral/strategy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ def main():
4848

4949
if __name__ == "__main__":
5050
import doctest
51+
5152
doctest.testmod()

patterns/behavioral/template.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,5 @@ def main():
6969

7070
if __name__ == "__main__":
7171
import doctest
72+
7273
doctest.testmod()

0 commit comments

Comments
 (0)