Author: scoder
Date: Wed Jul 26 08:01:51 2006
New Revision: 30550
Modified:
lxml/branch/capi/doc/objectify.txt
Log:
objectify.txt: make clear that sequence operations on data classes work on the
tree sequence, not the data sequence (strings etc.)
Modified: lxml/branch/capi/doc/objectify.txt
==============================================================================
--- lxml/branch/capi/doc/objectify.txt (original)
+++ lxml/branch/capi/doc/objectify.txt Wed Jul 26 08:01:51 2006
@@ -203,7 +203,8 @@
-----------------
The objectify module knows about Python data types and tries its best to let
-element content behave like them. For example, this works::
+element content behave like them. For example, they support the normal math
+operators::
>>> root = etree.XML("<root><a>5</a><b>11</b><c>true</c></root>")
>>> root.a + root.b
@@ -225,6 +226,42 @@
... print "false!"
false!
+However, data elements continue to provide the objectify API. This means that
+sequence operations such as ``len()``, slicing and indexing (e.g. of strings)
+cannot behave as the Python types. Like all other tree elements, they show
+the normal slicing behaviour of objectify elements::
+
+ >>> root = etree.XML("<root><a>test</a><b>toast</b></root>")
+ >>> print root.a + ' me'
+ test me
+ >>> len(root.a)
+ 1
+ >>> [ a.tag for a in root.a ]
+ ['a']
+ >>> print root.a[0].tag
+ a
+
+ >>> print root.a
+ test
+ >>> [ str(a) for a in root.a[:1] ]
+ ['test']
+
+If you need to run sequence operations on data types, you must ask the API for
+the *real* Python value. The string value is always available throught the
+normal ElementTree ``.text`` attribute. Additionally, all data classes
+provide a ``.pyval`` attribute that returns the value as Python type::
+
+ >>> root = etree.XML("<root><a>test</a><b>5</b></root>")
+ >>> root.a.text
+ 'test'
+ >>> root.a.pyval
+ 'test'
+
+ >>> root.b.text
+ '5'
+ >>> root.b.pyval
+ 5
+
Objectify determines data types by trial and error, unless it finds an
attribute ``pytype`` in the namespace given by the URI in
``lxml.objectify.PYTYPE_NAMESPACE``, which must contain any of the following
|