コード例 #1
0
 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([])}
コード例 #2
0
ファイル: test_utils.py プロジェクト: TheVinhLuong102/jinja
 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"]
コード例 #3
0
ファイル: test_utils.py プロジェクト: xblue-light/jinja
 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
コード例 #4
0
ファイル: test_utils.py プロジェクト: TheVinhLuong102/jinja
 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
コード例 #5
0
ファイル: test_utils.py プロジェクト: xblue-light/jinja
 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([])
     }
コード例 #6
0
ファイル: test_utils.py プロジェクト: TheVinhLuong102/jinja
 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}>")
コード例 #7
0
ファイル: db.py プロジェクト: RonnyPfannschmidt/lektor
class RecordCache(object):
    """The record cache holds records eitehr in an persistent or ephemeral
    section which helps the pad not load records it already saw.
    """

    def __init__(self, ephemeral_cache_size=500):
        self.persistent = {}
        self.ephemeral = LRUCache(ephemeral_cache_size)

    def _get_cache_key(self, record_or_path, alt=PRIMARY_ALT, page_num=None):
        if isinstance(record_or_path, basestring):
            path = record_or_path
        else:
            path = record_or_path['_path']
            alt = record_or_path.alt
            page_num = record_or_path.page_num
        rv = path
        if alt != PRIMARY_ALT:
            rv = '%s+%s' % (rv, alt)
        if page_num is not None:
            rv = '%s@%s' % (rv, page_num)
        return rv

    def is_persistent(self, record):
        """Indicates if a record is in the persistent record cache."""
        cache_key = self._get_cache_key(record)
        return cache_key in self.persistent

    def remember(self, record):
        """Remembers the record in the record cache."""
        cache_key = self._get_cache_key(record)
        if cache_key not in self.persistent and cache_key not in self.ephemeral:
            self.ephemeral[cache_key] = record

    def persist(self, record):
        """Persists a record.  This will put it into the persistent cache."""
        cache_key = self._get_cache_key(record)
        self.persistent[cache_key] = record
        try:
            del self.ephemeral[cache_key]
        except KeyError:
            pass

    def persist_if_cached(self, record):
        """If the record is already ephemerally cached, this promotes it to
        the persistent cache section.
        """
        cache_key = self._get_cache_key(record)
        if cache_key in self.ephemeral:
            self.persist(record)

    def get(self, path, alt=PRIMARY_ALT, page_num=None):
        """Looks up a record from the cache."""
        cache_key = self._get_cache_key(path, alt, page_num)
        rv = self.persistent.get(cache_key)
        if rv is not None:
            return rv
        rv = self.ephemeral.get(cache_key)
        if rv is not None:
            return rv
コード例 #8
0
 def test_simple(self):
     d = LRUCache(3)
     d['a'] = 1
     d['b'] = 2
     d['c'] = 3
     d['a']
     d['d'] = 4
コード例 #9
0
 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))
コード例 #10
0
def create_cache(size):
    """Return the cache class for the given size."""
    if size == 0:
        return None
    if size < 0:
        return {}
    return LRUCache(size)
コード例 #11
0
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)
コード例 #12
0
ファイル: test_utils.py プロジェクト: TheVinhLuong102/jinja
 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"]
コード例 #13
0
 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
コード例 #14
0
ファイル: test_utils.py プロジェクト: ywang021/catboost
 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
コード例 #15
0
ファイル: cache.py プロジェクト: cratejoy/flask-experiment
    def __getitem__(self, key):
        """Get an item from the cache. Moves the item up so that it has the
        highest priority then.

        Raise an `KeyError` if it does not exist.
        """
        #print "Calling parent for __get__", self.experiment_key(key)
        #print self._mapping
        return LRUCache.__getitem__(self, self.experiment_key(key))
コード例 #16
0
ファイル: test_utils.py プロジェクト: xblue-light/jinja
 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
コード例 #17
0
 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
コード例 #18
0
ファイル: test_utils.py プロジェクト: ywang021/catboost
    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
コード例 #19
0
ファイル: test_utils.py プロジェクト: TheVinhLuong102/jinja
    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"]
コード例 #20
0
ファイル: db.py プロジェクト: jab/lektor
class RecordCache(object):
    """The record cache holds records eitehr in an persistent or ephemeral
    section which helps the pad not load records it already saw.
    """

    def __init__(self, ephemeral_cache_size=500):
        self.persistent = {}
        self.ephemeral = LRUCache(ephemeral_cache_size)

    def is_persistent(self, record):
        """Indicates if a record is in the persistent record cache."""
        return record['_path'] in self.persistent

    def remember(self, record):
        """Remembers the record in the record cache."""
        cache_key = record['_path']
        if cache_key not in self.persistent and cache_key not in self.ephemeral:
            self.ephemeral[cache_key] = record

    def persist(self, record):
        """Persists a record.  This will put it into the persistent cache."""
        cache_key = record['_path']
        self.persistent[cache_key] = record
        try:
            del self.ephemeral[cache_key]
        except KeyError:
            pass

    def persist_if_cached(self, record):
        """If the record is already ephemerally cached, this promotes it to
        the persistent cache section.
        """
        if record['_path'] in self.ephemeral:
            self.persist(record)

    def __getitem__(self, key):
        rv = self.persistent.get(key)
        if rv is not None:
            return rv
        rv = self.ephemeral.get(key)
        if rv is not None:
            return rv
コード例 #21
0
ファイル: db.py プロジェクト: jab/lektor
class RecordCache(object):
    """The record cache holds records eitehr in an persistent or ephemeral
    section which helps the pad not load records it already saw.
    """
    def __init__(self, ephemeral_cache_size=500):
        self.persistent = {}
        self.ephemeral = LRUCache(ephemeral_cache_size)

    def is_persistent(self, record):
        """Indicates if a record is in the persistent record cache."""
        return record['_path'] in self.persistent

    def remember(self, record):
        """Remembers the record in the record cache."""
        cache_key = record['_path']
        if cache_key not in self.persistent and cache_key not in self.ephemeral:
            self.ephemeral[cache_key] = record

    def persist(self, record):
        """Persists a record.  This will put it into the persistent cache."""
        cache_key = record['_path']
        self.persistent[cache_key] = record
        try:
            del self.ephemeral[cache_key]
        except KeyError:
            pass

    def persist_if_cached(self, record):
        """If the record is already ephemerally cached, this promotes it to
        the persistent cache section.
        """
        if record['_path'] in self.ephemeral:
            self.persist(record)

    def __getitem__(self, key):
        rv = self.persistent.get(key)
        if rv is not None:
            return rv
        rv = self.ephemeral.get(key)
        if rv is not None:
            return rv
コード例 #22
0
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)
コード例 #23
0
ファイル: lexer.py プロジェクト: AndroidTamer/Anaconda
    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:
コード例 #24
0
ファイル: db.py プロジェクト: jab/lektor
 def __init__(self, ephemeral_cache_size=500):
     self.persistent = {}
     self.ephemeral = LRUCache(ephemeral_cache_size)
コード例 #25
0
ファイル: environment.py プロジェクト: R4M80MrX/eve-1
def create_cache(size):
    if size == 0:
        return None
    if size < 0:
        return {}
    return LRUCache(size)
コード例 #26
0
ファイル: environment.py プロジェクト: R4M80MrX/eve-1
def copy_cache(cache):
    if cache is None:
        return
    if type(cache) is dict:
        return {}
    return LRUCache(cache.capacity)
コード例 #27
0
ファイル: db.py プロジェクト: RonnyPfannschmidt/lektor
 def __init__(self, ephemeral_cache_size=500):
     self.persistent = {}
     self.ephemeral = LRUCache(ephemeral_cache_size)
コード例 #28
0
ファイル: test_utils.py プロジェクト: xblue-light/jinja
 def test_itervalues_empty(self):
     cache = LRUCache(2)
     values = [v for v in cache.itervalues()]
     assert len(values) == 0
コード例 #29
0
ファイル: cache.py プロジェクト: cratejoy/flask-experiment
 def __contains__(self, key):
     """Check if a key exists in this cache."""
     #print "Calling parent for contains", self.experiment_key(key)
     return LRUCache.contains(self, self.experiment_key(key))
コード例 #30
0
ファイル: test_utils.py プロジェクト: TheVinhLuong102/jinja
 def test_values_empty(self):
     cache = LRUCache(2)
     assert cache.values() == []
コード例 #31
0
ファイル: test_utils.py プロジェクト: TheVinhLuong102/jinja
 def test_values(self):
     cache = LRUCache(3)
     cache["b"] = 1
     cache["a"] = 2
     assert cache.values() == [2, 1]
コード例 #32
0
ファイル: cache.py プロジェクト: cratejoy/flask-experiment
 def __setitem__(self, key, value):
     """Sets the value for an item. Moves the item up so that it
     has the highest priority then.
     """
     #print "Calling parent for __set__", self.experiment_key(key)
     return LRUCache.__setitem__(self, self.experiment_key(key), value)
コード例 #33
0
ファイル: cache.py プロジェクト: cratejoy/flask-experiment
 def __delitem__(self, key):
     """Remove an item from the cache dict.
     Raise an `KeyError` if it does not exist.
     """
     return LRUCache.__delitem__(self, self.experiment_key(key))
コード例 #34
0
ファイル: templates.py プロジェクト: mdrachuk/lightweight
 def __init__(self, capacity: int):
     self.by_cwd = defaultdict(lambda: LRUCache(capacity))
コード例 #35
0
ファイル: models.py プロジェクト: pmac/snippets-service
                       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
コード例 #36
0
ファイル: test_utils.py プロジェクト: xblue-light/jinja
    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']
コード例 #37
0
ファイル: test_utils.py プロジェクト: xblue-light/jinja
 def test_itervalue_deprecated(self):
     cache = LRUCache(3)
     cache["a"] = 1
     cache["b"] = 2
     with pytest.deprecated_call():
         cache.itervalue()