Amara 2

34 views
Skip to first unread message

Werner

unread,
Sep 10, 2012, 10:52:08 AM9/10/12
to ak...@googlegroups.com
Hi all,

I am finally getting some time to get back into my project.

Things work fine if I write a XML file with Amara 2 and read it again,
but when I do the following I don't get the same result.

doc = xmlexp.createXMLdoc()
xmlu.addElementPlusValue(doc.wine, u"name", u"Querciabella")

In wingIDE debug probe window I do:
doc
<entity at 0x077284F8: 1 children>
doc.wine
<wine at 0x07728540: name u'wine', 0 namespaces, 0 attributes, 0 children>
doc.wine
<wine at 0x07728540: name u'wine', 0 namespaces, 0 attributes, 1 children>
unicode(getattr(doc.wine, 'name'))
u"<amara.tree.element at 0x07727570: name u'name', 0 namespaces, 0
attributes, 1 children>"

Why is the above not returning the wine name? But if I do the following
I get it.

for x in doc.wine:
print unicode(x)

Querciabella

I am sure I must be doing/missing something really stupid - I guess been
away from it too long:).

Werner

The code in the methods used above:

def createXMLdoc():
root = '''<wine></wine>'''

doc = bindery.parse(root)

return doc

def addElementPlusValue(element, name, value):
new_elem = amara.tree.element(None, name)
if not type(value) == unicode:
value = unicode(value)
new_elem.xml_append(amara.tree.text(value))
element.xml_append(new_elem)

Werner

unread,
Sep 11, 2012, 9:45:44 AM9/11/12
to ak...@googlegroups.com
Hi all,

On 10/09/2012 16:52, Werner wrote:
> Hi all,
>
> I am finally getting some time to get back into my project.
>
> Things work fine if I write a XML file with Amara 2 and read it again,
> but when I do the following I don't get the same result.
>
> doc = xmlexp.createXMLdoc()
> xmlu.addElementPlusValue(doc.wine, u"name", u"Querciabella")
Did some more searching/debugging and find that if after the above I
write things to a file and read it in again things work as expected.

I.e. doing this:

doc
<entity at 0x08EF2348: 1 children>
unicode(doc.wine.name)
u"<amara.tree.element at 0x060CB970: name u'name', 0 namespaces, 0
attributes, 1 children>"
tFile = open('vinoxmltest', 'w')
doc.xml_write(lookup("xml-indent"), stream=tFile)
tFile.close()
xdoc = bindery.parse('vinoxmltest')
unicode(doc.wine.name)
u"<amara.tree.element at 0x060CB970: name u'name', 0 namespaces, 0
attributes, 1 children>"
unicode(xdoc.wine.name)
u'Goldwater Esslin'

Why is it working on "xdoc.wine.name" but not on "doc.wine.name", what
am I doing wrong or missing here.

Work around would be writing to a temp file and reading again, but I
really only want to do this if there is no other way.

Werner

Uche Ogbuji

unread,
Sep 11, 2012, 10:05:46 AM9/11/12
to ak...@googlegroups.com
On Mon, Sep 10, 2012 at 8:52 AM, Werner <werner...@sfr.fr> wrote:
Hi all,

I am finally getting some time to get back into my project.

Things work fine if I write a XML file with Amara 2 and read it again, but when I do the following I don't get the same result.

        doc = xmlexp.createXMLdoc()
        xmlu.addElementPlusValue(doc.wine, u"name", u"Querciabella")

In wingIDE debug probe window I do:
doc
<entity at 0x077284F8: 1 children>
doc.wine
<wine at 0x07728540: name u'wine', 0 namespaces, 0 attributes, 0 children>
doc.wine
<wine at 0x07728540: name u'wine', 0 namespaces, 0 attributes, 1 children>
unicode(getattr(doc.wine, 'name'))
u"<amara.tree.element at 0x07727570: name u'name', 0 namespaces, 0 attributes, 1 children>"

Why is the above not returning the wine name?  But if I do the following I get it.

for x in doc.wine:
    print unicode(x)

Remember that in your case doc.wine is an iterator.  I suggest using the U function, which is a great convenience:

from amara.lib import U

Rather than the unicode function.

 
Querciabella

I am sure I must be doing/missing something really stupid - I guess been away from it too long:).

Werner

The code in the methods used above:

def createXMLdoc():
    root = '''<wine></wine>'''

    doc = bindery.parse(root)

    return doc

def addElementPlusValue(element, name, value):
    new_elem = amara.tree.element(None, name)


Here:
 
    if not type(value) == unicode:
        value = unicode(value)
    new_elem.xml_append(amara.tree.text(value))

All three lines could be reduced to one:

new_elem.xml_append(amara.tree.text(U(value)))
 
    element.xml_append(new_elem)


--
Uche Ogbuji                       http://uche.ogbuji.net
Founding Partner, Zepheira        http://zepheira.com
http://wearekin.org
http://www.thenervousbreakdown.com/author/uogbuji/
http://copia.ogbuji.net
http://www.linkedin.com/in/ucheogbuji
http://twitter.com/uogbuji

Uche Ogbuji

unread,
Sep 11, 2012, 10:34:56 AM9/11/12
to ak...@googlegroups.com
On Tue, Sep 11, 2012 at 7:45 AM, Werner <werner...@sfr.fr> wrote:
doc
<entity at 0x08EF2348: 1 children>
unicode(doc.wine.name)
u"<amara.tree.element at 0x060CB970: name u'name', 0 namespaces, 0 attributes, 1 children>"
tFile = open('vinoxmltest', 'w')
doc.xml_write(lookup("xml-indent"), stream=tFile)
tFile.close()
xdoc = bindery.parse('vinoxmltest')
unicode(doc.wine.name)
u"<amara.tree.element at 0x060CB970: name u'name', 0 namespaces, 0 attributes, 1 children>"
unicode(xdoc.wine.name)
u'Goldwater Esslin'

Why is it working on "xdoc.wine.name" but not on "doc.wine.name", what am I doing wrong or missing here.

Work around would be writing to a temp file and reading again, but I really only want to do this if there is no other way.

Not sure why I missed this the first time around, but for some reason reading the obvious came to me that you're mixing up binder nodes and non-bindery nodes.

Don't use amara.tree constructors to append things to bindery.  You need to use bindery's factories.  Refer to the tutorial:


Werner

unread,
Sep 11, 2012, 11:23:13 AM9/11/12
to ak...@googlegroups.com
Hi Uche,

Thanks for the quick feed back.

I am still a bit stuck.

I had this:
new_elem = amara.tree.element(None, name)
if not type(value) == unicode:
value = unicode(value)
new_elem.xml_append(amara.tree.text(value))
element.xml_append(new_elem)

Which I would change to:
dRoot = element.xml_root
if not type(value) == unicode:
value = unicode(value)
element.xml_append(dRoot.xml_element_factory(None, name))
??? but how do I do the amara.tree.text thingy here in bindery terms???

Werner

Uche Ogbuji

unread,
Sep 11, 2012, 12:03:28 PM9/11/12
to ak...@googlegroups.com
Yeah I should update the tutorial to cover that.  Bindery is more flexible and you can just do:

>>> from amara import bindery
>>> doc = bindery.nodes.entity_base()
>>> new_elem = doc.xml_element_factory(None, u'spam')
>>> new_elem.xml_append(u'BOO!')
>>> doc.xml_append(new_elem)
>>> doc.xml_write()
<?xml version="1.0" encoding="UTF-8"?>
<spam>BOO!</spam>>>> 

Or you could use xml_append_fragment, which is covered in the next tutorial example:

from amara import bindery
doc = bindery.nodes.entity_base()
doc.xml_append_fragment('<spam>BOO!</spam>')
doc.xml_write()


Werner

unread,
Sep 11, 2012, 2:21:35 PM9/11/12
to ak...@googlegroups.com
Good evening Uche,

Thanks again, this got things moving again, will work some more on this
tomorrow.

Werner
Reply all
Reply to author
Forward
0 new messages