views:

27

answers:

1

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?

+3  A: 

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 do dynamictypes._newClass you will get the final type created.

You need to make a variable to hold each new class, as you create it:

globals()[ _className ] = _newClass

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 get a4PageTemplate instead of A4PageTemplate.

katrielalex
That's what I was missing! I didn't know how to refer to globals.
Steve Cooper