Skip to content

Commit

Permalink
Added dependency free makefile for mformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
rednafi committed Jul 19, 2020
1 parent 2e021bf commit 51e83c5
Show file tree
Hide file tree
Showing 49 changed files with 403 additions and 229 deletions.
88 changes: 88 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# REDNAFI
# This only works with embedded venv not virtualenv
# Install venv: python3.8 -m venv venv
# Activate venv: source venv/bin/activate

# Usage (line =black line length, path = action path, ignore= exclude folders)
# ------
# make pylinter [make pylinter line=88 path=.]
# make pyupgrade

path := .
line := 88
ignore := *env

all:
@echo

.PHONY: checkvenv
checkvenv:
# raises error if environment is not active
ifeq ("$(VIRTUAL_ENV)","")
@echo "Venv is not activated!"
@echo "Activate venv first."
@echo
exit 1
endif

.PHONY: pyupgrade
pyupgrade: checkvenv
# checks if pip-tools is installed
ifeq ("$(wildcard venv/bin/pip-compile)","")
@echo "Installing Pip-tools..."
@pip install pip-tools
endif

ifeq ("$(wildcard venv/bin/pip-sync)","")
@echo "Installing Pip-tools..."
@pip install pip-tools
endif

# pip-tools
@pip-compile --upgrade requirements-dev.txt
@pip-compile --upgrade requirements.txt
@pip-sync requirements-dev.txt requirements.txt


.PHONY: pylinter
pylinter: checkvenv
# checks if black is installed
ifeq ("$(wildcard venv/bin/black)","")
@echo "Installing Black..."
@pip install black
endif

# checks if isort is installed
ifeq ("$(wildcard venv/bin/isort)","")
@echo "Installing Isort..."
@pip install isort
endif

# checks if flake8 is installed
ifeq ("$(wildcard venv/bin/flake8)","")
@echo -e "Installing flake8..."
@pip install flake8
@echo
endif

# black
@echo "Applying Black"
@echo "----------------\n"
@black --line-length $(line) --exclude $(ignore) $(path)
@echo

# isort
@echo "Applying Isort"
@echo "----------------\n"
@isort --atomic --profile black $(path)
@echo

# flake8
@echo "Applying Flake8"
@echo "----------------\n"
@flake8 --max-line-length "$(line)" \
--max-complexity "18" \
--select "B,C,E,F,W,T4,B9" \
--ignore "E203,E266,E501,W503,F403,F401,E402" \
--exclude ".git,__pycache__,old, build, \
dist, venv" $(path)
2 changes: 1 addition & 1 deletion patterns/behavioral/chain_of_responsibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from abc import ABC, abstractmethod
from typing import Callable, Optional, Tuple, TypeVar


T = TypeVar("T")


Expand Down Expand Up @@ -115,4 +114,5 @@ def main():

if __name__ == "__main__":
import doctest

doctest.testmod(optionflags=doctest.ELLIPSIS)
9 changes: 5 additions & 4 deletions patterns/behavioral/chaining_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def __init__(self, name, action):
self.action = action

def do_action(self):
print(self.name, self.action.name, end=' ')
print(self.name, self.action.name, end=" ")
return self.action


Expand All @@ -13,11 +13,11 @@ def __init__(self, name):
self.name = name

def amount(self, val):
print(val, end=' ')
print(val, end=" ")
return self

def stop(self):
print('then stop')
print("then stop")


def main():
Expand All @@ -29,6 +29,7 @@ def main():
"""


if __name__ == '__main__':
if __name__ == "__main__":
import doctest

doctest.testmod()
1 change: 1 addition & 0 deletions patterns/behavioral/iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ def main():

if __name__ == "__main__":
import doctest

doctest.testmod()
12 changes: 7 additions & 5 deletions patterns/behavioral/iterator_alt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@

class NumberWords:
"""Counts by word numbers, up to a maximum of five"""

_WORD_MAP = (
'one',
'two',
'three',
'four',
'five',
"one",
"two",
"three",
"four",
"five",
)

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

# Test the iterator


def main():
"""
# Counting to two...
Expand Down
3 changes: 2 additions & 1 deletion patterns/behavioral/mediator.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def main():
"""


if __name__ == '__main__':
if __name__ == "__main__":
import doctest

doctest.testmod()
8 changes: 4 additions & 4 deletions patterns/behavioral/memento.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
Provides the ability to restore an object to its previous state.
"""

from copy import copy
from copy import deepcopy
from copy import copy, deepcopy


def memento(obj, deep=False):
Expand Down Expand Up @@ -67,14 +66,14 @@ def __init__(self, value):
self.value = value

def __repr__(self):
return '<%s: %r>' % (self.__class__.__name__, self.value)
return "<%s: %r>" % (self.__class__.__name__, self.value)

def increment(self):
self.value += 1

@Transactional
def do_stuff(self):
self.value = '1111' # <- invalid value
self.value = "1111" # <- invalid value
self.increment() # <- will fail and rollback


Expand Down Expand Up @@ -134,4 +133,5 @@ def main():

if __name__ == "__main__":
import doctest

doctest.testmod(optionflags=doctest.ELLIPSIS)
9 changes: 6 additions & 3 deletions patterns/behavioral/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def notify(self, modifier=None):


class Data(Subject):
def __init__(self, name=''):
def __init__(self, name=""):
Subject.__init__(self)
self.name = name
self._data = 0
Expand All @@ -48,12 +48,14 @@ def data(self, value):

class HexViewer:
def update(self, subject):
print('HexViewer: Subject {} has data 0x{:x}'.format(subject.name, subject.data))
print(
"HexViewer: Subject {} has data 0x{:x}".format(subject.name, subject.data)
)


class DecimalViewer:
def update(self, subject):
print('DecimalViewer: Subject %s has data %d' % (subject.name, subject.data))
print("DecimalViewer: Subject %s has data %d" % (subject.name, subject.data))


def main():
Expand Down Expand Up @@ -97,4 +99,5 @@ def main():

if __name__ == "__main__":
import doctest

doctest.testmod()
1 change: 1 addition & 0 deletions patterns/behavioral/publish_subscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,5 @@ def main():

if __name__ == "__main__":
import doctest

doctest.testmod()
1 change: 1 addition & 0 deletions patterns/behavioral/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ def main():

if __name__ == "__main__":
import doctest

doctest.testmod(optionflags=doctest.ELLIPSIS)
15 changes: 11 additions & 4 deletions patterns/behavioral/specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def __init__(self, one, other):
self._other = other

def is_satisfied_by(self, candidate):
return bool(self._one.is_satisfied_by(candidate) and self._other.is_satisfied_by(candidate))
return bool(
self._one.is_satisfied_by(candidate)
and self._other.is_satisfied_by(candidate)
)


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

def is_satisfied_by(self, candidate):
return bool(self._one.is_satisfied_by(candidate) or self._other.is_satisfied_by(candidate))
return bool(
self._one.is_satisfied_by(candidate)
or self._other.is_satisfied_by(candidate)
)


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

class SuperUserSpecification(CompositeSpecification):
def is_satisfied_by(self, candidate):
return getattr(candidate, 'super_user', False)
return getattr(candidate, "super_user", False)


def main():
Expand All @@ -105,6 +111,7 @@ def main():
"""


if __name__ == '__main__':
if __name__ == "__main__":
import doctest

doctest.testmod()
3 changes: 2 additions & 1 deletion patterns/behavioral/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def main():
"""


if __name__ == '__main__':
if __name__ == "__main__":
import doctest

doctest.testmod()
1 change: 1 addition & 0 deletions patterns/behavioral/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ def main():

if __name__ == "__main__":
import doctest

doctest.testmod()
1 change: 1 addition & 0 deletions patterns/behavioral/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ def main():

if __name__ == "__main__":
import doctest

doctest.testmod()
7 changes: 4 additions & 3 deletions patterns/behavioral/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Visitor:
def visit(self, node, *args, **kwargs):
meth = None
for cls in node.__class__.__mro__:
meth_name = 'visit_' + cls.__name__
meth_name = "visit_" + cls.__name__
meth = getattr(self, meth_name, None)
if meth:
break
Expand All @@ -46,10 +46,10 @@ def visit(self, node, *args, **kwargs):
return meth(node, *args, **kwargs)

def generic_visit(self, node, *args, **kwargs):
print('generic_visit ' + node.__class__.__name__)
print("generic_visit " + node.__class__.__name__)

def visit_B(self, node, *args, **kwargs):
print('visit_B ' + node.__class__.__name__)
print("visit_B " + node.__class__.__name__)


def main():
Expand All @@ -70,4 +70,5 @@ def main():

if __name__ == "__main__":
import doctest

doctest.testmod()
3 changes: 2 additions & 1 deletion patterns/creational/abstract_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def main():


if __name__ == "__main__":
random.seed(1234) # for deterministic doctest outputs
random.seed(1234) # for deterministic doctest outputs
import doctest

doctest.testmod()
8 changes: 4 additions & 4 deletions patterns/creational/borg.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,18 @@ def __init__(self):


class YourBorg(Borg):

def __init__(self, state=None):
super().__init__()
if state:
self.state = state
else:
# initiate the first instance with default state
if not hasattr(self, 'state'):
self.state = 'Init'
if not hasattr(self, "state"):
self.state = "Init"

def __str__(self):
return self.state


def main():
"""
Expand Down Expand Up @@ -106,4 +105,5 @@ def main():

if __name__ == "__main__":
import doctest

doctest.testmod()
Loading

0 comments on commit 51e83c5

Please sign in to comment.