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 <abpillai@gmail.com>
11+
Created Anand B Pillai <abpillai@gmail.com>
1312
1413
"""
1514
__author__ = "Anand B Pillai"
@@ -18,8 +17,10 @@
1817

1918

2019
def normalize(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
if val.find('-') != -1:
2526
val = val.replace('-', '_')
@@ -38,8 +39,7 @@ def denormalize(val):
3839

3940
class SpecialDict(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
return not self._children
128128

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

132133
return self._name
133134

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

137139
if child in self._children:
@@ -145,29 +147,31 @@ def getDict(self):
145147
return self[self._name]
146148

147149
def getProperty(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
if childDict:
154156
return childDict.get(key, None)
155157

156-
def setProperty(self, child, key, value):
157-
""" Set the value for the property 'key' for
158-
the child 'child' to 'value' """
158+
def setProperty(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
if childDict:
163165
childDict[key] = value
164166

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

168171
return self._children
169172

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

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

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

183188
for child in self._children:
184189
if child.getName() == name:
185190
return child
186191

187192
def findChild(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
return child
197203

198204
def findChildren(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
return children
211218

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

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

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

224233
return self._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
class Provider:
1111

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

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

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

2930
def update(self):
3031
for msg in self.msg_queue:
31-
if msg in self.subscribers:
32-
for sub in self.subscribers[msg]:
33-
sub.run(msg)
32+
if msg in self.subscribers.keys():
33+
for suber in self.subscribers[msg]:
34+
suber.get(msg)
3435
self.msg_queue = []
3536

3637

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

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

5859

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy