passed extra info to repl function

Michael P. Soulier msoulier at nortelnetworks.com
Mon Oct 29 14:08:21 EST 2001


    Hello. 

    I'm using Python's re.sub() function, with the replace part of the call
being a function, to parse a template. The idea is to have a dictionary of
keywords with the corresponding replacement strings, and then doing one big
call of re.sub(), to replace all keywords with their replacement strings. 

    Here is what I have so far:

    def parse(self, file, dict):
        """parse(file, dict) -> string
        This method parses the open file object passed, replacing any keys
        found using the replacement dictionary passed."""
        global replacement_dict
        if type(file) != types.FileType:
            raise TypeError, "First argument must be a file object"
        elif type(dict) != types.DictType:
            raise TypeError, "Second argument must be a dictionary"
        # Define a nested function to use in the parsing.
        def replace(match):
            """replace(match) -> string This function's purpose is to replace
            the string grouped by the parenthesis in the regular expression of
            the parse function, by the corresponding string in the replacement
            dictionary, if there is one."""
            global replacement_dict
            key = match.group(1)
            if replacement_dict.has_key(key):
                return replacement_dict[key]
            else:
                return ''
        # Read in the file and perform the replacement.
        replacement_dict = dict
        # Read in the entire template into memory. I guess we'd better keep
        # the templates a reasonable size if we're going to keep doing this.
        buffer = file.read()
        replaced = re.sub("%%(\w+)%%", replace, buffer)
        return replaced

Now, replacement_dict is obviously a global in this module. That's the
problem. I need it because I can't pass any extra information to replace, as
it only accepts a match object. Unfortunatly, using a single global like this
prevents the user of this class from instantiating multiple instances of this
object without potentially conflicting with overlapping use of the global
replacement_dict variable. 

    I'm using Python 1.5.2, so I can't use nested scopes to get around this.
I'd like replace to be able to access an instance variable of
self.replacement_dict, so that all replacement dictionaries are unique to the
class instance, but the scoping rules prevent replace from seeing the self
reference. 

    Any ideas from the experienced?

    Thanks,

    Mike

-- 
Michael P. Soulier, TD12, SKY  Tel: 613-765-4699 (ESN: 39-54699)
Optical Networks, Nortel Networks, SDE Pegasus
"...the word HACK is used as a verb to indicate a massive amount
of nerd-like effort."  -Harley Hahn, A Student's Guide to Unix



More information about the Python-list mailing list