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?