Created
November 4, 2010 19:32
-
-
Save lbjay/663036 to your computer and use it in GitHub Desktop.
python memoize decorator example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def memoize(fn): | |
memory = {} | |
def simple_memoizer(*param_tuple): | |
if param_tuple in memory: | |
return memory[param_tuple] | |
else: | |
memory[param_tuple] = result = fn(*param_tuple) | |
return result | |
return simple_memoizer | |
@memoize | |
def normalize_author(author): | |
try: | |
normalized = NormalizeAuthor(author, uh=UH) # pass in our own UnicodeHandler obj | |
except: | |
log.error("Failed normalizing author name %s" % author) | |
normalized = author | |
return normalized | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment