Skip to content

Commit fee0702

Browse files
committed
edit the publish_subscribe.py
1 parent 1496222 commit fee0702

File tree

3 files changed

+30
-57
lines changed

3 files changed

+30
-57
lines changed

‎composite.py‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
A class which defines a composite object which can store
66
hieararchical dictionaries with names.
77
8-
This class is same as a hiearchical dictionary, but it
9-
provides methods to add/access/modify children by name,
10-
like a Composite.
8+
This class is same as a hiearchical dictionary, but it provides methods
9+
to add/access/modify children by name, like a Composite.
1110
12-
Created Anand B Pillai <[email protected]>
11+
Created Anand B Pillai <[email protected]>
1312
1413
"""
1514
__author__="Anand B Pillai"
@@ -18,8 +17,10 @@
1817

1918

2019
defnormalize(val):
21-
""" Normalize a string so that it can be used as an attribute
22-
to a Python object """
20+
"""Normalize a string so that it can be used as an attribute to a Python
21+
22+
object
23+
"""
2324

2425
ifval.find('-') !=-1:
2526
val=val.replace('-', '_')
@@ -38,8 +39,7 @@ def denormalize(val):
3839

3940
classSpecialDict(dict):
4041

41-
""" A dictionary type which allows direct attribute
42-
access to its keys """
42+
"""A dictionary type which allows direct attribute access to its keys """
4343

4444
def__getattr__(self, name):
4545

@@ -127,11 +127,13 @@ def isLeaf(self):
127127
returnnotself._children
128128

129129
defgetName(self):
130+
130131
""" Return the name of this ConfigInfo object """
131132

132133
returnself._name
133134

134135
defgetIndex(self, child):
136+
135137
""" Return the index of the child ConfigInfo object 'child' """
136138

137139
ifchildinself._children:
@@ -145,29 +147,31 @@ def getDict(self):
145147
returnself[self._name]
146148

147149
defgetProperty(self, child, key):
148-
""" Return the value for the property for child
149-
'child' with key 'key' """
150+
151+
"""Return the value for the property for child 'child' with key 'key' """
150152

151153
# First get the child's dictionary
152154
childDict=self.getInfoDict(child)
153155
ifchildDict:
154156
returnchildDict.get(key, None)
155157

156-
defsetProperty(self, child, key, value):
157-
""" Set the value for the property 'key' for
158-
the child 'child' to 'value' """
158+
defsetProperty(self, child, key, value):
159+
160+
"""Set the value for the property 'key' for the child 'child' to 'value' """
159161

160162
# First get the child's dictionary
161163
childDict=self.getInfoDict(child)
162164
ifchildDict:
163165
childDict[key] =value
164166

165167
defgetChildren(self):
168+
166169
""" Return the list of immediate children of this object """
167170

168171
returnself._children
169172

170173
defgetAllChildren(self):
174+
171175
""" Return the list of all children of this object """
172176

173177
l= []
@@ -178,13 +182,15 @@ def getAllChildren(self):
178182
returnl
179183

180184
defgetChild(self, name):
185+
181186
""" Return the immediate child object with the given name """
182187

183188
forchildinself._children:
184189
ifchild.getName() ==name:
185190
returnchild
186191

187192
deffindChild(self, name):
193+
188194
""" Return the child with the given name from the tree """
189195

190196
# Note - this returns the first child of the given name
@@ -196,6 +202,7 @@ def findChild(self, name):
196202
returnchild
197203

198204
deffindChildren(self, name):
205+
199206
""" Return a list of children with the given name from the tree """
200207

201208
# Note: this returns a list of all the children of a given
@@ -210,6 +217,7 @@ def findChildren(self, name):
210217
returnchildren
211218

212219
defgetPropertyDict(self):
220+
213221
""" Return the property dictionary """
214222

215223
d=self.getChild('__properties')
@@ -219,6 +227,7 @@ def getPropertyDict(self):
219227
return{}
220228

221229
defgetParent(self):
230+
222231
""" Return the person who created me """
223232

224233
returnself._father

‎iterator.py‎

Lines changed: 0 additions & 37 deletions
This file was deleted.

‎publish_subscribe.py‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
classProvider:
1111

1212
def__init__(self):
13-
self.msg_queue= []
14-
self.subscribers={}
15-
13+
self.subscribe_queue={}
14+
self.msg_queue=[]
1615
defnotify(self, msg):
1716
self.msg_queue.append(msg)
1817

@@ -25,12 +24,14 @@ def subscribe(self, msg, subscriber):
2524

2625
defunsubscribe(self, msg, subscriber):
2726
self.subscribers[msg].remove(subscriber)
27+
if !self.subscribe[msg]:
28+
delself.subscribe[msg]
2829

2930
defupdate(self):
3031
formsginself.msg_queue:
31-
ifmsginself.subscribers:
32-
forsubinself.subscribers[msg]:
33-
sub.run(msg)
32+
ifmsginself.subscribers.keys():
33+
forsuberinself.subscribers[msg]:
34+
suber.get(msg)
3435
self.msg_queue= []
3536

3637

@@ -52,7 +53,7 @@ def __init__(self, name, msg_center):
5253
defsubscribe(self, msg):
5354
self.provider.subscribe(msg, self)
5455

55-
defrun(self, msg):
56+
defget(self, msg):
5657
print("{} got{}".format(self.name, msg))
5758

5859

0 commit comments

Comments
(0)