| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,10 +5,11 @@ | |||
| 5 | 5 | A class which defines a composite object which can store | |
| 6 | 6 | hieararchical dictionaries with names. | |
| 7 | 7 | ||
| 8 | - This class is same as a hiearchical dictionary, but it provides methods | ||
| 9 | - to add/access/modify children by name, like a Composite. | ||
| 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. | ||
| 10 | 11 | ||
| 11 | - Created Anand B Pillai <abpillai@gmail.com> | ||
| 12 | + Created Anand B Pillai <abpillai@gmail.com> | ||
| 12 | 13 | ||
| 13 | 14 | """ | |
| 14 | 15 | __author__ = "Anand B Pillai" | |
@@ -17,10 +18,8 @@ | |||
| 17 | 18 | ||
| 18 | 19 | ||
| 19 | 20 | def normalize(val): | |
| 20 | - """Normalize a string so that it can be used as an attribute to a Python | ||
| 21 | - | ||
| 22 | - object | ||
| 23 | - """ | ||
| 21 | + """ Normalize a string so that it can be used as an attribute | ||
| 22 | + to a Python object """ | ||
| 24 | 23 | ||
| 25 | 24 | if val.find('-') != -1: | |
| 26 | 25 | val = val.replace('-', '_') | |
@@ -39,7 +38,8 @@ def denormalize(val): | |||
| 39 | 38 | ||
| 40 | 39 | class SpecialDict(dict): | |
| 41 | 40 | ||
| 42 | - """A dictionary type which allows direct attribute access to its keys """ | ||
| 41 | + """ A dictionary type which allows direct attribute | ||
| 42 | + access to its keys """ | ||
| 43 | 43 | ||
| 44 | 44 | def __getattr__(self, name): | |
| 45 | 45 | ||
@@ -127,13 +127,11 @@ def isLeaf(self): | |||
| 127 | 127 | return not self._children | |
| 128 | 128 | ||
| 129 | 129 | def getName(self): | |
| 130 | - | ||
| 131 | 130 | """ Return the name of this ConfigInfo object """ | |
| 132 | 131 | ||
| 133 | 132 | return self._name | |
| 134 | 133 | ||
| 135 | 134 | def getIndex(self, child): | |
| 136 | - | ||
| 137 | 135 | """ Return the index of the child ConfigInfo object 'child' """ | |
| 138 | 136 | ||
| 139 | 137 | if child in self._children: | |
@@ -147,31 +145,29 @@ def getDict(self): | |||
| 147 | 145 | return self[self._name] | |
| 148 | 146 | ||
| 149 | 147 | def getProperty(self, child, key): | |
| 150 | - | ||
| 151 | - """Return the value for the property for child 'child' with key 'key' """ | ||
| 148 | + """ Return the value for the property for child | ||
| 149 | + 'child' with key 'key' """ | ||
| 152 | 150 | ||
| 153 | 151 | # First get the child's dictionary | |
| 154 | 152 | childDict = self.getInfoDict(child) | |
| 155 | 153 | if childDict: | |
| 156 | 154 | return childDict.get(key, None) | |
| 157 | 155 | ||
| 158 | - def setProperty(self, child, key, value): | ||
| 159 | - | ||
| 160 | - """Set the value for the property 'key' for the child 'child' to 'value' """ | ||
| 156 | + def setProperty(self, child, key, value): | ||
| 157 | + """ Set the value for the property 'key' for | ||
| 158 | + the child 'child' to 'value' """ | ||
| 161 | 159 | ||
| 162 | 160 | # First get the child's dictionary | |
| 163 | 161 | childDict = self.getInfoDict(child) | |
| 164 | 162 | if childDict: | |
| 165 | 163 | childDict[key] = value | |
| 166 | 164 | ||
| 167 | 165 | def getChildren(self): | |
| 168 | - | ||
| 169 | 166 | """ Return the list of immediate children of this object """ | |
| 170 | 167 | ||
| 171 | 168 | return self._children | |
| 172 | 169 | ||
| 173 | 170 | def getAllChildren(self): | |
| 174 | - | ||
| 175 | 171 | """ Return the list of all children of this object """ | |
| 176 | 172 | ||
| 177 | 173 | l = [] | |
@@ -182,15 +178,13 @@ def getAllChildren(self): | |||
| 182 | 178 | return l | |
| 183 | 179 | ||
| 184 | 180 | def getChild(self, name): | |
| 185 | - | ||
| 186 | 181 | """ Return the immediate child object with the given name """ | |
| 187 | 182 | ||
| 188 | 183 | for child in self._children: | |
| 189 | 184 | if child.getName() == name: | |
| 190 | 185 | return child | |
| 191 | 186 | ||
| 192 | 187 | def findChild(self, name): | |
| 193 | - | ||
| 194 | 188 | """ Return the child with the given name from the tree """ | |
| 195 | 189 | ||
| 196 | 190 | # Note - this returns the first child of the given name | |
@@ -202,7 +196,6 @@ def findChild(self, name): | |||
| 202 | 196 | return child | |
| 203 | 197 | ||
| 204 | 198 | def findChildren(self, name): | |
| 205 | - | ||
| 206 | 199 | """ Return a list of children with the given name from the tree """ | |
| 207 | 200 | ||
| 208 | 201 | # Note: this returns a list of all the children of a given | |
@@ -217,7 +210,6 @@ def findChildren(self, name): | |||
| 217 | 210 | return children | |
| 218 | 211 | ||
| 219 | 212 | def getPropertyDict(self): | |
| 220 | - | ||
| 221 | 213 | """ Return the property dictionary """ | |
| 222 | 214 | ||
| 223 | 215 | d = self.getChild('__properties') | |
@@ -227,7 +219,6 @@ def getPropertyDict(self): | |||
| 227 | 219 | return {} | |
| 228 | 220 | ||
| 229 | 221 | def getParent(self): | |
| 230 | - | ||
| 231 | 222 | """ Return the person who created me """ | |
| 232 | 223 | ||
| 233 | 224 | return self._father | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,37 @@ | |||
| 1 | + #!/usr/bin/env python | ||
| 2 | + # -*- coding: utf-8 -*- | ||
| 3 | + | ||
| 4 | + """http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/ | ||
| 5 | + | ||
| 6 | + Implementation of the iterator pattern with a generator""" | ||
| 7 | + | ||
| 8 | + | ||
| 9 | + def count_to(count): | ||
| 10 | + """Counts by word numbers, up to a maximum of five""" | ||
| 11 | + numbers = ["one", "two", "three", "four", "five"] | ||
| 12 | + # enumerate() returns a tuple containing a count (from start which | ||
| 13 | + # defaults to 0) and the values obtained from iterating over sequence | ||
| 14 | + for pos, number in zip(range(count), numbers): | ||
| 15 | + yield number | ||
| 16 | + | ||
| 17 | + # Test the generator | ||
| 18 | + count_to_two = lambda: count_to(2) | ||
| 19 | + count_to_five = lambda: count_to(5) | ||
| 20 | + | ||
| 21 | + print('Counting to two...') | ||
| 22 | + for number in count_to_two(): | ||
| 23 | + print(number, end=' ') | ||
| 24 | + | ||
| 25 | + print() | ||
| 26 | + | ||
| 27 | + print('Counting to five...') | ||
| 28 | + for number in count_to_five(): | ||
| 29 | + print(number, end=' ') | ||
| 30 | + | ||
| 31 | + print() | ||
| 32 | + | ||
| 33 | + ### OUTPUT ### | ||
| 34 | + # Counting to two... | ||
| 35 | + # one two | ||
| 36 | + # Counting to five... | ||
| 37 | + # one two three four five | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,8 +10,9 @@ | |||
| 10 | 10 | class Provider: | |
| 11 | 11 | ||
| 12 | 12 | def __init__(self): | |
| 13 | - self.subscribe_queue = {} | ||
| 14 | - self.msg_queue=[] | ||
| 13 | + self.msg_queue = [] | ||
| 14 | + self.subscribers = {} | ||
| 15 | + | ||
| 15 | 16 | def notify(self, msg): | |
| 16 | 17 | self.msg_queue.append(msg) | |
| 17 | 18 | ||
@@ -24,14 +25,12 @@ def subscribe(self, msg, subscriber): | |||
| 24 | 25 | ||
| 25 | 26 | def unsubscribe(self, msg, subscriber): | |
| 26 | 27 | self.subscribers[msg].remove(subscriber) | |
| 27 | - if !self.subscribe[msg]: | ||
| 28 | - del self.subscribe[msg] | ||
| 29 | 28 | ||
| 30 | 29 | def update(self): | |
| 31 | 30 | for msg in self.msg_queue: | |
| 32 | - if msg in self.subscribers.keys(): | ||
| 33 | - for suber in self.subscribers[msg]: | ||
| 34 | - suber.get(msg) | ||
| 31 | + if msg in self.subscribers: | ||
| 32 | + for sub in self.subscribers[msg]: | ||
| 33 | + sub.run(msg) | ||
| 35 | 34 | self.msg_queue = [] | |
| 36 | 35 | ||
| 37 | 36 | ||
@@ -53,7 +52,7 @@ def __init__(self, name, msg_center): | |||
| 53 | 52 | def subscribe(self, msg): | |
| 54 | 53 | self.provider.subscribe(msg, self) | |
| 55 | 54 | ||
| 56 | - def get(self, msg): | ||
| 55 | + def run(self, msg): | ||
| 57 | 56 | print("{} got {}".format(self.name, msg)) | |
| 58 | 57 | ||
| 59 | 58 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments