Another method of lazy, cached evaluation.

S

simonwittber

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.
 
K

Kamilche

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
 
K

Kamilche

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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top