473,399 Members | 4,254 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,399 software developers and data experts.

Help Understanding weakref (lazyloader)

I found this script at http://www.pygame.org/wiki/LazyImage...arent=CookBook
And I can't quite figure out how it works. I was wondering if someone
could clarify it for me.
The Code is:

import pygame
import weakref

class ResourceController(object):
def __init__(self, loader):
self.__dict__.update(dict(
names = {},
cache = weakref.WeakValueDictionary(),
loader = loader
))

def __setattr__(self, name, value):
self.names[name] = value

def __getattr__(self, name):
try:
img = self.cache[name]
except KeyError:
img = self.loader(self.names[name])
self.cache[name] = img
return img
class ImageController(ResourceController):
def __init__(self):
ResourceController.__init__(self, pygame.image.load)
My question is why does the __setattr__ set the value to
self.names[name] instead of self.cache[name]?
Sep 4 '08 #1
1 1349
On Sep 3, 10:46*pm, Ripter <Ripter...@gmail.comwrote:
I found this script athttp://www.pygame.org/wiki/LazyImageLoading?parent=CookBook
And I can't quite figure out how it works. I was wondering if someone
could clarify it for me.
The Code is:

import pygame
import weakref

class ResourceController(object):
* * def __init__(self, loader):
* * * * self.__dict__.update(dict(
* * * * * * names = {},
* * * * * * cache = weakref.WeakValueDictionary(),
* * * * * * loader = loader
* * * * ))

* * def __setattr__(self, name, value):
* * * * self.names[name] = value

* * def __getattr__(self, name):
* * * * try:
* * * * * * img = self.cache[name]
* * * * except KeyError:
* * * * * * img = self.loader(self.names[name])
* * * * * * self.cache[name] = img
* * * * return img

class ImageController(ResourceController):
* * def __init__(self):
* * * * ResourceController.__init__(self, pygame.image.load)

My question is why does the __setattr__ set the value to
self.names[name] instead of self.cache[name]?
I feel stupid, I figured it out. (Don't program while watching Back to
the Future)
The reason would be the 'lazy' part. self.names holds the file name,
and when you get the property back (__getattr__) it checks to see if
it already got the value or not, and uses the name to get the value if
it hasn't already.

Duh. Sorry.
Sep 4 '08 #2

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

Similar topics

1
by: Ames Andreas (MPA/DF) | last post by:
Hi all, I'm using python 2.1 and can't easily upgrade (Zope). I'm using the Queue module to synchronize/communicate between two threads and weakref.proxy objects to avoid cycles. Scenario: ...
1
by: Mathias Mamsch | last post by:
Hi, I have some confusion concerning the weakref module. I am trying to save a weak reference to a bound member function of a class instance for using it as a callback function. But I always...
2
by: Mike C. Fletcher | last post by:
I'm looking at rewriting parts of Twisted and TwistedSNMP to eliminate __del__ methods (and the memory leaks they create). Looking at the docs for 2.3's weakref.ref, there's no mention of whether...
2
by: Chua Wen Ching | last post by:
Hi, Is there any place that i can learn the basics of IL and using ILDASM. I saw the internet but there is not much to see and read. Maybe i didn't search deep enough. I prefer if someone can...
0
by: bearophileHUGS | last post by:
This is yet another memoize decorator, it's meant to be resilient (and fast enough): http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/466320 Like most memoize decorators it stores the...
3
by: TonyHa | last post by:
Hello I try to install boost_1_33_1 on RedHat Linux. I have downloaded boost into /user/dtgtools/tmp and I try to install it into /user/dtgtools/packages/boost. I use the following commands:...
2
by: John Nagle | last post by:
Is there some way to get a strong ref to the original object back from a weakref proxy object? I can't find any Python function to do this. ".ref()" doesn't work on proxy objects. John Nagle
3
by: Odalrick | last post by:
I'm trying to write a simple game and decided I need an eventmanager. <code> import weakref from collections import defaultdict class _EventManager( object ): def __init__( self ):...
6
by: George Sakkis | last post by:
I'm baffled with a situation that involves: 1) an instance of some class that defines __del__, 2) a thread which is created, started and referenced by that instance, and 3) a weakref proxy to the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.