Basic API using XML emitter. Repeating element names and XML ordering

15 views
Skip to first unread message

Harry Wood

unread,
Feb 9, 2010, 7:26:43 AM2/9/10
to django-piston
I created a simple piston API which doesn't use any django models (no
db involved) and just does the "business logic" in the handler. So
rather than referencing '.objects' on a model, I'm directly building
up a dict to match my desired response structures. I'm a django
newbie, and python beginner so please let me know if this is the wrong
way to do things.

In the XML response I noticed that arrays were transformed into
repeating XML elements, always named <resource> That's due to the
emitter code here: http://bitbucket.org/jespern/django-piston/src/cbe9f56497ec/piston/emitters.py#cl-346
So I created a new emitter inheriting from this. I changed it so that
when it encounters an array it takes the name of the containing dict
key. So { fruit : [ 'apple', 'banana', 'pear'] } gets emitted as
<fruit>apple</fruit><fruit>banana</fruit><fruit>pear</fruit>

I'm trying to work out if this was a clever thing to do, or something
completely stupid

My next problem is that the XML elements are not output in any
particular order, which is due to dicts being unordered. I could come
up with some even more hacky solutions to this problem, but has anyone
else tackled these things? are most people are using JSON?

jespern

unread,
Feb 9, 2010, 7:45:17 AM2/9/10
to django-piston
Seeing as it's been difficult to create a versatile XML emitter, you
are correct in subclassing it.

When it comes to element ordering, if that's important to you, you can
use an ordered dict, like http://docs.python.org/dev/library/collections.html#collections.OrderedDict.

It's unfortunately new in Python 2.7, but there are a few others you
can use:

* http://www.voidspace.org.uk/python/odict.html
* http://code.activestate.com/recipes/496761/
* http://dev.pocoo.org/hg/sandbox/raw-file/tip/odict.py
* http://www.xs4all.nl/~anthon/Python/ordereddict/
* http://pypi.python.org/pypi/StableDict/0.2


HTH,

Jesper

On Feb 9, 2:26 pm, Harry Wood <harry.w...@gmail.com> wrote:
> I created a simple piston API which doesn't use any django models (no
> db involved) and just does the "business logic" in the handler. So
> rather than referencing '.objects' on a model,  I'm directly building
> up a dict to match my desired response structures. I'm a django
> newbie, and python beginner so please let me know if this is the wrong
> way to do things.
>
> In the XML response I noticed that arrays were transformed into
> repeating XML elements, always named <resource>   That's due to the

> emitter code here:http://bitbucket.org/jespern/django-piston/src/cbe9f56497ec/piston/em...

Harry Wood

unread,
Feb 9, 2010, 8:54:28 AM2/9/10
to django-piston
I wondered whether the 'fields' setting would help with ordering in
the output
http://bitbucket.org/jespern/django-piston/wiki/Documentation#fieldsexclude
Doesn't seem to have any effect on my non-model handler.

So using an OrderedDict would work.

Only thing is, later on I do plan to create some database-backed API
operations using django models. I guess I'll be snookered then because
model '.objects' always results in normal python dict (?)

I'm trying to stick to the most standard approach. Nobody else is
worrying much about output field ordering? (I know in *theory* it
shouldn't matter, but in practice it's an API readability issue at
least)


On Feb 9, 12:45 pm, jespern <jno...@gmail.com> wrote:
> Seeing as it's been difficult to create a versatile XML emitter, you
> are correct in subclassing it.
>
> When it comes to element ordering, if that's important to you, you can

> use an ordered dict, likehttp://docs.python.org/dev/library/collections.html#collections.Order....

> *http://pypi.python.org/pypi/StableDict/0.2

jespern

unread,
Feb 9, 2010, 1:29:23 PM2/9/10
to django-piston
You *can* do the sorting in your custom emitter, and that should be
compatible with any future changes you make.


Jesper

On Feb 9, 3:54 pm, Harry Wood <harry.w...@gmail.com> wrote:
> I wondered whether the 'fields' setting would help with ordering in

> the outputhttp://bitbucket.org/jespern/django-piston/wiki/Documentation#fieldse...

Reply all
Reply to author
Forward
0 new messages