Skip to content

Commit 63dbcbf

Browse files
committed
Merge pull request #1 from faif/master
Merge with original repository
2 parents bcaa897 + 2123037 commit 63dbcbf

File tree

6 files changed

+126
-35
lines changed

6 files changed

+126
-35
lines changed

borg.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,34 @@ class Borg:
33

44
def __init__(self):
55
self.__dict__ = self.__shared_state
6-
self.state = 'Running'
76

87
def __str__(self):
98
return self.state
109

10+
class YourBorg(Borg):
11+
pass
12+
1113
if __name__ == '__main__':
1214
rm1 = Borg()
1315
rm2 = Borg()
1416

15-
print('rm1 state: {}'.format(rm1))
16-
print('rm2 state: {}'.format(rm2))
17+
rm1.state = 'Idle'
18+
rm2.state = 'Running'
19+
20+
print('rm1:', rm1)
21+
print('rm2:', rm2)
22+
23+
rm2.state = 'Zombie'
24+
25+
print('rm1:', rm1)
26+
print('rm2:', rm2)
27+
28+
print('rm1 id:', id(rm1))
29+
print('rm2 id:', id(rm2))
1730

18-
rm2.state = 'Idle'
31+
rm3 = YourBorg()
1932

20-
print('rm1 state: {}'.format(rm1))
21-
print('rm2 state: {}'.format(rm2))
33+
print('rm1:', rm1)
34+
print('rm2:', rm2)
35+
print('rm3:', rm3)
2236

23-
print('rm1 id: {}', id(rm1))
24-
print('rm2 id: {}', id(rm2))

factory_method.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1+
#encoding=utf-8
12
'''http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/'''
23

3-
class GreekGetter:
4-
"""A simple localizer a la gettext"""
5-
def __init__(self):
4+
class GreekGetter:
5+
"""A simple localizer a la gettext"""
6+
def __init__(self):
67
self.trans = dict(dog="σκύλος", cat="γάτα")
7-
8-
def get(self, msgid):
8+
9+
def get(self, msgid):
910
"""We'll punt if we don't have a translation"""
10-
try:
11-
return str(self.trans[msgid])
12-
except KeyError:
13-
return str(msgid)
11+
try:
12+
return self.trans[msgid]
13+
except KeyError:
14+
return str(msgid)
1415

15-
class EnglishGetter:
16+
class EnglishGetter:
1617
"""Simply echoes the msg ids"""
17-
def get(self, msgid):
18-
return str(msgid)
18+
def get(self, msgid):
19+
return str(msgid)
1920

20-
def get_localizer(language="English"):
21-
"""The factory method"""
22-
languages = dict(English=EnglishGetter,Greek=GreekGetter)
23-
return languages[language]()
21+
def get_localizer(language="English"):
22+
"""The factory method"""
23+
languages = dict(English=EnglishGetter, Greek=GreekGetter)
24+
return languages[language]()
2425

25-
# Create our localizers
26-
e, j = get_localizer("English"), get_localizer("Greek")
27-
# Localize some text
28-
for msgid in "dog parrot cat bear".split():
29-
print(e.get(msgid), j.get(msgid))
26+
# Create our localizers
27+
e, g = get_localizer("English"), get_localizer("Greek")
28+
# Localize some text
29+
for msgid in "dog parrot cat bear".split():
30+
print(e.get(msgid), g.get(msgid))

graph_search.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
class GraphSearch:
2+
"""Graph search emulation in python, from source http://www.python.org/doc/essays/graphs/"""
3+
4+
def __init__(self, graph):
5+
self.graph = graph
6+
7+
def find_path(self, start, end, path=[]):
8+
self.start = start
9+
self.end = end
10+
self.path = path
11+
12+
self.path+=[self.start]
13+
if self.start == self.end:
14+
return self.path
15+
if not self.graph.has_key(self.start):
16+
return None
17+
for node in self.graph[self.start]:
18+
if node not in self.path:
19+
newpath = self.find_path(node, self.end, self.path)
20+
if newpath:
21+
return newpath
22+
return None
23+
24+
def find_all_path(self, start, end, path=[]):
25+
self.start = start
26+
self.end = end
27+
self.path = path
28+
self.path+=[self.start]
29+
if self.start == self.end:
30+
return [self.path]
31+
if not self.graph.has_key(self.start):
32+
return []
33+
paths=[]
34+
for node in self.graph[self.start]:
35+
if node not in self.path:
36+
newpaths = self.find_all_path(node, self.end, self.path)
37+
for newpath in newpaths:
38+
paths.append(newpath)
39+
return paths
40+
41+
def find_shortest_path(self, start, end, path=[]):
42+
self.start = start
43+
self.end = end
44+
self.path = path
45+
46+
self.path+=[self.start]
47+
if self.start == self.end:
48+
return self.path
49+
if not self.graph.has_key(self.start):
50+
return None
51+
shortest = None
52+
for node in self.graph[self.start]:
53+
if node not in self.path:
54+
newpath = self.find_shortest_path(node, self.end, self.path)
55+
if newpath:
56+
if not shortest or len(newpath) < len(shortest):
57+
shortest = newpath
58+
return shortest
59+
60+
#example of graph usage
61+
graph = {'A':['B', 'C'],
62+
'B': ['C', 'D'],
63+
'C': ['D'],
64+
'D': ['C'],
65+
'E': ['F'],
66+
'F': ['C']
67+
}
68+
69+
#inistialization of new graph search object
70+
graph1 = GraphSearch(graph)
71+
72+
73+
print graph1.find_path('A', 'D')
74+
print graph1.find_all_path('A', 'D')
75+
print graph1.find_shortest_path('A', 'D')

iterator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
def count_to(count):
55
"""Counts by word numbers, up to a maximum of five"""
66
numbers = ["one", "two", "three", "four", "five"]
7-
# The zip keeps from counting over the limit
8-
for number, pos in zip(numbers, list(range(count))):
7+
# enumerate() returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over sequence
8+
for pos, number in enumerate(numbers):
99
yield number
1010

1111
# Test the generator

pool.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ def __del__(self):
2525
self._q.put(self.o)
2626
self.o = None
2727

28-
if __name__ == "__main__":
29-
import queue
28+
if __name__ == "__main__":
29+
try:
30+
import queue as Queue
31+
except: # python 2.x compatibility
32+
import Queue
3033

3134
def testObj(Q):
3235
someObj = qObj(Q, True)
3336
print('Inside func: {}'.format(someObj.o))
3437

35-
aQ = queue.Queue()
38+
aQ = Queue.Queue()
3639
aQ.put("yam")
3740

3841
with qObj(aQ) as obj:

proxy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def __init__(self):
1414

1515
def work (self):
1616
print("Proxy checking for Sales Manager availability")
17-
if self.busy == 'Yes':
17+
if self.busy == 'No':
1818
self.sales = SalesManager()
1919
time.sleep(2);
2020
self.sales.talk()

0 commit comments

Comments
 (0)