[Checkins] SVN: zc.zk/trunk/src/zc/zk/ properties set and update methods now accept positional
Jim Fulton
jim at zope.com
Fri Dec 2 11:12:27 UTC 2011
Log message for revision 123560:
properties set and update methods now accept positional
mapping objects (or iterables of items) as well as keyword arguments.
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-02 04:03:34 UTC (rev 123559)
+++ zc.zk/trunk/src/zc/zk/README.txt 2011-12-02 11:12:26 UTC (rev 123560)
@@ -150,7 +150,8 @@
You can't set data properties, but you can update data by calling it's
``update`` method::
- >>> data.update(threads=2, secret='123')
+ >>> thread_info = {'threads': 2}
+ >>> data.update(thread_info, secret='123')
data updated
database: u'/databases/foomain'
favorite_color: u'red'
@@ -159,11 +160,15 @@
or by calling it's ``set`` method, which removes keys not listed::
- >>> data.set(threads=3, secret='1234')
+ >>> data.set(threads= 3, secret='1234')
data updated
secret: u'1234'
threads: 3
+Both update and set can take data from a positional data argument, or
+from keyword parameters. Keyword parameters take precedent over the
+positional data argument.
+
ZooKeeper Session Management
----------------------------
@@ -521,12 +526,21 @@
Properties objects provide the usual read-only mapping methods,
__getitem__, __len__, etc..
-``set(**properties)``
+``set(data=None, **properties)``
Set the properties for the node, replacing existing data.
-``update(**properties)``
+ The data argument, if given, must be a dictionary or something that
+ can be passed to the ``dict`` constructor. Items supplied as
+ keywords take precedence over items supplied in the data argument.
+
+``update(data=None, **properties)``
Update the properties for the node.
+ The data argument, if given, must be a dictionary or something that
+ can be passed to a dictiomnary's ``update`` method. Items supplied
+ as keywords take precedence over items supplied in the data
+ argument.
+
``__call__(callable)``
Register a callback to be called whenever a node's properties are changed.
@@ -551,6 +565,8 @@
- Added tree import and export.
- Added recursive node-deletion API.
- Added symbolic-links.
+- properties set and update methods now accept positional
+ mapping objects (or iterables of items) as well as keyword arguments.
- Added convenience access to low-level ZooKeeper APIs.
- Added ``OPEN_ACL_UNSAFE`` and ``READ_ACL_UNSAFE`` (in ``zc.zk``),
which are mentioned by the ZooKeeper docs. but not included in the
Modified: zc.zk/trunk/src/zc/zk/__init__.py
===================================================================
--- zc.zk/trunk/src/zc/zk/__init__.py 2011-12-02 04:03:34 UTC (rev 123559)
+++ zc.zk/trunk/src/zc/zk/__init__.py 2011-12-02 11:12:26 UTC (rev 123560)
@@ -465,13 +465,17 @@
self.data = data
zookeeper.set(self.session.handle, self.path, encode(data))
- def set(self, **data):
+ def set(self, data=None, **properties):
+ data = data and dict(data) or {}
+ data.update(properties)
self._set(data)
- def update(self, **updates):
- data = self.data.copy()
- data.update(updates)
- self._set(data)
+ def update(self, data=None, **properties):
+ d = self.data.copy()
+ if data:
+ d.update(data)
+ d.update(properties)
+ self._set(d)
def __hash__(self):
# Gaaaa, collections.Mapping
Modified: zc.zk/trunk/src/zc/zk/tests.py
===================================================================
--- zc.zk/trunk/src/zc/zk/tests.py 2011-12-02 04:03:34 UTC (rev 123559)
+++ zc.zk/trunk/src/zc/zk/tests.py 2011-12-02 11:12:26 UTC (rev 123560)
@@ -19,6 +19,7 @@
import manuel.testing
import mock
import os
+import pprint
import re
import StringIO
import time
@@ -462,7 +463,33 @@
"""
+def property_set_and_update_variations():
+ """
+ >>> zk = zc.zk.ZooKeeper('zookeeper.example.com:2181')
+ >>> data = zk.properties('/fooservice')
+ >>> @data
+ ... def _(data):
+ ... pprint.pprint(dict(data), width=70)
+ {u'database': u'/databases/foomain',
+ u'favorite_color': u'red',
+ u'threads': 1}
+ >>> data.set(dict(x=1))
+ {u'x': 1}
+ >>> data.set(dict(x=1), x=2, y=3)
+ {u'x': 2, u'y': 3}
+ >>> data.set(z=1)
+ {u'z': 1}
+ >>> data.update(a=1)
+ {u'a': 1, u'z': 1}
+ >>> data.update(dict(b=1), a=2)
+ {u'a': 2, u'b': 1, u'z': 1}
+ >>> data.update(dict(c=1))
+ {u'a': 2, u'b': 1, u'c': 1, u'z': 1}
+ >>> data.update(dict(d=1), d=2)
+ {u'a': 2, u'b': 1, u'c': 1, u'd': 2, u'z': 1}
+ """
+
def assert_(cond, mess=''):
if not cond:
print 'assertion failed: ', mess
More information about the checkins
mailing list