| Although XMLObject automatically generates a Python parser for you, you may wish to edit the parser to enhance its functionality. One time that you'll certainly want to do this is when you derive the parser classes from other classes. Consider the painfully simple example: This will create the following parser: Example 4.1. Parent class Parser - 1 # vvv Generated code, do not modify vvv import XMLObject # ^^^ Generated code, do not modify ^^^ # vvv Generated code, do not modify vvv class Root(Parent): ChildSpec = XMLObject.ChildClass({}, "") def __init__(self, Attr, XMLStack=XMLObject.Stack()): self.Attr = str(Attr) # ^^^ Generated code, do not modify ^^^ This code won't load, of course, since we haven't yet defined the Parent class just yet. We can do this by editing the code and either entering the class into the module or by importing it with a: from ParentModule import Parent For now, we'll just add code directly into the parser: Example 4.2. Parent class Parser - 2 # vvv Generated code, do not modify vvv import XMLObject # ^^^ Generated code, do not modify ^^^ class Parent: def __init__(self): self.Var = 5 # vvv Generated code, do not modify vvv class Root(Parent): ChildSpec = XMLObject.ChildClass({}, "") def __init__(self, Attr, XMLStack=XMLObject.Stack()): self.Attr = str(Attr) # ^^^ Generated code, do not modify ^^^ Parent.__init__(self) Note how I've added the Parent class and the Parent class construction without modifying any code within the blocks marked "do not modify". This is important. Modifying the code within these blocks can still create valid code, but you may no longer be able to reload the code with XMLObjApp. Worse, the application may load the code, but then save changes to these blocks without any warnings. Just don't do it. You can also add member functions to classes, as long as the code you add is not within the "do not modify" blocks. XMLObject processes the XML file from top to bottom, and performs the following actions on each element in order:
_end_init accepts no parameters. It gives you a chance to do any initialization that cannot happen until after the element's children have been processed. |