def test_repr(self): d = LRUCache(3) d["a"] = 1 d["b"] = 2 d["c"] = 3 # Sort the strings - mapping is unordered assert sorted(repr(d)) == sorted("<LRUCache {'a': 1, 'b': 2, 'c': 3}>")
def create_cache(size): """Return the cache class for the given size.""" if size == 0: return None if size < 0: return {} return LRUCache(size)
def test_clear(self): d = LRUCache(3) d["a"] = 1 d["b"] = 2 d["c"] = 3 d.clear() assert d.__getstate__() == {"capacity": 3, "_mapping": {}, "_queue": deque([])}
def test_simple(self): d = LRUCache(3) d['a'] = 1 d['b'] = 2 d['c'] = 3 d['a'] d['d'] = 4
def test_pickleable(self): cache = LRUCache(2) cache['foo'] = 42 cache['bar'] = 23 cache['foo'] for protocol in range(3): copy = pickle.loads(pickle.dumps(cache, protocol))
def copy_cache(cache): """Create an empty copy of the given cache.""" if cache is None: return None elif type(cache) is dict: return {} return LRUCache(cache.capacity)
def test_simple(self): d = LRUCache(3) d["a"] = 1 d["b"] = 2 d["c"] = 3 d["a"] d["d"] = 4 assert d.keys() == ["d", "a", "c"]
def test_setdefault(self): d = LRUCache(3) assert len(d) == 0 assert d.setdefault("a") is None assert d.setdefault("a", 1) is None assert len(d) == 1 assert d.setdefault("b", 2) == 2 assert len(d) == 2
def test_itervalues(self): cache = LRUCache(3) cache["b"] = 1 cache["a"] = 2 values = [v for v in cache.itervalues()] assert len(values) == 2 assert 1 in values assert 2 in values
def test_copy(self, copy_func): cache = LRUCache(2) cache["a"] = 1 cache["b"] = 2 copy = copy_func(cache) assert copy._queue == cache._queue copy["c"] = 3 assert copy._queue != cache._queue assert "a" not in copy and "b" in copy and "c" in copy
def test_simple(self): d = LRUCache(3) d["a"] = 1 d["b"] = 2 d["c"] = 3 d["a"] d["d"] = 4 assert len(d) == 3 assert "a" in d and "c" in d and "d" in d and "b" not in d
def test_copy(self, copy_func): cache = LRUCache(2) cache['a'] = 1 cache['b'] = 2 copy = copy_func(cache) assert copy._queue == cache._queue copy['c'] = 3 assert copy._queue != cache._queue assert 'a' not in copy and 'b' in copy and 'c' in copy
def test_simple(self): d = LRUCache(3) d["a"] = 1 d["b"] = 2 d["c"] = 3 d["a"] d["d"] = 4 assert len(d) == 3 assert 'a' in d and 'c' in d and 'd' in d and 'b' not in d
def test_copy(self, copy_func): cache = LRUCache(2) cache["a"] = 1 cache["b"] = 2 copy = copy_func(cache) assert copy._queue == cache._queue copy["c"] = 3 assert copy._queue != cache._queue assert copy.keys() == ["c", "b"]
def test_pickleable(self): cache = LRUCache(2) cache["foo"] = 42 cache["bar"] = 23 cache["foo"] for protocol in range(3): copy = pickle.loads(pickle.dumps(cache, protocol)) assert copy.capacity == cache.capacity assert copy._mapping == cache._mapping assert copy._queue == cache._queue
def test_clear(self): d = LRUCache(3) d["a"] = 1 d["b"] = 2 d["c"] = 3 d.clear() assert d.__getstate__() == { 'capacity': 3, '_mapping': {}, '_queue': deque([]) }
def test_items(self): """Test various items, keys, values and iterators of LRUCache.""" d = LRUCache(3) d["a"] = 1 d["b"] = 2 d["c"] = 3 assert d.items() == [("c", 3), ("b", 2), ("a", 1)] assert d.keys() == ["c", "b", "a"] assert d.values() == [3, 2, 1] assert list(reversed(d)) == ["a", "b", "c"] # Change the cache a little d["b"] d["a"] = 4 assert d.items() == [("a", 4), ("b", 2), ("c", 3)] assert d.keys() == ["a", "b", "c"] assert d.values() == [4, 2, 3] assert list(reversed(d)) == ["c", "b", "a"]
def test_items(self): """Test various items, keys, values and iterators of LRUCache.""" d = LRUCache(3) d["a"] = 1 d["b"] = 2 d["c"] = 3 assert d.items() == list(d.iteritems()) == [('c', 3), ('b', 2), ('a', 1)] assert d.keys() == list(d.iterkeys()) == ['c', 'b', 'a'] assert d.values() == list(d.itervalues()) == [3, 2, 1] assert list(reversed(d)) == ['a', 'b', 'c'] # Change the cache a little d["b"] d["a"] = 4 assert d.items() == list(d.iteritems()) == [('a', 4), ('b', 2), ('c', 3)] assert d.keys() == list(d.iterkeys()) == ['a', 'b', 'c'] assert d.values() == list(d.itervalues()) == [4, 2, 3] assert list(reversed(d)) == ['c', 'b', 'a']
import os import sys from jinja2 import nodes from jinja2.defaults import * from jinja2.lexer import get_lexer, TokenStream from jinja2.parser import Parser from jinja2.optimizer import optimize from jinja2.compiler import generate from jinja2.runtime import Undefined, new_context from jinja2.exceptions import TemplateSyntaxError, TemplateNotFound, \ TemplatesNotFound from jinja2.utils import import_string, LRUCache, Markup, missing, \ concat, consume, internalcode, _encode_filename # for direct template usage we have up to ten living environments _spontaneous_environments = LRUCache(10) # the function to create jinja traceback objects. This is dynamically # imported on the first exception in the exception handler. _make_traceback = None def get_spontaneous_environment(*args): """Return a new spontaneous environment. A spontaneous environment is an unnamed and unaccessible (in theory) environment that is used for templates generated from a string and not from the file system. """ try: env = _spontaneous_environments.get(args) except TypeError: return Environment(*args)
On the one hand it filters out invalid operators like the bitshift operators we don't allow in templates. On the other hand it separates template code and python code in expressions. :copyright: (c) 2010 by the Jinja Team. :license: BSD, see LICENSE for more details. """ import re from operator import itemgetter from collections import deque from jinja2.exceptions import TemplateSyntaxError from jinja2.utils import LRUCache, next # cache for the lexers. Exists in order to be able to have multiple # environments with the same lexer _lexer_cache = LRUCache(50) # static regular expressions whitespace_re = re.compile(r'\s+', re.U) string_re = re.compile( r"('([^'\\]*(?:\\.[^'\\]*)*)'" r'|"([^"\\]*(?:\\.[^"\\]*)*)")', re.S) integer_re = re.compile(r'\d+') # we use the unicode identifier rule if this python version is able # to handle unicode identifiers, otherwise the standard ASCII one. try: compile('föö', '<unknown>', 'eval') except SyntaxError: name_re = re.compile(r'\b[a-zA-Z_][a-zA-Z0-9_]*\b') else:
def copy_cache(cache): if cache is None: return if type(cache) is dict: return {} return LRUCache(cache.capacity)
def test_itervalues_empty(self): cache = LRUCache(2) values = [v for v in cache.itervalues()] assert len(values) == 0
def test_values_empty(self): cache = LRUCache(2) assert cache.values() == []
def test_values(self): cache = LRUCache(3) cache["b"] = 1 cache["a"] = 2 assert cache.values() == [2, 1]
def __init__(self, ephemeral_cache_size=500): self.persistent = {} self.ephemeral = LRUCache(ephemeral_cache_size)
def create_cache(size): if size == 0: return None if size < 0: return {} return LRUCache(size)
message=e.getMessage(), column=e.getColumnNumber())) raise ValidationError(error_msg) return data # NamedTuple that represents a user's client program. Client = namedtuple('Client', ('startpage_version', 'name', 'version', 'appbuildid', 'build_target', 'locale', 'channel', 'os_version', 'distribution', 'distribution_version')) # Cache for compiled snippet templates. Using jinja's built in cache # requires either an extra trip to the database/cache or jumping through # hoops. template_cache = LRUCache(100) class SnippetBundle(object): """ Group of snippets to be sent to a particular client configuration. """ def __init__(self, client): self.client = client @cached_property def key(self): """A unique key for this bundle as a sha1 hexdigest.""" # Key should consist of snippets that are in the bundle. This part # accounts for all the properties sent by the Client, since the # self.snippets lists snippets are all filters and CMRs have been
def __init__(self, capacity: int): self.by_cwd = defaultdict(lambda: LRUCache(capacity))
def test_itervalue_deprecated(self): cache = LRUCache(3) cache["a"] = 1 cache["b"] = 2 with pytest.deprecated_call(): cache.itervalue()