[Checkins] SVN: zc.zk/trunk/src/zc/zk/ Added __len__ to Children.
Jim Fulton
jim at zope.com
Tue Dec 6 16:29:28 UTC 2011
Log message for revision 123594:
Added __len__ to Children.
Changed:
U zc.zk/trunk/src/zc/zk/README.txt
U zc.zk/trunk/src/zc/zk/__init__.py
U zc.zk/trunk/src/zc/zk/tests.py
-=-
Modified: zc.zk/trunk/src/zc/zk/README.txt
===================================================================
--- zc.zk/trunk/src/zc/zk/README.txt 2011-12-06 16:20:53 UTC (rev 123593)
+++ zc.zk/trunk/src/zc/zk/README.txt 2011-12-06 16:29:27 UTC (rev 123594)
@@ -95,6 +95,11 @@
>>> sorted(addresses)
['192.168.0.42:8080', '192.168.0.42:8081']
+You can also get the number of children with ``len``:
+
+ >>> len(addresses)
+ 2
+
You can call the iterable with a callback function that is called
whenever the list of children changes::
@@ -727,6 +732,13 @@
Changes
-------
+0.3.0 (2011-12-??)
+------------------
+
+- `zc.zk.Children`_ objects now have a __len__, which is mainly useful
+ for testing whether they are empty.
+
+
0.2.0 (2011-12-05)
~~~~~~~~~~~~~~~~~~
Modified: zc.zk/trunk/src/zc/zk/__init__.py
===================================================================
--- zc.zk/trunk/src/zc/zk/__init__.py 2011-12-06 16:20:53 UTC (rev 123593)
+++ zc.zk/trunk/src/zc/zk/__init__.py 2011-12-06 16:29:27 UTC (rev 123594)
@@ -628,6 +628,9 @@
event_type = zookeeper.CHILD_EVENT
+ def __len__(self):
+ return len(self.data)
+
class Properties(NodeInfo, collections.Mapping):
event_type = zookeeper.CHANGED_EVENT
Modified: zc.zk/trunk/src/zc/zk/tests.py
===================================================================
--- zc.zk/trunk/src/zc/zk/tests.py 2011-12-06 16:20:53 UTC (rev 123593)
+++ zc.zk/trunk/src/zc/zk/tests.py 2011-12-06 16:29:27 UTC (rev 123594)
@@ -364,12 +364,22 @@
>>> sorted(children)
[]
+ >>> len(children)
+ 0
+ >>> bool(children)
+ False
+
>>> def create(path):
... zk.create(path, '', zc.zk.OPEN_ACL_UNSAFE)
>>> create('/test/a')
>>> sorted(children)
['a']
+ >>> len(children)
+ 1
+ >>> bool(children)
+ True
+
We can register callbacks:
>>> @children
@@ -396,6 +406,11 @@
>>> create('/test/c')
good ['a', 'b', 'c']
+ >>> len(children)
+ 3
+ >>> bool(children)
+ True
+
If a callback raises an error later, it is logged and the callback is
cancelled:
More information about the checkins
mailing list