# HG changeset patch # User dclinton@b35c0ba3-1128-0410-9219-0b39014e361d # Date 1247339963 0 # Branch dclinton # Node ID 107dcadcf7e72175bb0dd9bf87a2f3f365a662b8 # Parent adedffd9839626034a1685dfb6acb4e904799520 Applies the DEFAULT_CACHE patch from r176 in trunk to branches/dclinton diff -r adedffd9839626034a1685dfb6acb4e904799520 -r 107dcadcf7e72175bb0dd9bf87a2f3f365a662b8 twitter.py --- a/twitter.py Sun Jun 21 04:30:20 2009 +0000 +++ b/twitter.py Sat Jul 11 19:19:23 2009 +0000 @@ -43,6 +43,9 @@ CHARACTER_LIMIT = 140 +# A singleton representing a lazily instantiated FileCache. +DEFAULT_CACHE = object() + class TwitterError(Exception): '''Base class for Twitter errors''' @@ -319,7 +322,8 @@ username=None, password=None, input_encoding=None, - request_headers=None): + request_headers=None, + cache=DEFAULT_CACHE): '''Instantiate a new twitter.Api object. Args: @@ -327,8 +331,11 @@ password: The password for the twitter account. [optional] input_encoding: The encoding used to encode input strings. [optional] request_header: A dictionary of additional HTTP request headers. [optional] + cache: + The cache instance to use. Defaults to DEFAULT_CACHE. Use + None to disable caching. [optional] ''' - self._cache = _FileCache() + self.SetCache(cache) self._urllib = urllib2 self._cache_timeout = Api.DEFAULT_CACHE_TIMEOUT self._InitializeRequestHeaders(request_headers) @@ -949,9 +956,12 @@ '''Override the default cache. Set to None to prevent caching. Args: - cache: an instance that supports the same API as the twitter._FileCache + cache: an instance that supports the same API as the twitter._FileCache ''' - self._cache = cache + if cache == DEFAULT_CACHE: + self._cache = _FileCache() + else: + self._cache = cache def SetUrllib(self, urllib): '''Override the default urllib implementation. diff -r adedffd9839626034a1685dfb6acb4e904799520 -r 107dcadcf7e72175bb0dd9bf87a2f3f365a662b8 twitter_test.py --- a/twitter_test.py Sun Jun 21 04:30:20 2009 +0000 +++ b/twitter_test.py Sat Jul 11 19:19:23 2009 +0000 @@ -306,8 +306,7 @@ def setUp(self): self._urllib = MockUrllib() - api = twitter.Api(username='test', password='test') - api.SetCache(NullCache()) + api = twitter.Api(username='test', password='test', cache=None) api.SetUrllib(self._urllib) self._api = api @@ -543,23 +542,6 @@ # TODO(dewitt): Add verification that the proper args are passed pass - -class NullCache(object): - '''A no-op replacement for the cache class''' - - def Get(self, key): - return None - - def Set(self, key, data): - pass - - def Remove(self, key): - pass - - def GetCachedTime(self, key): - return None - - class curry: # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52549