#1 fix for cache disabling/enabling

open
nobody
None
5
2005-01-31
2005-01-31
Steven Bethard
No

There are currently a couple of problems in the cache
disabling/enabling code due to the use of globals in
the functions that alter the cahce.

(1) disableCache doesn't disable the cache:

py> wordnet.disableCache()
py> wordnet._entityCache
<wordnet._LRUCache instance at 0x012F3D78>

Note that it's still a wordnet._LRUCache, not a
wordnet._NullCache like it should be.

(2) enableCache raises an UnboundLocalError:

py> wordnet.enableCache()
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\Program
Files\Python\lib\site-packages\wordnet.py", line 1329,
in enableCache
if not isinstance(_entityCache, LRUCache):
UnboundLocalError: local variable '_entityCache'
referenced before assignment

The quick fix is to sprinkle around some 'global'
declarations, but I'm suggesting a slight refactoring
to put the disable/enable code into the LRUCache
object. This compacts the code, and also allows you to
make the _entityCache visibile if you like, and let the
users call the cache methods though the cache object
directly instead of through the module functions.
(Personally, I'd prefer this approache because
wordnet.cache.enable(), wordnet.cache.disable(), etc.
move the cache functions into their own namespace which
they probably deserve.)

I've kept the module functions for the sake of
backwards compatibility (though now they're simple
aliases to the cache object's methods), but if you ever
have a backwards-incompatible release, I'd suggest
removing them.

Discussion

  • Steven Bethard
    Steven Bethard
    2005-01-31

    cache refactoring code

     
    Attachments