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. Advertisements

  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. Advertisements

  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. Advertisements

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:
    454
    Michael Hudson
    Nov 26, 2004
  2. Adam dR.
    Replies:
    2
    Views:
    557
    Adam dR.
    Oct 16, 2007
  3. johkar
    Replies:
    0
    Views:
    2,296
    johkar
    Dec 16, 2009
  4. Florian Frank
    Replies:
    0
    Views:
    457
    Florian Frank
    Jun 30, 2009
  5. sajuptpm
    Replies:
    2
    Views:
    698
    sajuptpm
    Dec 28, 2012
  6. Acácio Centeno
    Replies:
    1
    Views:
    608
    dieter
    Feb 15, 2013
  7. Bryan Britten
    Replies:
    9
    Views:
    524
    Bryan Britten
    May 28, 2013
  8. David Karr
    Replies:
    1
    Views:
    329
    David Karr
    Jun 17, 2013
Loading...