动态类实例化 [英] dynamic class instantiation
问题描述
我有一个语言定义。文件,类似于:
页面::
名称:简单
标题:简单
功能:复杂
功能::
名称:简单
代码:简单
>
组件::
名称:简单
类型:简单
数据集:复杂
等
另一方面,作为输入,我有.xml类型的文件:
< page>
< name> WebPage< / name>
< 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>
我有一个解析器,它将通过语言定义
文件并生成以下单独的.py文件:
类页面(对象):
def __init __():
self.name =无
self.caption =无
self.functions = []
类函数(对象):
def __init __():
self.name =无
self.code =无
现在我想使用像xml.dom.minidom这样的东西来解析将
..xml文件放入根据语言
定义定义的一组类中。文件。 xml.dom.minidom
包中的parse()方法将返回一个文档实例,我可以从中获取节点
名称。说我得到了页面作为一个字符串。我如何从这条信息中实例化一个类?
?为了使它更明显我如何基于页面创建page()类?
字符串我有?我想制作这个通用的,所以对我来说语言
定义文件将包含页面,函数,数据集等但是
对于其他人来说里程会有所不同。
谢谢,
Ognen
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
推荐答案
dropdownlist< / value>
< function>
< name> sqlSelect< / name>
< code>选择国家
从blah into
dropdownlist</value>
<function>
<name>sqlSelect</name>
<code>select countries
from blah into
下拉列表< / code>
< / function>
< / component>
< ; / page>
我有一个解析器将通过语言定义
文件并生成以下单独的.py文件:
班级页面(对象):
def __init __():
self.name =无
self .caption =无
self.functions = []
类函数(对象):
def __init __():
self.name =无
self.code =没有
现在我想使用像xml.dom.minidom这样的东西来解析将
..xml文件放入根据语言
定义定义的一组类中。文件。 xml.dom.minidom
包中的parse()方法将返回一个文档实例,我可以从中获取节点
名称。说我得到了页面作为一个字符串。我如何从这条信息中实例化一个类?
?为了使它更明显我如何基于页面创建page()类?
字符串我有?我想制作这个通用的,所以对我来说语言
定义文件将包含页面,函数,数据集等但是
对于其他人来说里程会有所不同。
谢谢,
Ognen
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
Ognen Duzlevski写道:
Ognen Duzlevski wrote:
我有语言定义文件,东西就行了:
page ::
名称:简单说明:简单
功能:复杂
功能: :
名称:简单
代码:简单
组件::
名称:简单
类型:简单
数据集:complex
另一方面,作为输入,我有.xml文件的类型:
< page>
< name> WebPage< / name>
< caption>浏览器可见标题< / caption>
< component>
< name> Countrylist< / name>
< type> dropdown< ; / type>
< value>
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>
这篇关于动态类实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!