Skip to content

Commit 43992f1

Browse files
committed
Merge pull request #1 from faif/master
add new content from faif's python-patterns
2 parents ee37556 + dc29feb commit 43992f1

35 files changed

Lines changed: 571 additions & 180 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__

3-tier.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,17 @@ def main():
5656

5757
if __name__ == '__main__':
5858
main()
59+
60+
### OUTPUT ###
61+
# PRODUCT LIST:
62+
# eggs
63+
# milk
64+
# cheese
65+
#
66+
# PRODUCT INFORMATION:
67+
# Name: Cheese, Price: 2.00, Quantity: 10
68+
# PRODUCT INFORMATION:
69+
# Name: Eggs, Price: 0.20, Quantity: 100
70+
# PRODUCT INFORMATION:
71+
# Name: Milk, Price: 1.50, Quantity: 10
72+
# That product "arepas" does not exist in the records

README.md

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
python-patterns
22
===============
33

4-
A collection of design patterns implemented (by other people) in python
4+
A collection of design patterns implemented (by other people) in python.
5+
6+
When an implementation is added or modified, be sure to update this file and
7+
rerun `append_output.sh` to keep the output comments at the bottom up to date.
58

69
Current Patterns:
710

8-
* 3-tier
9-
* composite
11+
* 3-tier
12+
* abstract_factory
13+
* adapter
14+
* borg
15+
* bridge
16+
* builder
17+
* catalog
18+
* chain
19+
* command
20+
* composite
21+
* decorator
22+
* facade
23+
* factory_method
24+
* flyweight
25+
* graph_search
26+
* iterator
27+
* mediator
28+
* memento
1029
* mvc
11-
* decorator
1230
* null
13-
* facade
1431
* observer
15-
* abstract_factory
16-
* factory_method
1732
* pool
18-
* adapter
19-
* flyweight
2033
* prototype
21-
* borg
2234
* proxy
23-
* bridge
24-
* graph_search
35+
* publish_subscribe
2536
* state
26-
* builder
27-
* iterator
2837
* strategy
29-
* chain
30-
* mediator
3138
* template
32-
* command
33-
* memento
3439
* visitor
35-
* publish_subscribe

__pycache__/facade.cpython-33.pyc

-3.51 KB
Binary file not shown.
-6.83 KB
Binary file not shown.

__pycache__/null.cpython-33.pyc

-2.85 KB
Binary file not shown.

abstract_factory.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
14
# http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
25

36
"""Implementation of the abstract factory pattern"""
@@ -73,3 +76,17 @@ def get_factory():
7376
shop.pet_factory = get_factory()
7477
shop.show_pet()
7578
print("=" * 20)
79+
80+
### OUTPUT ###
81+
# This is a lovely Dog
82+
# It says woof
83+
# It eats dog food
84+
# ====================
85+
# This is a lovely Cat
86+
# It says meow
87+
# It eats cat food
88+
# ====================
89+
# This is a lovely Dog
90+
# It says woof
91+
# It eats dog food
92+
# ====================

adapter.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# http://ginstrom.com/scribbles/2008/11/06/generic-adapter-class-in-python/
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""http://ginstrom.com/scribbles/2008/11/06/generic-adapter-class-in-python/"""
25

36
import os
47

@@ -86,5 +89,10 @@ def main():
8689

8790

8891
if __name__ == "__main__":
89-
import doctest
90-
doctest.testmod()
92+
main()
93+
94+
### OUTPUT ###
95+
# A Dog goes woof!
96+
# A Cat goes meow!
97+
# A Human goes 'hello'
98+
# A Car goes vroom!!!

append_output.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
src=$(sed -n -e '/### OUTPUT ###/,$!p' "$1")
6+
output=$(python "$1" | sed 's/^/# /')
7+
8+
# These are done separately to avoid having to insert a newline, which causes
9+
# problems when the text itself has '\n' in strings
10+
echo "$src" > $1
11+
echo -e "\n### OUTPUT ###" >> $1
12+
echo "$output" >> $1

borg.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
15
class Borg:
26
__shared_state = {}
37

@@ -18,19 +22,30 @@ class YourBorg(Borg):
1822
rm1.state = 'Idle'
1923
rm2.state = 'Running'
2024

21-
print('rm1:', rm1)
22-
print('rm2:', rm2)
25+
print('rm1: {0}'.format(rm1))
26+
print('rm2: {0}'.format(rm2))
2327

2428
rm2.state = 'Zombie'
2529

26-
print('rm1:', rm1)
27-
print('rm2:', rm2)
30+
print('rm1: {0}'.format(rm1))
31+
print('rm2: {0}'.format(rm2))
2832

29-
print('rm1 id:', id(rm1))
30-
print('rm2 id:', id(rm2))
33+
print('rm1 id: {0}'.format(id(rm1)))
34+
print('rm2 id: {0}'.format(id(rm2)))
3135

3236
rm3 = YourBorg()
3337

34-
print('rm1:', rm1)
35-
print('rm2:', rm2)
36-
print('rm3:', rm3)
38+
print('rm1: {0}'.format(rm1))
39+
print('rm2: {0}'.format(rm2))
40+
print('rm3: {0}'.format(rm3))
41+
42+
### OUTPUT ###
43+
# rm1: Running
44+
# rm2: Running
45+
# rm1: Zombie
46+
# rm2: Zombie
47+
# rm1 id: 139825262601040
48+
# rm2 id: 139825262601104
49+
# rm1: Zombie
50+
# rm2: Zombie
51+
# rm3: Zombie

0 commit comments

Comments
 (0)