本文整理汇总了Python中cache.Cache.max_cache方法的典型用法代码示例。如果您正苦于以下问题:Python Cache.max_cache方法的具体用法?Python Cache.max_cache怎么用?Python Cache.max_cache使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cache.Cache
的用法示例。
在下文中一共展示了Cache.max_cache方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: lessened
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import max_cache [as 别名]
class Relation:
"""helper class to provide for simple relations between items. relations
can be strengthend or lessened(?) by giving them a positive or negative
'nudge'.
>>> r = Relation('test')
>>> r.get(100, 200)
0.5
>>> '{:2.2f}'.format(r.positive(100, 200))
'0.71'
>>> '{:2.2f}'.format(r.negative(100, 200))
'0.43'
>>> '{:2.2f}'.format(r.get(100, 200))
'0.43'
>>> r.get(100, 200, 'pos')
1.0
>>> r.get(100, 200, 'neg')
1.0
"""
def __init__(self, name, db=None, mix=0.5, min_cache=100, max_cache=1000,
commit_after=100):
"""create a new relation class.
>>> db = sqlite3.connect(':memory:')
>>> r = Relation('test', db=db, mix=0.75, min_cache=100, max_cache=200)
>>> r.get(100, 200)
0.5
>>> '{:2.2f}'.format(r.positive(100, 200))
'0.73'
>>> del r
>>> r = Relation('test', db=db, mix=0.75, min_cache=100, max_cache=200)
>>> '{:2.2f}'.format(r.get(100, 200))
'0.73'
"""
self.__name = name
# for caching
self.__cache = Cache(min_cache, max_cache)
# factors for calculating the relation
self.__rolling = mix
self.__stable = 1.0 - mix
# commit changes to the database after save_after changes
self.__commit_after = commit_after
self.__pending_changes = 0
self.__tbl_name = 'relation_{}'.format(self.__name)
if db is None:
db = sqlite3.connect(':memory:')
self.__db = db
self.__db.row_factory = dict_factory
self.__db.isolation_level = 'Immediate'
self.__cursor = self.__db.cursor()
self.__init_db__()
def __init_db__(self):
sql = '''CREATE TABLE {} (
keyl INTEGER,
keyh INTEGER,
pos REAL,
neg REAL,
rel REAL,
UNIQUE(keyl, keyh))'''.format(self.__tbl_name)
self.__cursor.execute('''SELECT * FROM sqlite_master
WHERE type='table' AND name=?''', (self.__tbl_name,))
r = self.__cursor.fetchone()
# FIXME: this is probably not reliable
if not (r and sql == r['sql']):
self.__cursor.execute(sql)
def min_cache(self, min_cache=None):
"""set minimum cache size.
>>> r = Relation('test', min_cache=100)
>>> r.min_cache()
100
>>> r.min_cache(200)
200
>>> r.min_cache()
200
"""
return self.__cache.min_cache(min_cache)
def max_cache(self, max_cache=None):
"""set maximum cache size.
>>> r = Relation('test', max_cache=1000)
>>> r.max_cache()
1000
>>> r.max_cache(200)
200
>>> r.max_cache()
200
"""
return self.__cache.max_cache(max_cache)
def cache_size(self):
"""return the current size of the cache.
>>> r = Relation('test')
>>> r.cache_size()
0
#.........这里部分代码省略.........