Python-JEsMS Tutorial

This tutorial is still not complete, i plan to complete it soon, please have patience.

Using Python to develop external tools

Java is, and will be always, the main language to develop JEsMS, anyway i understand that a lot of people, interested in developing JEsMS tools, doesn't know java and doesn't want to learn it just for JEsMS.

Python is an easy and powerful programming language, it has all the benefits of a scripting language (no need of compilation and huge portability) combined with a very large user-base (which guarantee a huge set of pre-made libraries and tutorials) able to run programs going from simple batch files to complex, fully featured, window applications.

This section is not intended as a Python language introduction (just look at python website for it) but just as a tutorial in building JEsMS tools using it. Below we will build a simple end-of-season script which, given a roster, deletes all season related data (goals, assists, etc...) and increase player age while removing players older than a particular age.

Installing prerequisites

Aside from Python (which you must obviously have installed and configured) you'll need pyXML (python libraries for XML).

Examining XML Roster format

First of all have a look at Roster XML Format used by JEsMS: for each Player XML element we're interested in zeroing attributes games, saves, keyTackles, keyPasses, shots, goals, assists, dps, injury and suspension.

Then we're interested into increasing the year tag and after this we're also interested into eliminating players with an age greater of (let's say) 35 years.

pyXML usage

There are two standard ways to access XML files from a programming language: SAX (Simple API for XML) and DOM (Document Object Model). Both are supported by pyXML and below we will use DOM to make our tool.

DOM functions are contained in package xml.dom.ext.reader and therefore this package should be imported and, considering that we also need sys package for accessing files the first two rows of our program will be:

    import sys
    from xml.dom.ext.reader import Sax2

The Sax2 object, contained in xml.dom.ext.reader package is used to build a DOM in memory representation of an XML file.

Given an XML filename we just need to apply the reader to the filename as in below code:

    # create Reader object
    reader = Sax2.Reader()

    # parse the document
    doc = reader.fromStream("C:\\JESMS\\bar.xml")

Now the doc variable contains file data and can be accessed with DOM functions. To reproduce the XML stream from a DOM object (and therefore to test that all has been correctly done) you may use xml.dom.ext.PrettyPrint(doc,stream) if stream parameter is omitted data is written directly on the screen. Therefore to print the previously loaded don on the screen you just use the command:


But considering that PrettyPrint is contained in xml.dom.ext package we first need to import it with the following line:

    from xml.dom.ext import PrettyPrint

To traverse elements in an XML file pyXML uses walker objects, for example the following code snippet will print all element names contained in the file:

    from xml.dom.NodeFilter import NodeFilter

    walker = doc.createTreeWalker(doc.documentElement,
                                     NodeFilter.SHOW_ELEMENT, NONE, 0)
    while 1:
        print walker.currentNode.tagName
	next = walker.nextNode()
	if next is None: break