使用内置 type(,,) 函数创建动态模块
我可以使用 type 语句轻松创建类型;到目前为止我所拥有的看起来像这样;
#
# dynamictypes.py
#
import os, re
_requiredEnd = "-template.tex"
_packageDir = "C:\\Users\\...";
def _getClassName(name):
return re.sub("-(.)", lambda m: m.group(1).upper() , name) + "Template"
for file in os.listdir(_packageDir):
if file.lower().endswith(_requiredEnd):
_fileWithoutExt = file[:-len(_requiredEnd)]
_className = _getClassName(_fileWithoutExt)
_newClass = type(_className, (object,), dict(template=file))
print _newClass
请注意,倒数第二行创建了类型,其后面的 print 语句显示已为每个模板创建了一个类型;
<class 'dynamictypes.PandocA4BookTemplate'>
<class 'dynamictypes.PandocBookTemplate'>
<class 'dynamictypes.PandocCompactTemplate'>
但是,当我使用该模块时,我无法使用该类型;如果我写这个消费者脚本;
import dynamictypes
myTemplate = dynamictypes.PandocA4BookTemplate()
我收到此错误;
Traceback (most recent call last):
File "C:\Users\steve.cooper\Desktop\driver.py", line 3, in <module>
myTemplate = dynamictypes.PandocA4BookTemplate()
AttributeError: 'module' object has no attribute 'PandocA4BookTemplate'
你能想出一种方法让我将我创建的类型添加到模块中,使其成为模块的一流部分吗?
I'm trying to use the type(,,)
function to dynamically build a module. The module creates classes representing templates, and I need a new class for every .tex
file that lives in a particular folder. For instance, if I have a a4-page-template.tex
file, I need to create a class called A4PageTemplate
.
I can create the type easily enough using the type statement; what I have so far looks like this;
#
# dynamictypes.py
#
import os, re
_requiredEnd = "-template.tex"
_packageDir = "C:\\Users\\...";
def _getClassName(name):
return re.sub("-(.)", lambda m: m.group(1).upper() , name) + "Template"
for file in os.listdir(_packageDir):
if file.lower().endswith(_requiredEnd):
_fileWithoutExt = file[:-len(_requiredEnd)]
_className = _getClassName(_fileWithoutExt)
_newClass = type(_className, (object,), dict(template=file))
print _newClass
Note the penultimate line creates the type, and the print statement that follows it shows that a type has been created for each template;
<class 'dynamictypes.PandocA4BookTemplate'>
<class 'dynamictypes.PandocBookTemplate'>
<class 'dynamictypes.PandocCompactTemplate'>
However, when I use the module, I can't make use of the type; if I write this consumer script;
import dynamictypes
myTemplate = dynamictypes.PandocA4BookTemplate()
I get this error;
Traceback (most recent call last):
File "C:\Users\steve.cooper\Desktop\driver.py", line 3, in <module>
myTemplate = dynamictypes.PandocA4BookTemplate()
AttributeError: 'module' object has no attribute 'PandocA4BookTemplate'
Can you think of a way for me to add the type I've created to the module, so that it is a first-class part of the module?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经创建了新类并将其分配给全局变量
_newClass
,但您没有存储该变量!请注意,如果您执行dynamictypes._newClass
,您将获得创建的最终类型。在创建每个新类时,您需要创建一个变量来保存每个新类:
这会将 _className 注册为模块中的全局变量,以便您可以从外部访问它。
顺便说一句,您的正则表达式在
a4-page-template.tex
上失败 - 您得到的是a4PageTemplate
而不是A4PageTemplate
。You have created the new class and assigned it to the global variable
_newClass
, but you're not storing this variable! Notice that if you dodynamictypes._newClass
you will get the final type created.You need to make a variable to hold each new class, as you create it:
This registers _className as a global variable in the module, so that you can access it from outside.
By the way, your regex fails on
a4-page-template.tex
-- you geta4PageTemplate
instead ofA4PageTemplate
.