server/general: fix pylint warnings

This commit is contained in:
rr- 2016-06-02 23:39:26 +02:00
parent 7c7adafd82
commit 28009bf46d
4 changed files with 15 additions and 22 deletions

View file

@ -15,8 +15,12 @@ class LruCache(object):
def insert_item(self, item): def insert_item(self, item):
if item.key in self.hash: if item.key in self.hash:
item_index = next(i for i, v in enumerate(self.item_list) if v.key == item.key) item_index = next(i \
self.item_list[:] = self.item_list[:item_index] + self.item_list[item_index+1:] for i, v in enumerate(self.item_list) \
if v.key == item.key)
self.item_list[:] \
= self.item_list[:item_index] \
+ self.item_list[item_index+1:]
self.item_list.insert(0, item) self.item_list.insert(0, item)
else: else:
if len(self.item_list) > self.length: if len(self.item_list) > self.length:
@ -32,25 +36,16 @@ class LruCache(object):
del self.hash[item.key] del self.hash[item.key]
del self.item_list[self.item_list.index(item)] del self.item_list[self.item_list.index(item)]
def validate_item(self): _CACHE = LruCache(length=100)
def _outdated_items():
now = datetime.now()
for item in self.item_list:
time_delta = now - item.timestamp
if time_delta.seconds > self.delta:
yield item
map(lambda x: self.remove_item(x), _outdated_items())
_cache = LruCache(length=100)
def purge(): def purge():
_cache.remove_all() _CACHE.remove_all()
def has(key): def has(key):
return key in _cache.hash return key in _CACHE.hash
def get(key): def get(key):
return _cache.hash[key].value return _CACHE.hash[key].value
def put(key, value): def put(key, value):
_cache.insert_item(LruCacheItem(key, value)) _CACHE.insert_item(LruCacheItem(key, value))

View file

@ -1,4 +1,3 @@
import falcon
from szurubooru.func import cache from szurubooru.func import cache
class CachePurger(object): class CachePurger(object):

View file

@ -1,5 +1,4 @@
import sqlalchemy import sqlalchemy
import szurubooru.errors
from szurubooru import db, errors from szurubooru import db, errors
from szurubooru.func import util from szurubooru.func import util
from szurubooru.search import criteria from szurubooru.search import criteria
@ -55,7 +54,7 @@ class BaseSearchConfig(object):
expr = column <= int(criterion.max_value) expr = column <= int(criterion.max_value)
else: else:
assert False assert False
except ValueError as e: except ValueError:
raise errors.SearchError( raise errors.SearchError(
'Criterion value %r must be a number.' % (criterion,)) 'Criterion value %r must be a number.' % (criterion,))
if criterion.negated: if criterion.negated:

View file

@ -14,13 +14,13 @@ class SearchExecutor(object):
self.config = search_config self.config = search_config
def execute(self, query_text, page, page_size): def execute(self, query_text, page, page_size):
key = (id(self.config), query_text, page, page_size)
if cache.has(key):
return cache.get(key)
''' '''
Parse input and return tuple containing total record count and filtered Parse input and return tuple containing total record count and filtered
entities. entities.
''' '''
key = (id(self.config), query_text, page, page_size)
if cache.has(key):
return cache.get(key)
filter_query = self.config.create_filter_query() filter_query = self.config.create_filter_query()
filter_query = filter_query.options(sqlalchemy.orm.lazyload('*')) filter_query = filter_query.options(sqlalchemy.orm.lazyload('*'))
filter_query = self._prepare(filter_query, query_text) filter_query = self._prepare(filter_query, query_text)