当前位置: 首页>>代码示例>>Python>>正文


Python Cache.min_cache方法代码示例

本文整理汇总了Python中cache.Cache.min_cache方法的典型用法代码示例。如果您正苦于以下问题:Python Cache.min_cache方法的具体用法?Python Cache.min_cache怎么用?Python Cache.min_cache使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cache.Cache的用法示例。


在下文中一共展示了Cache.min_cache方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: lessened

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import min_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
#.........这里部分代码省略.........
开发者ID:pscn,项目名称:yampdqmgr,代码行数:103,代码来源:relation.py


注:本文中的cache.Cache.min_cache方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。