English 中文(简体)
2. 汇辑中定义的有活力的平流层
原标题:Dynamically call python-class defined in config-file
  • 时间:2011-11-12 15:53:38
  •  标签:
  • python

I have a config.cfg which I parse using the python-module ConfigParser. In one section I want to configure assignments of the form fileextension : ClassName. Parsing results in the following dictionary:

types = {
  "extension1" : "ClassName1",
  "extension2" : "ClassName2"
}

EDIT:我现在知道:

class_ = eval(types[extension])
foo = class()

But I was given to knowledge that eval 是邪恶的,不应使用。

www.un.org/Depts/DGACM/index_spanish.htm 你们是否知道一种充满活力的聚会方式,其中哪类是档案延期的结果?

最佳回答

如果组合文档中的类别名称与贵方代码中的类别名称完全吻合(如果这些类别属于范围!),那么你可以使用<代码>eval。 (一) 只有一个字母差异的巧合? 我想不要!


A safer way to do it would be to add an extra dictionary that maps from configuration class name to python class name. I d do this because:

  • configuration files don t have to know about your code s names
  • can change config files without changing code and vice versa
  • it avoids eval

因此,它研究的是:

mappingDict = {"ClassName1" : MyPythonClass1, 
               "ClassName2" : MyPythonClass2, ... }
# keys are strings, values are classes

接着,你利用汇票卷的价值进行了一次调查:

myClassName = types[ extension1 ]
myClass = mappingDict[myClassName]
问题回答

如果<代码>module,则该模块的名称为 密名。 生命中,你可以使用同级物体

class_ = getattr(module, classname)

(如果这几类人生活在主模块中,则使用<编码> 进口 __main__,以获得本模块的标语。)

在现有模块中研究这一类别的全球范围,使用

class_ = globals()[classname]

我认为,如,Matt s response是一个更好的解决办法。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签