本文整理汇总了Python中cache.Cache.has_key方法的典型用法代码示例。如果您正苦于以下问题:Python Cache.has_key方法的具体用法?Python Cache.has_key怎么用?Python Cache.has_key使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cache.Cache
的用法示例。
在下文中一共展示了Cache.has_key方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: hostbroker
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import has_key [as 别名]
class hostbroker(Handler):
def __init__(self, server, addr, transport, call_later, max_ul_rate, config, rlcount):
self.server = server
self.addr = addr
self.transport = transport
self.rltransport = KRateLimiter(transport, max_ul_rate, call_later, rlcount, config['max_rate_period'])
self.call_later = call_later
self.connections = Cache(touch_on_access=True)
self.hammerlock = Hammerlock(100, call_later)
self.expire_connections(loop=True)
self.config = config
if not self.config.has_key('pause'):
self.config['pause'] = False
def expire_connections(self, loop=False):
self.connections.expire(bttime() - KRPC_CONNECTION_CACHE_TIME)
if loop:
self.call_later(self.expire_connections, KRPC_CONNECTION_CACHE_TIME, (True,))
def data_came_in(self, addr, datagram):
#if addr != self.addr:
if not self.config['pause'] and self.hammerlock.check(addr):
c = self.connectionForAddr(addr)
c.datagramReceived(datagram, addr)
def connection_lost(self, socket):
## this is like, bad
print ">>> connection lost!", socket
def connectionForAddr(self, addr):
if addr == self.addr:
raise KRPCSelfNodeError()
if not self.connections.has_key(addr):
conn = KRPC(addr, self.server, self.transport, self.rltransport, self.call_later)
self.connections[addr] = conn
else:
conn = self.connections[addr]
return conn
示例2: __init__
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import has_key [as 别名]
class Track:
def __init__(self, db=None, artist=None, album=None, min_cache=100,
max_cache=1000, commit_after=100):
"""Create a new track factory.
>>> a = Track(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
if album is None:
album = Album(db=db, artist=artist, min_cache=min_cache,
max_cache=max_cache, commit_after=commit_after)
self.__album = album
self.__tbl_name = 'track'
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,
album_id INTEGER,
title TEXT,
length INTEGER,
UNIQUE(artist_id, album_id, title, length))'''.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, length=-1, album=None, album_artist=None):
artist_id = self.__artist.get_id(artist)
if album_artist is None:
album_artist = artist
album_id = self.__album.get_id(album_artist, album)
return u'{}|{}|{}|{}'.format(artist_id, album_id, title, length), \
artist_id, album_id
def get_id(self, artist, title, length=-1, album=None, album_artist=None):
"""return the id of track.
>>> a = Track(min_cache=1, max_cache=1)
>>> a.get_id('The Beatles', 'Please Please Me', 126, 'Please Please Me')
0
>>> a.get_id('The Beatles', 'Please Please Me', 126, 'Please Please Me')
0
"""
title = title.lower()
key, artist_id, album_id = self.__key(artist, title, length, album,
album_artist)
if not self.__cache_key.has_key(key):
track = self.__load(artist_id=artist_id, title=title, album_id=album_id,
length=length)
self.__cache_key.set(key, track['id'])
return track['id']
return self.__cache_key.get(key)
def get(self, track_id):
"""return info from track_id.
>>> a = Track(min_cache=1, max_cache=1)
>>> a.get_id('Taco', "Puttin' on the Ritz", 280, "Hits of the 80's", 'Various Artists')
0
>>> a.get(0)
(u'taco', u"puttin' on the ritz", 280, u"hits of the 80's", u'various artists')
"""
if not self.__cache_id.has_key(track_id):
track = self.__load(track_id=track_id)
if not track:
return None
self.__cache_id.set(track_id, track)
else:
track = self.__cache_id.get(track_id)
artist = self.__artist.get(track['artist_id'])
album_artist, album_title = self.__album.get(track['album_id'])
if album_title == '':
album_title = None
return artist, track['title'], track['length'], album_title, album_artist
def _dump(self):
self.__cursor.execute('''SELECT * FROM {}'''.format(self.__tbl_name))
for row in self.__cursor.fetchall():
print(row)
def __load(self, track_id=None, artist_id=None, title=None, album_id=None,
length=-1):
#.........这里部分代码省略.........
示例3: __init__
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import has_key [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()
#.........这里部分代码省略.........
示例4: lessened
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import has_key [as 别名]
#.........这里部分代码省略.........
def getall(self, key1):
"""return all relations for key1.
>>> r = Relation('test')
>>> r.get(100, 200)
0.5
>>> r.get(100, 300)
0.5
>>> [(key, rel) for key, rel in r.getall(100)]
[(200, 0.5), (300, 0.5)]
>>> [(key, rel) for key, rel in r.getall(200)]
[(100, 0.5)]
"""
self.__cursor.execute('''
SELECT keyh AS key2 FROM {} WHERE keyl=?
UNION ALL
SELECT keyl AS key2 FROM {} WHERE keyh=?'''.format(
self.__tbl_name, self.__tbl_name), (key1,key1,))
for row in self.__cursor.fetchall():
yield row['key2'], self.get(key1, row['key2'])
def _get(self, key, keyl, keyh, save=True):
"""Internal function to get the relation from cache or load it from the
database.
>>> r = Relation('test')
>>> key, keyl, keyh = r._keys(1, 2)
>>> r._get(key, keyl, keyh)
{'neg': 0.0, 'keyh': 2, 'pos': 0.0, 'rel': 0.5, 'keyl': 1}
>>> r.positive(1, 2)
0.7083333333333333
>>> r._get(key, keyl, keyh)
{'neg': 0.0, 'keyh': 2, 'pos': 1.0, 'rel': 0.7083333333333333, 'keyl': 1}
"""
if not self.__cache.has_key(key):
relation = self._load(key, keyl, keyh, save)
self.__cache.set(key, relation)
return relation
return self.__cache.get(key)
def _load(self, key, keyl, keyh, save=True):
"""Internal function to load the relation from the database, or create a
new one.
>>> r = Relation('test')
>>> key, keyl, keyh = r._keys(1, 2)
>>> r._load(key, keyl, keyh)
{'neg': 0.0, 'keyh': 2, 'pos': 0.0, 'rel': 0.5, 'keyl': 1}
"""
self.__cursor.execute('''SELECT * FROM {}
WHERE keyl=? AND keyh=?'''.format(self.__tbl_name),
(keyl, keyh,))
relation = self.__cursor.fetchone()
if not relation:
relation = {
'keyl':keyl,
'keyh':keyh,
'pos':0.0,
'neg':0.0,
'rel':0.5
}
if save:
self._save(relation)
return relation
def _save(self, relation):
"""internal method to save a relation to the database"""
self.__cursor.execute('''INSERT OR REPLACE INTO {}
示例5: get
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import has_key [as 别名]
def get(cls, count=settings.paginator["DEFAULT_COUNT"], q_filters={}, search=None, start=None, model=None, \
order='ASC', order_by='__key__'):
"""
get queries the database on model, starting with key, ordered by
order. It receives count + 1 items, returning count and setting a
next field to the count + 1 item key. It then reverses the sort, and
grabs count objects, returning the last as a the previous.
Arguments:
count: The amount of entries to pull on query
q_filter: The filter value (optional)
search: Search is used for SearchableModel searches
start: The key to start the page from
model: The Model object to query against. This is not a
string, it must be a Model derived object.
order: The order in which to pull the values.
order_by: The attribute to order results by. This defaults to
__key__
Returns a dict:
{
'next': next_key,
'prev': prev_key,
'items': entities_pulled
}
"""
# argument validation
if model == None:
raise ValueError('You must pass a model to query')
# a valid model object will have a gql method.
if callable(model.gql) == False:
raise TypeError('model must be a valid model object.')
# cache check
cache_string = "gae_paginator_"
for q_filter in q_filters:
cache_string = cache_string + q_filter + "_" + q_filters[q_filter] + "_"
cache_string = cache_string + "index"
c = Cache()
if c.has_key(cache_string):
return c[cache_string]
# build query
query = model.all()
if len(q_filters) > 0:
for q_filter in q_filters:
query.filter(q_filter + " = ", q_filters[q_filter])
if start:
if order.lower() == "DESC".lower():
query.filter(order_by + " <", start)
else:
query.filter(order_by + " >", start)
if search:
query.search(search)
if order.lower() == "DESC".lower():
query.order("-" + order_by)
else:
query.order(order_by)
results = query.fetch(count + 1)
if len(results) == count + 1:
next = getattr(results[count - 1], order_by)
# reverse the query to get the value for previous
if start is not None:
rquery = model.all()
for q_filter in q_filters:
rquery.filter(q_filter + " = ", q_filters[q_filter])
if search:
query.search(search)
if order.lower() == "DESC".lower():
rquery.order(order_by)
else:
rquery.order("-" + order_by)
rresults = rquery.fetch(count)
previous = getattr(results[0], order_by)
else:
previous = None
else:
next = None
return {
"results": results,
"next": next,
"previous": previous
}
示例6: DataManager
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import has_key [as 别名]
#.........这里部分代码省略.........
# Delete any newly deleted objects.
sql = 'SELECT identifier FROM deleted WHERE table_name=' + wsq(self.table.name) + ' AND deleted >= ' + wsq(last_synched)
cursor = db.select(sql)
while (1):
row = cursor.fetchone()
if row==None: break
# Load keys for the deleted object
object = self.new_object()
if len(self.table.key_list)==1:
field = self.table.fields[self.table.key_list[0]]
value = field.field_to_attr(row[0])
setattr(object, field.attribute, value)
else:
values = row[0].split()
for key in self.table.key_list:
field = self.table.fields[key]
value = field.field_to_attr(values[field.index])
setattr(object, field.attribute, value)
object.key = self.table.get_key(object)
#print 'Deleting from ' + self.table.name + ' cache: ' + str(value)
self.cache.delete(object)
# FIXME: Delete the object from all data sets which contain it!
# Update any newly updated objects.
sql = self.table.select + ' WHERE updated >= ' + wsq(last_synched)
cursor = db.select(sql)
while (1):
row = cursor.fetchone()
if row==None: break
key = self.table.get_row_key(row)
if self.cache.has_key(key):
object = self.cache[key]
self.table.load_row(object, row)
#print 'Updating in ' + self.table.name + ' cache: ' + str(object.key)
else:
object = self.row_to_object(row)
self.cache.add(object)
#print 'Adding in ' + self.table.name + ' cache: ' + str(object.key)
# FIXME: Add the object to all data sets whose filters it matches.
def get_cached(self):
"""
Returns a dataset containing all objects in the object cache.
"""
#print 'Pulling ' + self.table.name + ' from cache.'
dataset = self.new_dataset()
for key in self.cache.keys():
dataset[key] = self.cache[key]
return dataset
def get_sql(self, sql):
"""
Accepts a SQL statement, instantiates the corresponding objects from the
database, and stores those objects in the data cache if possible.
"""
#print 'Cache miss, loading: ' + self.table.name
dataset = self.new_dataset()
cursor = db.select(sql)
while (1):
row = cursor.fetchone()
if row==None: break
object = self.row_to_object(row)