Skip to content

Commit b54cad9

Browse files
committed
Merge pull request faif#144 from yarikoptic/master
ENH+BF: fix up of some tests/docs to pass with python2.7, testing on travis, consistent shebanging,
2 parents ad59bd5 + 126bbd9 commit b54cad9

26 files changed

+252
-41
lines changed

‎.travis.yml‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# vim ft=yaml
2+
# travis-ci.org definition for python-patterns build
3+
language: python
4+
5+
sudo: false
6+
7+
python:
8+
- "2.7"
9+
- "3.3"
10+
- "3.4"
11+
- "3.5"
12+
# Disabled for now since cause more pain than gain
13+
# - "pypy"
14+
# - "pypy3"
15+
16+
cache:
17+
- pip
18+
19+
install:
20+
- travis_retry pip install -q coveralls codecov
21+
- pip install flake8 # eventually worth
22+
23+
script:
24+
# Run tests
25+
- PYTHONPATH=. nosetests -s -v --with-doctest --with-cov --cover-package . --logging-level=INFO -v .
26+
# Actually run all the scripts, contributing to coverage
27+
- ./run_all.sh
28+
# for now failure in flaking is ignored
29+
- flake8 *py || echo "PEP8 the code"
30+
31+
after_success:
32+
- coveralls
33+
- codecov

‎3-tier.py‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ def __get__(self, obj, klas):
1919
classBusinessLogic(object):
2020
""" Business logic holding data store instances """
2121

22-
def__init__(self, data):
23-
self.data=data
22+
data=Data()
2423

2524
defproduct_list(self):
2625
returnself.data['products'].keys()
@@ -32,8 +31,8 @@ def product_information(self, product):
3231
classUi(object):
3332
""" UI interaction class """
3433

35-
def__init__(self, logic):
36-
self.business_logic=logic
34+
def__init__(self):
35+
self.business_logic=BusinessLogic()
3736

3837
defget_product_list(self):
3938
print('PRODUCT LIST:')
@@ -54,9 +53,7 @@ def get_product_information(self, product):
5453

5554

5655
defmain():
57-
data=Data()
58-
logic=BusinessLogic(data)
59-
ui=Ui(logic)
56+
ui=Ui()
6057
ui.get_product_list()
6158
ui.get_product_information('cheese')
6259
ui.get_product_information('eggs')
@@ -72,7 +69,7 @@ def main():
7269
# cheese
7370
# eggs
7471
# milk
75-
#
72+
#
7673
# (Fetching from Data Store)
7774
# PRODUCT INFORMATION:
7875
# Name: Cheese, Price: 2.00, Quantity: 10

‎adapter.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ class Adapter(object):
4646
>>> objects = []
4747
>>> dog = Dog()
4848
>>> print(dog.__dict__)
49+
{'name': 'Dog'}
4950
>>> objects.append(Adapter(dog, make_noise=dog.bark))
5051
>>> print(objects[0].original_dict())
52+
{'name': 'Dog'}
5153
>>> cat = Cat()
5254
>>> objects.append(Adapter(cat, make_noise=cat.meow))
5355
>>> human = Human()

‎chain.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def delegate(self, requests):
5757
defcoroutine(func):
5858
defstart(*args, **kwargs):
5959
cr=func(*args, **kwargs)
60-
cr.next()
60+
next(cr)
6161
returncr
6262
returnstart
6363

‎chaining_method.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4+
from __future__ importprint_function
5+
46
classPerson(object):
57

68
def__init__(self, name, action):

‎command.py‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf-8 -*-
33

44
importos
5-
5+
fromos.pathimportlexists
66

77
classMoveFileCommand(object):
88

@@ -28,13 +28,23 @@ def main():
2828
command_stack.append(MoveFileCommand('foo.txt', 'bar.txt'))
2929
command_stack.append(MoveFileCommand('bar.txt', 'baz.txt'))
3030

31-
# they can be executed later on
32-
forcmdincommand_stack:
33-
cmd.execute()
34-
35-
# and can also be undone at will
36-
forcmdinreversed(command_stack):
37-
cmd.undo()
31+
# verify that none of the target files exist
32+
assert(notlexists("foo.txt"))
33+
assert(notlexists("bar.txt"))
34+
assert(notlexists("baz.txt"))
35+
try:
36+
withopen("foo.txt", "w"): # Creating the file
37+
pass
38+
39+
# they can be executed later on
40+
forcmdincommand_stack:
41+
cmd.execute()
42+
43+
# and can also be undone at will
44+
forcmdinreversed(command_stack):
45+
cmd.undo()
46+
finally:
47+
os.unlink("foo.txt")
3848

3949
if__name__=="__main__":
4050
main()

‎decorator.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python
12
"""https://docs.python.org/2/library/functools.html#functools.wraps"""
23
"""https://stackoverflow.com/questions/739654/how-can-i-make-a-chain-of-function-decorators-in-python/739665#739665"""
34

‎delegation_pattern.py‎

100755100644
File mode changed.

‎facade.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
importtime
55

6-
SLEEP=0.5
6+
SLEEP=0.1
77

88

99
# Complex Parts

‎flyweight.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ def __init__(self, *args, **kwargs):
7474

7575

7676
if__name__=='__main__':
77+
importsys
78+
ifsys.version_info[0] >2:
79+
sys.stderr.write("!!! This example is compatible only with Python 2 ATM !!!\n")
80+
raiseSystemExit(0)
81+
7782
# comment __new__ and uncomment __init__ to see the difference
7883
c1=Card('9', 'h')
7984
c2=Card('9', 'h')

0 commit comments

Comments
(0)