473,326 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

A context manager for temporary memoization.

I posted this to my blog at
http://michaelspeer.blogspot.com/200...temporary.html.

I decided to forward it onto the list for comments. I thought someone
might find it interesting.

***

This is very much a fragile hack at the moment. It's an interesting
idea I think. I was disappointed when I initially found that the
with_statement syntax does not restore the value of the `as var` upon
completion.

This made doing something along the lines of

with temporarily_memoized( func ) :
for datum in data :
func( datum )

unattainable.

Well, just a lot hackier actually.

Thus temporarily_memoized( ... ) was born :

#!/usr/bin/python

# a context manager for temporarily turning any function into
# a memoized version of itself.

from __future__ import with_statement
import contextlib , sys , types

def memoize ( func ) :
""" In haskell mutability must be handled explicitly.
Only fair that Python do the same to transparent functionality
"""
remembered = {}
def memoized ( *args ) :
""" memoized version of function """
if args in remembered :
return remembered[ args ]
else :
new = func( *args )
remembered[ args ] = new
return new
return memoized

@contextlib.contextmanager
def temporarily_memoized ( func ) :
""" memoize the given function for the duration of this block
save anything in the local variables that gets in the way of
using this so that it can be restored afterward , the memoized
version is found in the locals. use on actual functions only.
no members. """

# this is being called, there has to be a frame above it
frame = sys._getframe().f_back.f_back

if func.func_name in frame.f_locals :
f = frame.f_locals[ func.func_name ]
frame.f_locals[ func.func_name ] = memoize( func )
try :
# this hack replaces whatever in the local scope
# has the name of the variable. if you try to use
# the 'as whatever' portion of the syntax , you
# are doing it wrong
yield None
finally :
frame.f_locals[ f.func_name ] = f
else :
frame.f_locals[ func.func_name ] = memoize( func )
try :
yield None
finally :
del frame.f_locals[ func.func_name ]

def fib(n):
""" biggus fibbus """
if n == 0 or n == 1:
return n
else:
return fib(n-1) + fib(n-2)

if __name__ == '__main__' :
print fib.__doc__
with temporarily_memoized( fib ) :
print fib.__doc__
for i in xrange( 36 ) :
print "n=%d =%d" % (i, fib(i))
print fib.__doc__
print fib.__doc__

outputs :

biggus fibbus
memoized version of function
n=0 =0
......
n=35 =9227465
memoized version of function
biggus fibbus
Nov 29 '07 #1
1 1523
On Nov 29, 3:20 pm, "Michael Speer" <knome...@gmail.comwrote:
I posted this to my blog athttp://michaelspeer.blogspot.com/2007/11/context-manager-for-temporar....

I decided to forward it onto the list for comments. I thought someone
might find it interesting.

***

This is very much a fragile hack at the moment. It's an interesting
idea I think. I was disappointed when I initially found that the
with_statement syntax does not restore the value of the `as var` upon
completion.

This made doing something along the lines of

with temporarily_memoized( func ) :
for datum in data :
func( datum )

unattainable.

Well, just a lot hackier actually.

Thus temporarily_memoized( ... ) was born :

#!/usr/bin/python

# a context manager for temporarily turning any function into
# a memoized version of itself.

from __future__ import with_statement
import contextlib , sys , types

def memoize ( func ) :
""" In haskell mutability must be handled explicitly.
Only fair that Python do the same to transparent functionality
"""
remembered = {}
def memoized ( *args ) :
""" memoized version of function """
if args in remembered :
return remembered[ args ]
else :
new = func( *args )
remembered[ args ] = new
return new
return memoized

@contextlib.contextmanager
def temporarily_memoized ( func ) :
""" memoize the given function for the duration of this block
save anything in the local variables that gets in the way of
using this so that it can be restored afterward , the memoized
version is found in the locals. use on actual functions only.
no members. """

# this is being called, there has to be a frame above it
frame = sys._getframe().f_back.f_back

if func.func_name in frame.f_locals :
f = frame.f_locals[ func.func_name ]
frame.f_locals[ func.func_name ] = memoize( func )
try :
# this hack replaces whatever in the local scope
# has the name of the variable. if you try to use
# the 'as whatever' portion of the syntax , you
# are doing it wrong
yield None
finally :
frame.f_locals[ f.func_name ] = f
else :
frame.f_locals[ func.func_name ] = memoize( func )
try :
yield None
finally :
del frame.f_locals[ func.func_name ]

def fib(n):
""" biggus fibbus """
if n == 0 or n == 1:
return n
else:
return fib(n-1) + fib(n-2)

if __name__ == '__main__' :
print fib.__doc__
with temporarily_memoized( fib ) :
print fib.__doc__
for i in xrange( 36 ) :
print "n=%d =%d" % (i, fib(i))
print fib.__doc__
print fib.__doc__

outputs :

biggus fibbus
memoized version of function
n=0 =0
.....
n=35 =9227465
memoized version of function
biggus fibbus

Did you try to use temporarily_itemized inside a function? It might
not work as you think.

BTW, the name temporarily_memoized is superfluous. By definition,
whatever a context manager does is temporary, so using "temporarily"
in the name just adds a lot of letters and tells us nothing we didn't
already know.

Just call it memoized.
Carl Banks
Nov 29 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: SPG | last post by:
Hi, I am relatively new to TOMCAT, and am trying to deploy a web app using it. I have a domain name that I want to use to access my web app directly. By default, I deploy my WAR, and it deploys...
0
by: Mark E. Fenner | last post by:
In the code below, the class DifferentCache utilizes three different memoization (caching) strategies. Neither the function Memoize1 or the class Memoize2 will be adequate for all three of these...
0
by: bazzer | last post by:
hey, i am using visual basic.net 2003 and have an ASP.NET webform application thats accessing a microsoft access 2003 database. i kept getting the following error when i tried to run it: ERROR ...
0
by: bazzer | last post by:
hey, im trying to access a microsoft access database from an ASP.NET web application in visual basic 2003.NET. i get the following error when i try running it: Server Error in...
0
by: amitda | last post by:
I am getting following error while trying to access sql server report manager: I saw an msdn article about similar issue, made sure c:\temp exists and pointed TMP and TEMP sys env var to that....
3
by: Floris Bruynooghe | last post by:
Hi I was wondering when it was worthwil to use context managers for file. Consider this example: def foo(): t = False for line in file('/tmp/foo'): if line.startswith('bar'): t = True
5
by: trss | last post by:
Has anyone experienced automatic memoization by any C++ compiler before? The program coded as a solution for the problem based on the famous 3n +1 problem, both of which are given below, is...
7
by: ssecorp | last post by:
I am not clear about the results here. from timeit import Timer import Decorators def fib(n): a, b = 1, 0 while n: a, b, n = b, a+b, n-1
4
by: Daniel | last post by:
Hello, I need to access the code inside of a context manager, i.e. the call to with myManager(v=5) as x: a=b c=sin(x) should cause the following output (minus the first line, if that's...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.