Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20545

Re: Generating class definitions at runtime in memory from XSD or JSON

From Nobody <nobody@nowhere.com>
Subject Re: Generating class definitions at runtime in memory from XSD or JSON
Date 2012-02-17 10:02 +0000
Message-Id <pan.2012.02.17.10.02.26.887000@nowhere.com>
Newsgroups comp.lang.python
References <5cf03bed-8173-4910-80d1-5716334a6184@z9g2000vbv.googlegroups.com>
Organization Zen Internet

Show all headers | View raw


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().

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Generating class definitions at runtime in memory from XSD or JSON Stodge <stodge@gmail.com> - 2012-02-16 17:15 -0800
  Re: Generating class definitions at runtime in memory from XSD or JSON Nobody <nobody@nowhere.com> - 2012-02-17 10:02 +0000
  Re: Generating class definitions at runtime in memory from XSD or JSON Stefan Behnel <stefan_ml@behnel.de> - 2012-02-17 13:57 +0100

csiph-web