コード例 #1
0
ファイル: test_commons.py プロジェクト: rpatil524/StrepHit
    def test_collisions(self):
        def collision_hash(key):
            if key in {'key-1', 'key-2'}:
                return 'the same hashed value'
            else:
                return self.cache_hash_for(key)
        cache._hash_for = collision_hash

        cache.set('key-1', 'value-1')
        cache.set('key-2', 'value-2')

        self.assertEqual(cache.get('key-1'), 'value-1')
        self.assertEqual(cache.get('key-2'), 'value-2')
コード例 #2
0
ファイル: io.py プロジェクト: rpatil524/StrepHit
def get_and_cache(url, use_cache=True, **kwargs):
    """
    Perform an HTTP GET request to the given url and optionally cache the
    result somewhere in the file system. The cached content will be used
    in the subsequent requests.
    Raises all HTTP errors

    :param url: URL of the page to retrieve
    :param use_cache: Whether to use cache
    :param \*\*kwargs: keyword arguments to pass to `requests.get`
    :return: The content page at the given URL, unicode
    """
    if not use_cache:
        r = requests.get(url, **kwargs)
        r.raise_for_status()
        content = r.text
    else:
        key = url + json.dumps(kwargs)
        content = cache.get(key)
        if content is None:
            content = get_and_cache(url, use_cache=False, **kwargs)
            cache.set(key, content)
    return content
コード例 #3
0
ファイル: io.py プロジェクト: Wikidata/StrepHit
def get_and_cache(url, use_cache=True, **kwargs):
    """
    Perform an HTTP GET request to the given url and optionally cache the
    result somewhere in the file system. The cached content will be used
    in the subsequent requests.
    Raises all HTTP errors

    :param url: URL of the page to retrieve
    :param use_cache: Whether to use cache
    :param \*\*kwargs: keyword arguments to pass to `requests.get`
    :return: The content page at the given URL, unicode
    """
    if not use_cache:
        r = requests.get(url, **kwargs)
        r.raise_for_status()
        content = r.text
    else:
        key = url + json.dumps(kwargs)
        content = cache.get(key)
        if content is None:
            content = get_and_cache(url, use_cache=False, **kwargs)
            cache.set(key, content)
    return content
コード例 #4
0
ファイル: test_commons.py プロジェクト: rpatil524/StrepHit
 def test_objects(self):
     obj = {'this': ['is', ['a']], 'complex': 'object'}
     cache.set('obj', obj)
     self.assertEqual(obj, cache.get('obj'))
コード例 #5
0
ファイル: test_commons.py プロジェクト: rpatil524/StrepHit
 def test_enabled(self):
     cache.ENABLED = False
     cache.set('key', 'value')
     self.assertEqual(cache.get('key', 'something else'), 'something else')
     cache.ENABLED = True
コード例 #6
0
ファイル: test_commons.py プロジェクト: rpatil524/StrepHit
 def test_unicode(self):
     cache.set(u'\u84c4\u3048\u3066 \u304f\u3060\u3055\u3044',
               u'\u304a\u75b2\u308c\u3055\u307e')
     self.assertEqual(cache.get(u'\u84c4\u3048\u3066 \u304f\u3060\u3055\u3044'),
                      u'\u304a\u75b2\u308c\u3055\u307e')
コード例 #7
0
ファイル: test_commons.py プロジェクト: rpatil524/StrepHit
 def test_set_overwrite(self):
     cache.set('key', 'value')
     cache.set('key', 'another value', overwrite=True)
     self.assertEqual(cache.get('key'), 'another value')
     cache.set('key', 'something else', overwrite=False)
     self.assertEqual(cache.get('key'), 'another value')
コード例 #8
0
ファイル: test_commons.py プロジェクト: rpatil524/StrepHit
 def test_simple_set(self):
     cache.set('key', 'value')
     self.assertEqual(cache.get('key'), 'value')
コード例 #9
0
ファイル: test_commons.py プロジェクト: rpatil524/StrepHit
 def test_simple_get(self):
     self.assertIsNone(cache.get('non existing'))
     self.assertEqual(cache.get('non existing', 'default'), 'default')