Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Another method of lazy, cached evaluation.

Reply
Thread Tools

Another method of lazy, cached evaluation.

 
 
simonwittber@gmail.com
Guest
Posts: n/a
 
      01-18-2006
Recently, I needed to provide a number of game sprite classes with
references to assorted images.

I needed a class to:
- allow instances to specify which image files they need.
- ensure that a name should always refer to the same image.
- only load the image if it is actually used.

After some messing about, I came up with this:


class LazyBag(object):
def __init__(self, function):
self.__dict__["function"] = function
self.__dict__["arguments"] = {}

def __setattr__(self, key, args):
try:
existing_value = self.arguments[key]
except KeyError:
existing_value = args
if existing_value != args:
raise ValueError("Attribute must retain its initial value,
which is %s." % str(self.arguments[key]))
self.arguments[key] = args

def __getattr__(self, key):
args = self.arguments[key]
r = self.__dict__[key] = self.function(*self.arguments[key])
del self.arguments[key]
return r


This class lets me do something like this:

cache = LazyBag(Image.open)

cache.pic_1 = "data/pic_1.png"
cache.pic_2 = "data/pic_2.png"


Now, when the pic_1 and pic_2 attributes are accessed, they will return
an Image instance, which is something different to which they were
initially assigned. Is this kind of behavior bad form? Likewise, what
do people think about raising an exception during an assignment
operation?

Is there a correct name for this sort of class? 'LazyBag' doesn't sound
right...


-Sw.

 
Reply With Quote
 
 
 
 
Kamilche
Guest
Posts: n/a
 
      01-18-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Now, when the pic_1 and pic_2 attributes are accessed, they will return
> an Image instance, which is something different to which they were
> initially assigned. Is this kind of behavior bad form?


That's pretty good, I like it.

> Likewise, what
> do people think about raising an exception during an assignment
> operation?


I prefer to use the 'if key in dict' idiom, but I know some others
prefer your method.

I foresee 2 difficulties, neither earth shattering:

1. Speed might be an issue - on prior projects, my speed tests showed
that overriding getattr and setattr slowed the program down by quite a
bit.

2. I see you are caching the loaded picture, but I would choose a
different bag than __dict__. I would create a dict called cache in the
class to store it in, to avoid possible name conflicts where the stat
name has the same name as the png (which is unlikely, I realize.)

A potential enhancement would be to have some sort of cleanup, to where
if a picture is not being used by any assignment statement, it would
drop off. Or perhaps a 'force cleanup' where it clears out all the
cache, which would force the 'loadimage' routine to run again the next
time the picture is referenced.

Good job!

--Kamilche

 
Reply With Quote
 
 
 
 
Kamilche
Guest
Posts: n/a
 
      01-19-2006
If you wanted to avoid the __getattr__ __setattr__ speed hit, something
like this would work. However, you have to not mind calling a function
to get the data, instead of only getting the attribute:

class Cache(object):
_cache = {}
def __init__(self, filename):
self.filename = filename
def Get(self):
obj = self._cache.get(self.filename, None)
if not obj:
obj = self._cache[self.filename] = Load(self.filename)
return obj
@staticmethod
def Clear():
print "\nClearing cache\n"
Cache._cache = {}

class Sprite(Cache):
def Draw(self):
print self.Get()

class Sound(Cache):
def Play(self):
print self.Get()

def Load(filename):
print "********** Open '%s' here" % filename
suffix = filename.lower()[-3:]
if suffix in ['png', 'bmp', 'jpg', 'tif']:
tag = 'Image'
elif suffix in ['wav', 'mp3']:
tag = 'Sound'
return "%s data from %s" % (tag, filename)


def main():
sprite1 = Sprite('data/pic_1.png')
sprite2 = Sprite('data/pic_2.png')
sound1 = Sound('data/sound_22.wav')

sprite1.Draw()
sprite2.Draw()
sound1.Play()
sprite1.Draw()
sprite2.Draw()
sound1.Play()

Cache.Clear()

sprite1.Draw()
sprite2.Draw()
sound1.Play()
sprite1.Draw()
sprite2.Draw()
sound1.Play()

main()

 
Reply With Quote
 
simonwittber@gmail.com
Guest
Posts: n/a
 
      01-19-2006
Hmm, good ideas.

I've made some refinements and posted to the cookbook. The refinements
allow for multilple function arguments and keywords.

http://aspn.activestate.com/ASPN/Coo.../Recipe/466315

-Sw.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
my cached dataset just wont stay cached!! Craig G ASP .Net 0 03-07-2005 10:02 AM
Problem: return from one method when another method is called Magne Bergfjord Java 1 04-20-2004 04:25 PM
How can I clear cached ASPX pages from another page? Jim ASP .Net 0 04-05-2004 07:26 PM
File locked by another process when cached with CacheDependency Keith ASP .Net Web Services 2 10-02-2003 06:27 PM
File locked by another process when cached with CacheDependency Keith ASP .Net 0 10-01-2003 09:29 PM



Advertisments