Another method of lazy, cached evaluation.

Discussion in 'Python' started by simonwittber@gmail.com, Jan 18, 2006.

  1. Guest

    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.
    , Jan 18, 2006
    #1
    1. Advertising

  2. Kamilche Guest

    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
    Kamilche, Jan 18, 2006
    #2
    1. Advertising

  3. Kamilche Guest

    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()
    Kamilche, Jan 19, 2006
    #3
  4. Guest

    , Jan 19, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Craig G
    Replies:
    0
    Views:
    385
    Craig G
    Mar 7, 2005
  2. sturlamolden
    Replies:
    9
    Views:
    744
    Antoon Pardon
    May 3, 2007
  3. Ken Pu
    Replies:
    3
    Views:
    671
    Steven D'Aprano
    Jan 16, 2009
  4. Boris Borcic
    Replies:
    0
    Views:
    548
    Boris Borcic
    Jan 16, 2009
  5. Boris Borcic
    Replies:
    0
    Views:
    545
    Boris Borcic
    Jan 16, 2009
Loading...

Share This Page