Generating class definitions at runtime in memory from XSD or JSON

Discussion in 'Python' started by Stodge, Feb 17, 2012.

  1. Stodge

    Stodge Guest

    Does anyone know of a library to generate class definitions in memory,
    at runtime, from XSD or JSON? I know about PyXB, generateDS and some
    others, but they all rely on generating python source files at the
    command line, and then using those to parse XML.

    Thanks
    Stodge, Feb 17, 2012
    #1
    1. Advertising

  2. Stodge

    Nobody Guest

    On Thu, 16 Feb 2012 17:15:59 -0800, Stodge wrote:

    > Does anyone know of a library to generate class definitions in memory,
    > at runtime, from XSD or JSON? I know about PyXB, generateDS and some
    > others, but they all rely on generating python source files at the
    > command line, and then using those to parse XML.


    You don't need a library to generate classes. If the type() function is
    called with 3 arguments, it creates and returns a new class. The first
    argument is the name of the class, the second argument a tuple of base
    classes, the third argument is the class' dictionary. E.g.:

    class Foo(Bar, Baz):
    def __init__(self):
    pass

    could be written as:

    def foo_init(self):
    pass

    Foo = type('Foo', (Bar, Baz), {'__init__': foo_init})

    If you want to generate the function bodies from the contents of the
    JSON or XML file, use exec().
    Nobody, Feb 17, 2012
    #2
    1. Advertising

  3. Stodge, 17.02.2012 02:15:
    > Does anyone know of a library to generate class definitions in memory,
    > at runtime, from XSD or JSON?


    The question is: why do you want to do that? There may be other ways to do
    what you *actually* want to do, but we don't know what that is.

    Stefan
    Stefan Behnel, Feb 17, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carlos Ribeiro
    Replies:
    7
    Views:
    360
    Michael Hudson
    Nov 26, 2004
  2. Robert Brewer
    Replies:
    0
    Views:
    362
    Robert Brewer
    Nov 11, 2004
  3. Florian Frank
    Replies:
    0
    Views:
    231
    Florian Frank
    Jun 30, 2009
  4. sajuptpm
    Replies:
    2
    Views:
    317
    sajuptpm
    Dec 28, 2012
  5. Acácio Centeno
    Replies:
    1
    Views:
    241
    dieter
    Feb 15, 2013
Loading...

Share This Page