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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!dedekind.zen.co.uk!zen.net.uk!hamilton.zen.co.uk!shaftesbury.zen.co.uk.POSTED!not-for-mail
From Nobody <nobody@nowhere.com>
Subject Re: Generating class definitions at runtime in memory from XSD or JSON
Date Fri, 17 Feb 2012 10:02:27 +0000
User-Agent Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity.)
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>
MIME-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
Lines 26
Organization Zen Internet
NNTP-Posting-Host 3ce53108.news.zen.co.uk
X-Trace DXC=mSDloQVDWj<:o1:8MXjNT0nok4Z\<mH49hDU:2=XKPk8W1jQ7:12QA;QOMCU8L]Sh7HIMUmB:=[R5
X-Complaints-To abuse@zen.co.uk
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20545

Show key headers only | 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