Skip to content

Commit

Permalink
Revert "edit the publish_subscribe.py"
Browse files Browse the repository at this point in the history
This reverts commit fee0702.
  • Loading branch information
faif committed Sep 20, 2014
1 parent b655bf0 commit c10a94e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 30 deletions.
35 changes: 13 additions & 22 deletions composite.py
Expand Up @@ -5,10 +5,11 @@
A class which defines a composite object which can store
hieararchical dictionaries with names.
This class is same as a hiearchical dictionary, but it provides methods
to add/access/modify children by name, like a Composite.
This class is same as a hiearchical dictionary, but it
provides methods to add/access/modify children by name,
like a Composite.
Created Anand B Pillai <abpillai@gmail.com>
Created Anand B Pillai <abpillai@gmail.com>
"""
__author__ = "Anand B Pillai"
Expand All @@ -17,10 +18,8 @@


def normalize(val):
"""Normalize a string so that it can be used as an attribute to a Python
object
"""
""" Normalize a string so that it can be used as an attribute
to a Python object """

if val.find('-') != -1:
val = val.replace('-', '_')
Expand All @@ -39,7 +38,8 @@ def denormalize(val):

class SpecialDict(dict):

"""A dictionary type which allows direct attribute access to its keys """
""" A dictionary type which allows direct attribute
access to its keys """

def __getattr__(self, name):

Expand Down Expand Up @@ -127,13 +127,11 @@ def isLeaf(self):
return not self._children

def getName(self):

""" Return the name of this ConfigInfo object """

return self._name

def getIndex(self, child):

""" Return the index of the child ConfigInfo object 'child' """

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

def getProperty(self, child, key):

"""Return the value for the property for child 'child' with key 'key' """
""" Return the value for the property for child
'child' with key 'key' """

# First get the child's dictionary
childDict = self.getInfoDict(child)
if childDict:
return childDict.get(key, None)

def setProperty(self, child, key, value):
"""Set the value for the property 'key' for the child 'child' to 'value' """
def setProperty(self, child, key, value):
""" Set the value for the property 'key' for
the child 'child' to 'value' """

# First get the child's dictionary
childDict = self.getInfoDict(child)
if childDict:
childDict[key] = value

def getChildren(self):

""" Return the list of immediate children of this object """

return self._children

def getAllChildren(self):

""" Return the list of all children of this object """

l = []
Expand All @@ -182,15 +178,13 @@ def getAllChildren(self):
return l

def getChild(self, name):

""" Return the immediate child object with the given name """

for child in self._children:
if child.getName() == name:
return child

def findChild(self, name):

""" Return the child with the given name from the tree """

# Note - this returns the first child of the given name
Expand All @@ -202,7 +196,6 @@ def findChild(self, name):
return child

def findChildren(self, name):

""" Return a list of children with the given name from the tree """

# Note: this returns a list of all the children of a given
Expand All @@ -217,7 +210,6 @@ def findChildren(self, name):
return children

def getPropertyDict(self):

""" Return the property dictionary """

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

def getParent(self):

""" Return the person who created me """

return self._father
Expand Down
37 changes: 37 additions & 0 deletions iterator.py
@@ -0,0 +1,37 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
Implementation of the iterator pattern with a generator"""


def count_to(count):
"""Counts by word numbers, up to a maximum of five"""
numbers = ["one", "two", "three", "four", "five"]
# enumerate() returns a tuple containing a count (from start which
# defaults to 0) and the values obtained from iterating over sequence
for pos, number in zip(range(count), numbers):
yield number

# Test the generator
count_to_two = lambda: count_to(2)
count_to_five = lambda: count_to(5)

print('Counting to two...')
for number in count_to_two():
print(number, end=' ')

print()

print('Counting to five...')
for number in count_to_five():
print(number, end=' ')

print()

### OUTPUT ###
# Counting to two...
# one two
# Counting to five...
# one two three four five
15 changes: 7 additions & 8 deletions publish_subscribe.py
Expand Up @@ -10,8 +10,9 @@
class Provider:

def __init__(self):
self.subscribe_queue = {}
self.msg_queue=[]
self.msg_queue = []
self.subscribers = {}

def notify(self, msg):
self.msg_queue.append(msg)

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

def unsubscribe(self, msg, subscriber):
self.subscribers[msg].remove(subscriber)
if !self.subscribe[msg]:
del self.subscribe[msg]

def update(self):
for msg in self.msg_queue:
if msg in self.subscribers.keys():
for suber in self.subscribers[msg]:
suber.get(msg)
if msg in self.subscribers:
for sub in self.subscribers[msg]:
sub.run(msg)
self.msg_queue = []


Expand All @@ -53,7 +52,7 @@ def __init__(self, name, msg_center):
def subscribe(self, msg):
self.provider.subscribe(msg, self)

def get(self, msg):
def run(self, msg):
print("{} got {}".format(self.name, msg))


Expand Down

0 comments on commit c10a94e

Please sign in to comment.