本文整理汇总了Python中cache.Cache.add方法的典型用法代码示例。如果您正苦于以下问题:Python Cache.add方法的具体用法?Python Cache.add怎么用?Python Cache.add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cache.Cache
的用法示例。
在下文中一共展示了Cache.add方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: alphabeta
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import add [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
示例2: __init__
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import add [as 别名]
#.........这里部分代码省略.........
onExit()
return
def getLyric():
if 'lyric' not in self.songs[str(self.playing_id)].keys():
self.songs[str(self.playing_id)]["lyric"] = []
if len(self.songs[str(self.playing_id)]["lyric"]) > 0:
return
netease = NetEase()
lyric = netease.song_lyric(self.playing_id)
if lyric == [] or lyric == '未找到歌词':
return
lyric = lyric.split('\n')
self.songs[str(self.playing_id)]["lyric"] = lyric
return
def gettLyric():
if 'tlyric' not in self.songs[str(self.playing_id)].keys():
self.songs[str(self.playing_id)]["tlyric"] = []
if len(self.songs[str(self.playing_id)]["tlyric"]) > 0:
return
netease = NetEase()
tlyric = netease.song_tlyric(self.playing_id)
if tlyric == [] or tlyric == '未找到歌词翻译':
return
tlyric = tlyric.split('\n')
self.songs[str(self.playing_id)]["tlyric"] = tlyric
return
def cacheSong(song_id, song_name, artist, song_url):
def cacheExit(song_id, path):
self.songs[str(song_id)]['cache'] = path
self.cache.add(song_id, song_name, artist, song_url, cacheExit)
self.cache.start_download()
if 'cache' in popenArgs.keys() and os.path.isfile(popenArgs['cache']):
thread = threading.Thread(target=runInThread,
args=(onExit, popenArgs['cache']))
else:
thread = threading.Thread(target=runInThread,
args=(onExit, popenArgs['mp3_url']))
cache_thread = threading.Thread(
target=cacheSong,
args=(popenArgs['song_id'], popenArgs['song_name'], popenArgs[
'artist'], popenArgs['mp3_url']))
cache_thread.start()
thread.start()
lyric_download_thread = threading.Thread(target=getLyric, args=())
lyric_download_thread.start()
tlyric_download_thread = threading.Thread(target=gettLyric, args=())
tlyric_download_thread.start()
# returns immediately after the thread starts
return thread
def get_playing_id(self):
return self.playing_id
def recall(self):
if self.info["idx"] >= len(self.info["player_list"]) and self.end_callback is not None:
self.end_callback()
if self.info["idx"] < 0 or self.info["idx"] >= len(self.info["player_list"]):
self.info["idx"] = 0
self.stop()
return
self.playing_flag = True
示例3: __init__
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import add [as 别名]
class Player:
def __init__(self):
self.config = Config()
self.ui = Ui()
self.popen_handler = None
# flag stop, prevent thread start
self.playing_flag = False
self.pause_flag = False
self.process_length = 0
self.process_location = 0
self.process_first = False
self.storage = Storage()
self.info = self.storage.database["player_info"]
self.songs = self.storage.database["songs"]
self.playing_id = -1
self.cache = Cache()
self.mpg123_parameters = self.config.get_item("mpg123_parameters")
self.end_callback = None
def popen_recall(self, onExit, popenArgs):
"""
Runs the given args in a subprocess.Popen, and then calls the function
onExit when the subprocess completes.
onExit is a callable object, and popenArgs is a lists/tuple of args that
would give to subprocess.Popen.
"""
def runInThread(onExit, popenArgs):
para = ['mpg123', '-R']
para[1:1] = self.mpg123_parameters
self.popen_handler = subprocess.Popen(para, stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
self.popen_handler.stdin.write("V " + str(self.info["playing_volume"]) + "\n")
self.popen_handler.stdin.write("L " + popenArgs + "\n")
self.process_first = True
while (True):
if self.playing_flag == False:
break
try:
strout = self.popen_handler.stdout.readline()
except IOError:
break
if re.match("^\@F.*$", strout):
process_data = strout.split(" ")
process_location = float(process_data[4])
if self.process_first:
self.process_length = process_location
self.process_first = False
self.process_location = 0
else:
self.process_location = self.process_length - process_location
continue
if strout == "@P 0\n":
self.popen_handler.stdin.write("Q\n")
self.popen_handler.kill()
break
if self.playing_flag:
self.next_idx()
onExit()
return
def getLyric():
if 'lyric' not in self.songs[str(self.playing_id)].keys():
self.songs[str(self.playing_id)]["lyric"] = []
if len(self.songs[str(self.playing_id)]["lyric"]) > 0:
return
netease = NetEase()
lyric = netease.song_lyric(self.playing_id)
if (not lyric == []) or lyric == '未找到歌词':
lyric = lyric.split('\n')
self.songs[str(self.playing_id)]["lyric"] = lyric
return
def cacheSong(song_id, song_name, artist, song_url):
def cacheExit(song_id, path):
self.songs[str(song_id)]['cache'] = path
self.cache.add(song_id, song_name, artist, song_url, cacheExit)
self.cache.start_download()
if 'cache' in popenArgs.keys() and os.path.isfile(popenArgs['cache']):
thread = threading.Thread(target=runInThread, args=(onExit, popenArgs['cache']))
else:
thread = threading.Thread(target=runInThread, args=(onExit, popenArgs['mp3_url']))
cache_thread = threading.Thread(target=cacheSong, args=(
popenArgs['song_id'], popenArgs['song_name'], popenArgs['artist'], popenArgs['mp3_url']))
cache_thread.start()
thread.start()
lyric_download_thread = threading.Thread(target=getLyric, args=())
lyric_download_thread.start()
# returns immediately after the thread starts
return thread
def get_playing_id(self):
return self.playing_id
def recall(self):
if self.info["idx"] >= len(self.info["player_list"]) and self.end_callback != None:
#.........这里部分代码省略.........
示例4: DataManager
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import add [as 别名]
#.........这里部分代码省略.........
# 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)
dataset[object.key] = object
self.cache.add(object)
return dataset
def set_object_class(self, object_class):
self.object_class = object_class