Python script for mobile platforms -- suggested?

Jay Loden python at jayloden.com
Tue Aug 14 20:08:01 EDT 2007


Robert Dailey wrote:
> Hi Jay,
> 
> I apologize for not having been detailed enough. Right now I'm using a
> C++ library known as TinyXML to parse my XML files. When a menu is to be
> displayed, the XML is parsed and the appropriate images, text, etc that
> will display on the menu is loaded based on the data in that XML. Note
> that the XML parsing happens before the menu is drawn and only happens
> once per menu as to avoid performance overhead during the menu drawing.

TinyXML is pretty fast, and I'd wager to say that even without knowing what the Python code would be that replaces the XML, my guess is you'll take a performance (mem/cpu or both) hit, if for no other reason than introducing the Python interpreter overhead.

> For example, to create a menu that has an image in the center of the
> screen, the XML would be this:
> 
> <Page>
>     <Frame type="Root">
>         <Frame type="Image" value="MyImage.png">
>             <Origin point="CENTER"/>
>             <Justification point="CENTER"/>
>         </Frame>
>     </Frame>
> </Page>
> 
> The "Origin" and "Justification" elements act sort of like function
> calls, and the "Frame" element defines an "Object" kind of. Each Frame
> is a visual element in the Menu.
> 
> I'm not sure if you'll agree or not, but even in this simple example
> it's very hard to read just given the nature of the XML syntax.
> Secondly, I'm not using XML as it was intended to be used. I'm using it
> as a script instead of a representation of data. Most of the real menus
> are 600+ lines of XML much like above, however it is not nearly as
> simplistic. I will paste a real example of a menu below this email for
> those curious to look at it.

XML is first and foremost a machine-parseable language, and a human-readable one second ;) I don't think this is particularly hard to read, but then I work with XML configuration files on a daily basis at work, so I may just be a terrible person to ask...

I'm not sure I agree that you're not using XML as it was intended; you're mixing data and presentation, but it's still a document containing data. That's what XHTML is, and what the XML document definitions like OOXML are all about. Anyway, that's neither here nor there. My question would be a much simpler "why is this XML?". 

XML makes sense when you need a structured document, and sometimes that makes sense for configuration options or defining a structure, but it's not clear why you'd need it here. Is this something you're intending to make editable by the end user? If you're willing to replace it with Python scripts instead of XML documents, it sounds like maybe you're not worried about letting users edit the menus. If so, why not code the menus direct into the main C++ code? This would be faster, unless you need to frequently edit these menus.

> I haven't decided on what the Python version of the example above would
> look like, that would probably happen sometime after I decided to go
> with Python for a menu scripting replacement (If I do, that is). I might
> mix XML and Python much like World of Warcraft mixes XML with LUA
> script. Again, this all depends on design. The important points I wanted
> to get across is that the Python script wont' be executed past the
> construction of a menu. The Python/XML script would be simply there to
> allow the game to create the menu and other important things.

I think the best thing to do would be to take a step back and ask what it is that you're trying to do as the end result. What are your requirements; can you expect that Python will be installed already? Is the choice between parsing XML once with C++ or having to ship a Python interpreter with associated overhead? Once you have a set of requirements and determine what's most important to you it should make it easier to pick a solution. For instance, on a mobile device, you might simply not have the spare cycles that embedding a python interpreter would require versus a lightweight lib like TinyXML. Similarly, you'd have to ask yourself if the data is always static, or if you have a need for dynamic content (i.e. embedded scripting) within the menu definition.

-Jay



More information about the Python-list mailing list