Bladeren bron

Add example to DictPath docstring

Alois Mahdal 10 jaren geleden
bovenliggende
commit
4d4d1666d7
1 gewijzigde bestanden met toevoegingen van 21 en 1 verwijderingen
  1. 21
    1
      sznqalibs/hoover.py

+ 21
- 1
sznqalibs/hoover.py Bestand weergeven

@@ -220,7 +220,27 @@ class RuleOp():
220 220
 class DictPath():
221 221
     """Mixin that adds "path-like" behavior to the top dict of dicts.
222 222
 
223
-    See TinyCase for description"""
223
+    Use this class as a mixin for a deep dic-like structure and you can access
224
+    the elements using a path.  For example:
225
+
226
+        MyData(dict, DictPath):
227
+            pass
228
+
229
+        d = MyData({
230
+            'name': 'Joe',
231
+            'age': 34,
232
+            'ssn': {
233
+                'number': '012 345 678',
234
+                'expires': '10-01-16',
235
+            },
236
+        })
237
+
238
+        print ("%s's ssn number %s will expire on %s"
239
+                % (d.getpath('/name'),
240
+                   d.getpath('/ssn/number'),
241
+                   d.getpath('/ssn/expiry')))
242
+        # joe's ssn number 012 345 678 will expire 10-01-16
243
+    """
224 244
 
225 245
     DIV = "/"
226 246