File size: 1,606 Bytes
ab4488b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
"""
Node Searching with Cache.
.. note:: These functions require https://pypi.org/project/fastcache/, otherwise caching is not active.
"""
from . import search
# fastcache is optional
try:
from fastcache import clru_cache as _cache
except ImportError:
from functools import wraps
# dummy decorator which does NOT cache
def _cache(size):
# pylint: disable=W0613
def decorator(func):
@wraps(func)
def wrapped(*args, **kwargs):
return func(*args, **kwargs)
return wrapped
return decorator
CACHE_SIZE = 32
@_cache(CACHE_SIZE)
def findall(node, filter_=None, stop=None, maxlevel=None, mincount=None, maxcount=None):
"""Identical to :any:`search.findall` but cached."""
return search.findall(node, filter_=filter_, stop=stop, maxlevel=maxlevel, mincount=mincount, maxcount=maxcount)
@_cache(CACHE_SIZE)
def findall_by_attr(node, value, name="name", maxlevel=None, mincount=None, maxcount=None):
"""Identical to :any:`search.findall_by_attr` but cached."""
return search.findall_by_attr(node, value, name=name, maxlevel=maxlevel, mincount=mincount, maxcount=maxcount)
@_cache(CACHE_SIZE)
def find(node, filter_=None, stop=None, maxlevel=None):
"""Identical to :any:`search.find` but cached."""
return search.find(node, filter_=filter_, stop=stop, maxlevel=maxlevel)
@_cache(CACHE_SIZE)
def find_by_attr(node, value, name="name", maxlevel=None):
"""Identical to :any:`search.find_by_attr` but cached."""
return search.find_by_attr(node, value, name=name, maxlevel=maxlevel)
|