xml with python <-> python deprecated - why?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frank Millman

    xml with python <-> python deprecated - why?

    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,enab led,default)

    This is how the client deals with this -

    MyButton(self.p anel,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,'r ef':ref,
    'enabled':enabl ed,'default':de fault}))

    and the client code looks like this -

    MyButton(self.p anel,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

  • Duncan Booth

    #2
    Re: xml with python &lt;-&gt; python deprecated - why?

    Frank Millman wrote:
    [color=blue]
    >
    > I am curious. Why is xml frowned upon? Is my new method ok, or is there
    > a better way?[/color]

    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.

    Comment

    • Frank Millman

      #3
      Re: xml with python &lt;-&gt; python deprecated - why?


      Duncan Booth wrote:[color=blue]
      > Frank Millman wrote:
      >[color=green]
      > >
      > > I am curious. Why is xml frowned upon? Is my new method ok, or is there
      > > a better way?[/color]
      >
      > 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.[/color]

      Good point.
      [color=blue]
      > 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.
      >[/color]

      Another good point - things tend to get more complex as they evolve.
      [color=blue]
      > 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.[/color]

      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

      Comment

      Working...