xml with python <-> python deprecated - why?

F

Frank Millman

Hi all

I need to pass data between a server program and a client program, both
written in Python.

My first thought was to use xml. The server builds up an xml string,
without using any xml tools. The client uses Sax to parse the string
received. Here is an example.

The server passes to the client the widgets required to set up a
screen. This is how it tells it to display a button -

self.gui += '<button label="%s" ref="%s" enabled="%s"
default="%s"/>' \
% (label,ref,enabled,default)

This is how the client deals with this -

MyButton(self.panel,attrs['label'],int(attrs['ref']),
int(attrs['enabled']),int(attrs['default']))

I have recently read that it is not a good idea to use xml unless it is
a specific requirement, so I changed it to passing a list of
dictionaries.

The server code now looks like this -

self.gui.append((GUI_BUTTON,{'label':label,'ref':ref,
'enabled':enabled,'default':default}))

and the client code looks like this -

MyButton(self.panel,data['label'],data['ref'],
data['enabled'],data['default'])

Performance and readability are pretty much identical. The second
method results in a slightly longer string.

I am curious. Why is xml frowned upon? Is my new method ok, or is there
a better way?

Frank Millman
 
D

Duncan Booth

Frank said:
I am curious. Why is xml frowned upon? Is my new method ok, or is there
a better way?

Using XML gives you portability at the expense of increased code
manipulating the data structures.

Try creating a button with "<<<" as the label and you'll find why your xml
solution isn't ideal. For anything non-trivial you'll find it much better
to use one of the existing xml libraries to build your xml and then you'll
find why the non-xml solution looks simpler.

Generally speaking you make the decision to use xml or not based on a lot
of factors. Sometimes it is the best solution, other times using something
like a Python pickle might be better. Use whichever you are happy with
here.
 
F

Frank Millman

Duncan said:
Using XML gives you portability at the expense of increased code
manipulating the data structures.

Try creating a button with "<<<" as the label and you'll find why your xml
solution isn't ideal.

Good point.
For anything non-trivial you'll find it much better
to use one of the existing xml libraries to build your xml and then you'll
find why the non-xml solution looks simpler.

Another good point - things tend to get more complex as they evolve.
Generally speaking you make the decision to use xml or not based on a lot
of factors. Sometimes it is the best solution, other times using something
like a Python pickle might be better. Use whichever you are happy with
here.

My second method, using dictionaries instead of xml, seems equally
simple and equally fast, and it may save me some trouble in the future,
so I will run with it.

Thanks for the response

Frank
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top