[Pywikipedia-l] SVN: [5478] trunk/pywikipedia/wikipedia.py

btongminh at svn.wikimedia.org btongminh at svn.wikimedia.org
Fri May 30 14:58:37 UTC 2008


Revision: 5478
Author:   btongminh
Date:     2008-05-30 14:58:37 +0000 (Fri, 30 May 2008)

Log Message:
-----------
Cache Family objects using WeakValueDictionary

Modified Paths:
--------------
    trunk/pywikipedia/wikipedia.py

Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py	2008-05-30 14:44:23 UTC (rev 5477)
+++ trunk/pywikipedia/wikipedia.py	2008-05-30 14:58:37 UTC (rev 5478)
@@ -128,6 +128,7 @@
 from BeautifulSoup import *
 import simplejson
 import diskcache
+import weakref
 
 # Set the locale to system default. This will ensure correct string
 # handling for non-latin characters on Python 2.3.x. For Python 2.4.x it's no
@@ -3753,8 +3754,11 @@
             found = False
     return result
 
-
-def Family(fam = None, fatal = True):
+# Warning! _familyCache does not necessarily have to be consistent between
+# two statements. Always ensure that a local reference is created when
+# accessing Family objects 
+_familyCache = weakref.WeakValueDictionary()
+def Family(fam = None, fatal = True, force = False):
     """
     Import the named family.
 
@@ -3763,6 +3767,11 @@
     """
     if fam == None:
         fam = config.family
+    
+    family = _familyCache.get(fam)
+    if family and not force:
+        return family
+        
     try:
         # search for family module in the 'families' subdirectory
         sys.path.append(config.datafilepath('families'))
@@ -3778,7 +3787,10 @@
             sys.exit(1)
         else:
             raise ValueError("Family %s does not exist" % repr(fam))
-    return myfamily.Family()
+    
+    family = myfamily.Family()
+    _familyCache[fam] = family
+    return family
 
 
 class Site(object):





More information about the Pywikipedia-l mailing list