[lxml-dev] Issues with objectify.ObjectifiedElement: assignment, attribute handling, and documentation
Michael Pechal
Michael.Pechal at silabs.com
Sun Oct 7 21:30:34 CEST 2007
Hello,
I am new to XML and I have found lxml.objectify to be very useful. I am
using XML to store register settings. I use the register mnemonic as
the tag. I use custom attributes to store additional information, such
as address, description, apply, etc. I am also using the _pytype and
_xsi attributes. I am using the binary install of lxml 1.3.4 on WinXP
running Python 2.5.1.
My main problem is that assigning a new value to an
objectify.DataElement destroys the existing attribute list. My current
workaround is to retrieve the attributes with the items() call, assign
the new value, and then reapply attributes with set() method for each
pair in the items dict. I dug through the API documentation and I did
not see a way around this issue. Am I missing something here?
I thought about subclassing DataElement and then I scanned the SVN
development change list. I saw some discussion about preserving _pytype
or _xsi attributes, but does this include ALL attributes? If so, I will
proceed with a build from the latest SVN copy. How stable are dev
versions? Are there automated acceptance tests (unittest) that gate the
check in? I may just use my workaround until 1.3.5 arrives.
Another issue I noticed is that if I specify _xsi='int', the _pytype
attribute will be 'long' instead of 'integer', so I am forced to use
_pytype='integer' for all integer data elements. Also, if you run
objectify.annonate(), the integer becomes a long type again. Annotate
should look to the _xsi or even pyval type. Has this been fixed? This
is not really an issue for me, since I always keep the list annotated.
The objectify API documentation was helpful. As a new user, I had a few
problems with save and retrieve from file. I would suggest updating the
objectify API document to provide a full example of saving to and
loading from a file. I have provided a test case from my unittest code
below that may be useful for the documentation:
#
------------------------------------------------------------------------
-
def testFileSaveAndLoad(self):
""" Save to XML file, then reload and compare data. """
# note the self.objRoot is created in the setUp() method
tofile = etree.tostring(self.objRoot, pretty_print=True)
xmlFH = open('test.xml', 'w')
xmlFH.write(tofile)
xmlFH.close()
parser = etree.XMLParser(remove_blank_text=True)
lookup = objectify.ObjectifyElementClassLookup()
parser.setElementClassLookup(lookup)
tree = etree.parse('test.xml', parser)
root = tree.getroot() # crucial step, as parse() doesn't
return the root
fromfile = etree.tostring(root, pretty_print=True)
self.assertEqual(tofile, fromfile)
Also on the documentation front, there is a failure with help() on the
objectify module. :
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from lxml import objectify
>>> from lxml import etree
>>> help(objectify)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python25\lib\site.py", line 346, in __call__
return pydoc.help(*args, **kwds)
File "C:\Python25\lib\pydoc.py", line 1645, in __call__
self.help(request)
File "C:\Python25\lib\pydoc.py", line 1689, in help
else: doc(request, 'Help on %s:')
File "C:\Python25\lib\pydoc.py", line 1481, in doc
pager(title % desc + '\n\n' + text.document(object, name))
File "C:\Python25\lib\pydoc.py", line 324, in document
if inspect.ismodule(object): return self.docmodule(*args)
File "C:\Python25\lib\pydoc.py", line 1070, in docmodule
inspect.getclasstree(classlist, 1), name)]
File "C:\Python25\lib\inspect.py", line 656, in getclasstree
for parent in c.__bases__:
TypeError: 'functools.partial' object is not iterable
>>> objectify
<module 'lxml.objectify' from
'C:\Python25\lib\site-packages\lxml\objectify.pyd'>
Note that help(etree) works fine.
Thanks,
Michael
This email and any attachments thereto may contain private, confidential,
and privileged material for the sole use of the intended recipient. Any
review, copying, or distribution of this email (or any attachments thereto)
by others is strictly prohibited. If you are not the intended recipient,
please contact the sender immediately and permanently delete the original
and any copies of this email and any attachments thereto.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://codespeak.net/pipermail/lxml-dev/attachments/20071007/1e257c1b/attachment-0001.htm
More information about the lxml-dev
mailing list