dynamic class instantiation

Ognen Duzlevski maketo at ukato.freeshell.org
Mon Jan 30 15:11:23 EST 2006


Hi, I have a "language definition" file, something along the lines of:

page ::
	name : simple
	caption : simple
	function : complex

function ::
	name : simple
	code : simple

component ::
	name : simple
	type : simple
	dataset : complex
	
etc.

On the other hand as input I have .xml files of the type:

<page>
	<name>WebPage</name>
	<caption>Browser Visible Caption</caption>
	<component>
		<name>Countrylist</name>
		<type>dropdown</type>
		<value>$dropdownlist</value>
		<function>
			<name>sqlSelect</name>
			<code>select countries 
			from blah into $dropdownlist</code>
		</function>
	</component>
</page>

I have a parser that will go through the language definition 
file and produce the following as a separate .py file:

class page(object):
	def __init__():
		self.name = None
		self.caption = None
		self.functions = []

class function(object):
	def __init__():
		self.name = None
		self.code = None

Now I want to use something like xml.dom.minidom to "parse" the 
.xml file into a set of classes defined according to the "language 
definition" file. The parse() method from the xml.dom.minidom 
package will return a document instance and I can get the node 
name from it. Say I got "page" as a string. How do I go about 
instantiating a class from this piece of information? To make it 
more obvious how do I create the page() class based on the "page" 
string I have? I want to make this generic so for me the language 
definition file will contain pages, functions, datasets etc. but 
for someone else mileage will vary.

Thanks,
Ognen



More information about the Python-list mailing list