Download A Python Book - Dave Kuhlman dot org
Transcript
A Python Book (method exportLiteral). Each generated class contains the following: A constructor method (__init__), with member variable initializers. ● Methods with names get_xyz and set_xyz for each member variable "xyz" or, if the member variable is defined with maxOccurs="unbounded", methods with names get_xyz, set_xyz, add_xyz, and insert_xyz. (Note: If you use the useoldgettersetter, then you will get methods with names like getXyz and setXyz.) ● A build method that can be used to populate an instance of the class from a node in an ElementTree or Lxml tree. ● An export method that will write the instance (and any nested subinstances) to a file object as XML text. ● An exportLiteral method that will write the instance (and any nested subinstances) to a file object as Python literals (text). The generated subclass file contains one (sub)class definition for each data representation class. If the subclass file is used, then the parser creates instances of the subclasses (instead of creating instances of the superclasses). This enables the user to extend the subclasses with "tree walk" methods, for example, that process the contents of the XML file. The user can also generate and extend multiple subclass files which use a single, common superclass file, thus implementing a number of different processes on the same XML document type. ● This document introduces the user to generateDS.py and walks the user through several examples that show how to generate Python code and how to use that generated code. 4.2 Generating the code Note: The sample files used below are under the tutorial/Code/ directory. Use the following to get help: $ generateDS.py help I'll assume that generateDS.py is in a directory on your path. If not, you should do whatever is necessary to make it accessible and executable. Here is a simple XML schema document: And, here is how you might generate classes and subclasses that provide data bindings (a Python API) for the definitions in that schema: $ generateDS.py o people_api.py s people_sub.py people.xsd Page 261