questions: amara and python

I’m a recovering PHP fiend so please forgive me if this is incredibly simple.

One big reason I’m switching to python is all of the wonderful tools for manipulating XML. For example, the amara xml toolkit is exactly what I’ve been looking for. It’s really really nice. I’ve been toying around, and I was able to create a function that returns a data binding for some XML. It’s pretty simple, I have some rules that turn some of the nodes into simple text nodes, and it reads in the xml by dereferencing a URI.

This all works quite nicely, I then export this as a model to my templates using turbogears. Here comes the trick.

How do I set up an object so that when I attempt to access certain nodes, it automatically constructs and dereferences a new URI, and makes the resulting binding available?

For example, if I have some XML describing some questions, each question might have a some answers that are associated with it.

<Questions>
	<Question>
		<Answers>
		<Answer.id>1</Answer.id>
		</Answers>
	</Question>
</Questions>

In this example, when I do something like doc.Questions.Question.Answers.Answer.Contributer, It would be great if amara dereferenced another URI to get the XML necessary for that object. Anyone know how to do this?

Advertisements

5 Comments

  1. Posted October 28, 2006 at 8:15 pm | Permalink

    Hi, I was referred to this entry. I’m glad you’re finding Amara useful. You can do what you want using a custom binding class that overrides __getattr__. Start here:

    http://uche.ogbuji.net/tech/4suite/etc/amara-manual.html#custom-binding-classes

    I’ve wanted to make binding classes work with the simplified parsing API for a while, and I just worked that into the code. It will be a bit easier in Amara 1.2 (the first alpha should be out tomorrow).

    I also think this is an interesting enough problem for me to add to the Amara demos, so if you wait a bit, I might just write the code for you :-)

  2. Posted October 30, 2006 at 4:39 am | Permalink

    I did indeed simplify the API for customizing bindings, and add a demo based on your use case. For the former see the recent 1.2a2. For the latter, see

    http://cvs.4suite.org/viewcvs/*checkout*/Amara/demo/bindery/accesstrigger.py?content-type=text%2Fplain

  3. Posted October 30, 2006 at 7:58 am | Permalink

    Uche,
    Wow, this is great! Thanks. So is this a pretty common use case? Do you know of anyone else trying to do this kind of thing? You seem to have known exactly what I meant. This stuff is so neat.

  4. Posted November 1, 2006 at 12:28 am | Permalink

    I don’t know that it’s a common use-case because I haven’t heard of anyone else asking for it, nor have I ever personally needed it. However as soon as I read your question I thought to myself “that’s a pretty neat idea”. One way I could see it’s being used is lazy loading of a huge document. Say in your example you had ten thousand questions with 3 answers each, and answer were a long (1K or more) passage. If each answer were in a separate file, you could load only the framework, and only load each answer node if someone happens to access it.

    So thinking about such ideas it realized it might not be obvious how to implement such a thing in Amara, and that having to deal with __getattr__ would make it especially hairy to figure out. That’s why I thought it would make for a good demo.

    So even if it’s not now a common use case, I hope it inspires others to try such techniques with Amara.

    Good luck.

  5. Posted November 1, 2006 at 9:00 pm | Permalink

    Uche,

    Thanks again for your wonderful work. I’m really enjoying Amara. I’m using to implement a kind of object modeling over the web. URI’s act as addresses in a huge address space, and the representations can be made into objects by dereferencing their addresses. This allows me to create native python objects where nodes in the object hierarchy are made available on demand. Awesome.


Post a Comment

Required fields are marked *
*
*

%d bloggers like this: