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


Python Cache.get方法代码示例

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


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

示例1: testCache

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get [as 别名]
	def testCache(self, n):
		m = Cache(n)

		# Insert n elements, (0, n]
		for i in range(0, n):
			m.set(i, i+1)
		
		# Retrieve n elements, (0, n]
		for i in range(0, n):
			r = random.randrange(0, n)
			assert r+1 == m.get(r)
		
		# Insert n elements, (n, n+n]
		for i in range(n, n+n):
			m.set(i, i+1)
		
		# Retrieve n elements, (n, n+n]
		for i in range(0, n):
			r = random.randrange(n, n+n)
			assert r+1 == m.get(r)

		# Retrieve n elements, (0, n]
		# Elements should have been deleted from cache
		for i in range(0, n):
			assert m.get(i) == None
开发者ID:adcaes,项目名称:Wrpp,代码行数:27,代码来源:test.py

示例2: xtest_evicts_first_key_if_full

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get [as 别名]
 def xtest_evicts_first_key_if_full(self):
     cache = Cache(capacity=2)
     cache.set('foo', 'bar')
     cache.set('baz', 'baz')
     cache.set('bar', 'foo')
     self.assertEqual(cache.get('foo'), None)
     self.assertEqual(cache.get('baz'), 'baz')
     self.assertEqual(cache.get('bar'), 'foo')
开发者ID:michaelmcmillan,项目名称:LittList,代码行数:10,代码来源:test_cache.py

示例3: alphabeta

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get [as 别名]
def alphabeta(node, depth, alpha=float("-inf"), beta=float("inf"), cache=None):
	if cache is None:
		cache = Cache(1000)
	cached_result = cache.get(node)

	if (cached_result is not None):
		if (cached_result[0] >= depth):
			return cached_result[1]

	child_nodes = [m.get_board() for m in get_moves(node)]
	if (depth == 0) or (len(child_nodes) == 0):
		return score_board(node)

	if node.white_plays:
		bestvalue = alpha
		for child in child_nodes:
			ab_score = alphabeta(child, depth - 1, bestvalue, beta, cache=cache)  # , bla = ab_score2)
			bestvalue = max(bestvalue, ab_score)

			if beta <= bestvalue:
				break
		cache.add(node, bestvalue, depth)
		return bestvalue
	else:
		bestvalue = beta
		for child in child_nodes:
			ab_score = alphabeta(child, depth - 1, alpha, bestvalue, cache=cache)  # , bla = ab_score2)

			bestvalue = min(bestvalue, ab_score)
			if bestvalue <= alpha:
				break

		cache.add(node, bestvalue, depth)
		return bestvalue
开发者ID:fdusek,项目名称:Checkers,代码行数:36,代码来源:search.py

示例4: TestAlgorithms

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get [as 别名]
class TestAlgorithms(unittest.TestCase):
    def setUp(self):
        pass

    def test_algorithm(self, name=None):
        if name == None:
            return
        self.cache = Cache(name, CACHESIZE)
        self.assertEqual(str(self.cache.cache), name)
        self.assertEqual(self.cache.get("trausti"), None)
        self.assertEqual(self.cache.put("trausti", 100), 1)
        self.assertEqual(self.cache.get("trausti"), 100)

        for j in xrange(2000):
            self.assertEqual(self.cache.put("%d" % j, 200), 1)

        self.assertEqual(self.cache.get("1999"), 200)

        for j in xrange(2000, 3000):
            self.assertEqual(self.cache.get("%d" % j), None)

        for j in xrange(NUMREQUESTS):
            key = str(keydistribution[j])
            if not self.cache.get(key):
                self.cache.put(key, "A")

    def test_LRU(self):
        self.test_algorithm("LRU")
        self.cache.cache.walk()
        self.cache.put("hestur", 100)
        d = self.cache.cache.get_stackdistance("hestur")
        self.assertEqual(d[0], 0)
        self.assertEqual(d[1], True)
        self.cache.put("skinka", 101)
        d = self.cache.cache.get_stackdistance("hestur")
        self.assertEqual(d[0], 1)
        self.assertEqual(d[1], True)

    def test_CLOCK(self):
        self.test_algorithm("CLOCK")

    def test_LFU(self):
        self.test_algorithm("LFU")

    def test_LRU3(self):
        self.test_algorithm("LRU3")
    """
开发者ID:trauzti,项目名称:mimir,代码行数:49,代码来源:test_algorithms.py

示例5: cache_url

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get [as 别名]
 def cache_url(self):
     """cache the base_url"""
     cache = Cache()
     url = self.__get_base_url()
     cache.set("url", url, 500)
     while True:
         result = cache.get("url")
         if result is None:
             result = self.__get_base_url()
             cache.set("url", result, 500)
         return result
开发者ID:ma6174,项目名称:zhengfang,代码行数:13,代码来源:zheng.py

示例6: cache_url

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get [as 别名]
def cache_url(self):
    '''cache the base_url'''
    cache = Cache()
    url = self.__get_base_url()
    cache.set('url',url,500)
    while True:
        result = cache.get('url')
        if result is None:
            result = self.__get_base_url()
            cache.set('url',result,500)
        return result
开发者ID:AwesomeDennis,项目名称:gotit,代码行数:13,代码来源:zheng.py

示例7: predict

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get [as 别名]
 def predict(self, localities):
     """Predict locality type for each locality in a list."""
     for loc in localities:
         logging.info('Predicting locality type for "%s"' % loc.name)
         key = 'loctype-%s' % loc.name
         prediction = Cache.get(key)
         if not prediction:
             loctype, scores = self.predictor.get_type(loc.name)
             prediction = dict(locname=loc.name, loctype=loctype, scores=scores)
             Cache.put(key, prediction)
         loc.type = prediction['loctype']
         loc.type_scores = prediction['scores']
         logging.info('Predicted "%s" for "%s"' % (loc.type, loc.name))
     return localities
开发者ID:GeomancerProject,项目名称:Software,代码行数:16,代码来源:core.py

示例8: geocode

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get [as 别名]
 def geocode(self, localities):
     for loc in localities:
         loc.feature_geocodes = {}
         loc.parts['feature_geocodes'] = {}
         for feature in loc.parts['features']:              
             logging.info('Geocoding feature "%s"' % feature)  
             key = 'geocode-%s' % feature
             geocode = Cache.get(key)
             if not geocode:
                 geocode = self.geocoder.geocode(feature)
                 Cache.put(key, geocode)
             loc.parts['feature_geocodes'][feature] = geocode 
             logging.info('Geocoded feature "%s"' % feature)
     return localities
开发者ID:GeomancerProject,项目名称:Software,代码行数:16,代码来源:core.py

示例9: ImageHandler

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get [as 别名]
class ImageHandler(RequestHandler):
    def initialize(self, directory):
        self.cache = Cache(directory)
        self.twitter_token = twitter.get_bearer()

    @coroutine
    def get(self, user_id=None):
        image = self.cache.get(user_id)
        if not image:
            print("ImageHandler: {0}: image not present in cache".format(user_id))
            url = twitter.get_user_profile_image_url(user_id, self.twitter_token)
            if url is not None:
                print("ImageHandler: {0}: resolved profile image URL: {1}".format(user_id, url))
                if self.cache.set(user_id, url):
                    print("ImageHandler: {0}: cached image".format(user_id))
                    # Successfully cached user image.
                    image = self.cache.get(user_id)
                else:
                    self.fatal("ImageHandler: {0}: failed to cache image".format(user_id))
            else:
                print("ImageHandler: {0}: failed to fetch profile image URL from Twitter".format(user_id))
                image = "./static/default.png"

        content_type = self.path_to_content_type(image)
        self.set_header("Content-Type", content_type)
        with open(image, "r") as f:
            self.write(f.read())

    def fatal(self, message):
        print(message)
        raise tornado.web.HTTPError(404)

    def path_to_content_type(self, path):
        _, extension = os.path.splitext(path)
        # Remove leading dot from extension.
        return "image/{0}".format(extension[1:])
开发者ID:helderm,项目名称:stalkr,代码行数:38,代码来源:webapp.py

示例10: __init__

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get [as 别名]
class Album:
  def __init__(self, db=None, artist=None, min_cache=100, max_cache=1000,
      commit_after=100):
    """Create a new album factory.
    >>> a = Album(min_cache=1, max_cache=1)
    """
    self.__cache_key = Cache(min_cache, max_cache)
    self.__cache_id = Cache(min_cache, max_cache)
    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()

    if artist is None:
      artist = Artist(db=db, min_cache=min_cache, max_cache=max_cache,
        commit_after=commit_after)
    self.__artist = artist

    self.__tbl_name = 'album'
    self.__pending_changes = 0
    self.__commit_after = commit_after
    self.__init_db__()

  def __init_db__(self):
    sql = '''CREATE TABLE {} (
        id INTEGER PRIMARY KEY,
        artist_id INTEGER,
        title TEXT,
        UNIQUE(artist_id, title))'''.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 __key(self, artist, title):
    artist_id = self.__artist.get_id(artist)
    return u'{}|{}'.format(artist_id, title), artist_id

  def get_id(self, artist, title):
    """return the id of album with title and artist.

    >>> a = Album(min_cache=1, max_cache=1)
    >>> a.get_id('The Beatles', 'Please Please Me')
    0
    >>> a.get_id('The BEATles', 'Please PLEASE Me')
    0
    >>> a.get_id('The Rolling Stones', 'The Rolling Stones')
    1
    """
    if title is None:
      title = u''
    else:
      title = title.lower()
    key, artist_id = self.__key(artist, title)
    if not self.__cache_key.has_key(key):
      album = self.__load(artist_id=artist_id, title=title)
      self.__cache_key.set(key, album['id'])
      return album['id']
    return self.__cache_key.get(key)

  def get(self, album_id):
    """return the name of artist id.

    >>> a = Album(min_cache=1, max_cache=1)
    >>> a.get_id('The Beatles', 'Please Please Me')
    0
    >>> a.get_id('The Rolling Stones', 'The Rolling Stones')
    1
    >>> a.get(0)
    (u'the beatles', u'please please me')
    >>> a.get(1)
    (u'the rolling stones', u'the rolling stones')
    """
    if not self.__cache_id.has_key(album_id):
      album = self.__load(album_id=album_id)
      if not album:
        return None
      self.__cache_id.set(album_id, album)
    else:
      album = self.__cache_id.get(album_id)
    return self.__artist.get(album['artist_id']), album['title']

  def _dump(self):
    self.__cursor.execute('''SELECT * FROM {}'''.format(self.__tbl_name))
    for row in self.__cursor.fetchall():
      print(row)

  def __load(self, artist_id=None, title=None, album_id=None):
    if artist_id is not None and title is not None:
      self.__cursor.execute('''SELECT * FROM {}
          WHERE artist_id=? AND title=?'''.format(self.__tbl_name), (
          artist_id, title,))
    elif album_id is not None:
      self.__cursor.execute('''SELECT * FROM {}
          WHERE id=?'''.format(self.__tbl_name), (album_id,))
    album = self.__cursor.fetchone()
#.........这里部分代码省略.........
开发者ID:pscn,项目名称:yampdqmgr,代码行数:103,代码来源:album.py

示例11: __init__

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get [as 别名]
class TvDb:
    """
    
    API:
        scraper  - скрапер
        search   - поиск сериалов
        movie    - профайл фильма
        
    """

    def __init__(self, language='en'):
        self.api_key = '33DBB309BB2B0ADB'
        dbname='tvdb.%s.db' % language
        self.cache = Cache(dbname, 1.0)

        self.language = language

        self.http = HTTP()
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language': 'ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3',
            'Cache-Control': 'no-cache',
            'Referer': 'http://www.thetvdb.com/'
        }


    # API

    def scraper(self, search, year=None):
        try:
            if not isinstance(search, list):
                search = [search]
            tag = 'scraper:' + urllib.quote_plus(":".join(search).encode('utf8'))
        except:
            return None
        else:

            if year:
                tag += ':' + str(year)

            id = self.cache.get(tag, self._scraper, search, year)
            if not id:
                return None

            return self.movie(id)

    def search(self, search, year=None):
        return self._search(search, year)


    def movie(self, id):
        id = str(id)
        return self.cache.get('movie:' + id, self._movie, id)


    def _movie(self, id):
        try:
            dirname = tempfile.mkdtemp()
        except:
            dirname = xbmc.translatePath('special://temp')
            for subdir in ('xbmcup', 'plugin.video.torrenter'):
                dirname = os.path.join(dirname, subdir)
                if not os.path.exists(dirname):
                    os.mkdir(dirname)

        url = 'http://www.thetvdb.com/api/' + self.api_key + '/series/' + id + '/all/' + self.language + '.zip'
        # print url
        response = self.http.fetch(url, headers=self.headers, download=os.path.join(dirname, 'movie.zip'), timeout=20)
        if response.error:
            print "ERRRRRROR! " + str(response.error)
            self._movie_clear(dirname)
            return False, None

        try:
            filezip = zipfile.ZipFile(os.path.join(dirname, 'movie.zip'), 'r')
            filezip.extractall(dirname)
            filezip.close()
            movie = file(os.path.join(dirname, self.language + '.xml'), 'rb').read().decode('utf8')
        except:
            self._movie_clear(dirname)
            return False, None

        self._movie_clear(dirname)

        body = re.compile(r'<Series>(.+?)</Series>', re.U | re.S).search(movie)
        if not body:
            return False, None

        body = body.group(1)

        res = {
            'icon': None,
            'thumbnail': None,
            'properties': {
                'fanart_image': None,
            },
            'info': {
                'count': int(id)
            }
#.........这里部分代码省略.........
开发者ID:kharts,项目名称:plugin.video.torrenter,代码行数:103,代码来源:tvdb.py

示例12: __init__

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get [as 别名]
class TmDb:
    """
    
    API:
        scraper  - скрапер
        search   - поиск фильмов
        movie    - профайл фильма
        
    """
    
    def __init__(self):
        self.api_key = API_KEY #'57983e31fb435df4df77afb854740ea9'
        self.base_url = "http://api.themoviedb.org/3"
        self.cache = Cache('tmdb.db', 1.1)
        self.params = {
            "api_key": self.api_key, 
            "language": "ru"
        }
        self.headers = {
            'Referer': self.base_url
        }
        
        
    # API
    
    def scraper(self, search, year):
        try:
            if not isinstance(search, list):
                search = [search]
            tag = 'scraper:' + urllib.quote_plus(":".join(search).encode('utf8'))
        except:
            return None
        else:
            if year:
                tag += ':' + str(year)
            
            id = self.cache.get(tag, self._scraper, search, year)
            if not id:
                return None
            
            return self.movie(id)
        
    def search(self, name):
        return self._search(name)
    
    
    def movie(self, id):
        id = str(id)
        return self.cache.get('movie:' + id, self._movie, id)
    
    
    def _movie(self, imdb_id):
        import urllib2
        from xbmctorrent.utils import first

        meta = url_get_json("%s/movie/%s" % (self.base_url, imdb_id), params=self.params, headers=self.headers, with_immunicity=False) or {}

        if not meta:
            return False, None

        def img(key, size="original", default=""):
            return meta.get(key) and self._image(meta[key], size=size) or default
        def m(key, default=""):
            return meta.get(key) or default
        def m_crew(job):
          return first([crew["name"] for crew in (m("credits", default={}).get("crew") or []) if crew["job"] == job])
        def get_studio():
          return (first(sorted(m("production_companies") or [], key=lambda x: x["id"])) or {}).get("name") or ""

        res = {
            "icon": img("poster_path", size="w500"),
            "thumbnail": img("poster_path", size="w500"),
            "info": {
                "count": m("id"),
                "title": m("title"),
                "originaltitle" : m("original_title"),
                "genre": meta.get("genres") and ", ".join([genre["name"] for genre in meta["genres"]]) or "",
                "plot": m("overview"),
                "plot_outline": m("overview"),
                "tagline": m("tagline"),
                "rating": m("vote_average"),
                "duration": m("runtime"),
                "code": m("imdb_id"),
                "cast": [cast["name"] for cast in (m("credits", default={}).get("cast") or [])],
                "director": m_crew("Director"),
                "writer": m_crew("Writer") or m_crew("Novel") or m_crew("Screenplay"),
                "studio": get_studio(),
                "year": meta.get("release_date") and meta["release_date"].split("-")[0] or 0,
            },
            "properties": {
                "fanart_image": img("backdrop_path"),
            },
        }

        timeout = True

        # если фильм свежий, то кладем в кэш НЕ на долго (могут быть обновления на сайте)
        if 'year' not in res['info'] or int(res['info']['year']) >= time.gmtime(time.time()).tm_year:
            timeout = 7*24*60*60 #week
        
#.........这里部分代码省略.........
开发者ID:smoky-jr,项目名称:XBMC.ru-forum-Add-ons,代码行数:103,代码来源:tmdb.py

示例13: lessened

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get [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

示例14: test_returns_none_if_key_does_not_exist

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get [as 别名]
 def test_returns_none_if_key_does_not_exist(self):
     cache = Cache()
     value = cache.get('foo')
     self.assertEqual(value, None)
开发者ID:michaelmcmillan,项目名称:LittList,代码行数:6,代码来源:test_cache.py

示例15: a_hard_function

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get [as 别名]
#!/usr/bin/env python
#coding=utf-8
from cache import Cache

def a_hard_function():
    '''一个需要缓存的函数'''
    print "getting result"
    from time import sleep
    import random
    sleep(2)
    print "done"
    return random.randint(1,100)

if __name__ == "__main__":
    cache = Cache()
    cache.set('a','aaaa',5)   #a的值是'aaaa',生存时间位5秒
    cache.set('b',[1,2])      #b的值是[1,2],生存时间无限长
    while True:
        result = cache.get("hard_func")
        if result is None:
            result = a_hard_function()
            cache.set("hard_func",result,2)
        print cache.get('a'),
        print cache.get('b'),
        print result
开发者ID:ma6174,项目名称:pycache,代码行数:27,代码来源:test_cache.py


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