本文整理汇总了Python中util.get_code函数的典型用法代码示例。如果您正苦于以下问题:Python get_code函数的具体用法?Python get_code怎么用?Python get_code使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_code函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_runlist_for_runner
def get_runlist_for_runner( self, username, no_refresh=False ):
key = self.get_runlist_for_runner_memkey( username )
runlist = memcache.get( key )
if runlist is None and not no_refresh:
# Not in memcache, so construct the runlist and store in memcache.
runlist = [ ]
q = runs.Runs.all( )
q.ancestor( runs.key() )
q.filter( 'username =', username )
q.order( '-date' )
q.order( '-datetime_created' )
for run in q.run( limit = 1000 ):
runlist.append( dict( run_id = str( run.key().id() ),
game = run.game,
game_code = util.get_code( run.game ),
category = run.category,
category_code = util.get_code(
run.category ),
time = util.
seconds_to_timestr( run.seconds ),
date = run.date,
datetime_created = run.datetime_created,
video = run.video,
version = run.version,
notes = run.notes ) )
if memcache.set( key, runlist ):
logging.debug( "Set " + key + " in memcache" )
else:
logging.warning( "Failed to set " + key + " in memcache" )
elif runlist is not None:
logging.debug( "Got " + key + " from memcache" )
return runlist
示例2: get_runlist_for_runner
def get_runlist_for_runner( self, username, page_num ):
key = self.get_runlist_for_runner_memkey( username )
cached_runlists = memcache.get( key )
if cached_runlists is None:
cached_runlists = dict( )
res = cached_runlists.get( page_num )
if res is None:
# Not in memcache, so construct the runlist and store in memcache.
res = dict( page_num=page_num,
has_next=True )
runlist = [ ]
try:
q = runs.Runs.all( )
q.ancestor( runs.key() )
q.filter( 'username =', username )
q.order( '-date' )
q.order( '-datetime_created' )
c = memcache.get( self.get_runlist_for_runner_cursor_memkey(
username, page_num ) )
if c:
try:
q.with_cursor( start_cursor=c )
except BadRequestError:
res['page_num'] = 1
else:
res['page_num'] = 1
for run in q.run( limit = self.RUNLIST_PAGE_LIMIT ):
runlist.append( dict(
run_id = str( run.key().id() ),
game = run.game,
game_code = util.get_code( run.game ),
category = run.category,
category_code = util.get_code( run.category ),
time = util.
seconds_to_timestr( run.seconds ),
date = run.date,
datetime_created = run.datetime_created,
video = run.video,
version = run.version,
notes = run.notes ) )
c = q.cursor( )
cursor_key = self.get_runlist_for_runner_cursor_memkey(
username, res['page_num'] + 1 )
if memcache.set( cursor_key, c ):
logging.debug( "Set " + cursor_key + " in memcache" )
else:
logging.warning( "Failed to set new " + cursor_key
+ " in memcache" )
if len( runlist ) < self.RUNLIST_PAGE_LIMIT:
res['has_next'] = False
except apiproxy_errors.OverQuotaError, msg:
logging.error( msg )
return self.OVER_QUOTA_ERROR
res['runlist'] = runlist
cached_runlists[ res['page_num'] ] = res
if memcache.set( key, cached_runlists ):
logging.debug( "Set " + key + " in memcache" )
else:
logging.warning( "Failed to set " + key + " in memcache" )
示例3: get_gamepage
def get_gamepage(self, game, no_refresh=False):
key = self.get_gamepage_memkey(game)
gamepage = memcache.get(key)
if gamepage is None and not no_refresh:
# Not in memcache, so construct the gamepage and store it in
# memcache.
# Gamepage is a list of dictionaries. These dictionaries have up
# to 5 keys, 'category', 'bk_runner', 'bk_time', 'bk_video' and
# 'infolist'.
gamepage = []
# Grab the game model
game_model = self.get_game_model(util.get_code(game))
if game_model is None:
logging.error("Could not create " + key + " due to no " + "game model")
return None
gameinfolist = json.loads(game_model.info)
# Use a projection query to get all of the unique
# username, category pairs
q = db.Query(runs.Runs, projection=("username", "category"), distinct=True)
q.ancestor(runs.key())
q.filter("game =", game)
q.order("category")
cur_category = None
for run in q.run(limit=1000):
if run.category != cur_category:
# New category
d = dict(category=run.category, category_code=util.get_code(run.category), infolist=[])
gamepage.append(d)
cur_category = run.category
# Check for a best known time for this category
for gameinfo in gameinfolist:
if gameinfo["category"] == run.category:
d["bk_runner"] = gameinfo.get("bk_runner")
d["bk_time"] = util.seconds_to_timestr(gameinfo.get("bk_seconds"))
d["bk_date"] = util.datestr_to_date(gameinfo.get("bk_datestr"))[0]
d["bk_video"] = gameinfo.get("bk_video")
break
# Add the info to the gamepage
info = self.get_runinfo(run.username, game, run.category)
d["infolist"].append(info)
# For each category, sort the runlist by seconds, breaking ties
# by date
for runlist in gamepage:
runlist["infolist"].sort(key=lambda x: util.get_valid_date(x["pb_date"]))
runlist["infolist"].sort(key=itemgetter("pb_seconds"))
# Sort the categories by number of runners
gamepage.sort(key=lambda x: len(x["infolist"]), reverse=True)
if memcache.set(key, gamepage):
logging.debug("Set " + key + " in memcache")
else:
logging.warning("Failed to set " + key + " in memcache")
elif gamepage is not None:
logging.debug("Got " + key + " from memcache")
return gamepage
示例4: get
def get( self, run_id ):
user = self.get_user( )
if not user:
self.redirect( "/" )
return
elif user == self.OVER_QUOTA_ERROR:
self.error( 403 )
self.render( "403.html" )
return
# Get the run
run = self.get_run_by_id( run_id )
if run == self.OVER_QUOTA_ERROR:
self.error( 403 )
self.render( "403.html", user=user )
return
if( not run or
( not user.is_mod and run.username != user.username ) ):
self.error( 404 )
self.render( "404.html", user=user )
return
self.render( "deleterun.html", user=user, run=run,
game_code=util.get_code( run.game ),
username_code=util.get_code( user.username ),
time=util.seconds_to_timestr( run.seconds ) )
示例5: get
def get( self, game_code ):
user = self.get_user( )
return_url = self.request.get( 'from' )
if not return_url:
return_url = "/"
# Get the category
category_code = self.request.get( 'c' )
if user is None or not category_code:
self.error( 404 )
self.render( "404.html", user=user )
return
# Have to take the code of the category code because of percent
# encoded plusses
category_code = util.get_code( category_code )
# Check to make sure that the user has run this game
game_model = self.get_game_model( game_code )
user_has_run = self.get_user_has_run( user.username, game_model.game )
if not user_has_run and not user.is_mod:
self.error( 404 )
self.render( "404.html", user=user )
return
# Find the corresponding gameinfo for this category
gameinfolist = json.loads( game_model.info )
gameinfo = None
for g in gameinfolist:
if util.get_code( g['category'] ) == category_code:
gameinfo = g
break
if gameinfo is None:
self.error( 404 )
self.render( "404.html", user=user )
return
params = dict( user=user, game=game_model.game, game_code=game_code,
category=gameinfo['category'], return_url=return_url )
params['username'] = gameinfo.get( 'bk_runner' )
if params['username'] is None:
params['username'] = ''
params['time'] = ''
params['datestr'] = ''
params['video'] = ''
params['updating'] = False
else:
params['time'] = util.seconds_to_timestr(
gameinfo.get( 'bk_seconds' ) )
params['datestr'] = gameinfo.get( 'bk_datestr' )
params['video'] = gameinfo.get( 'bk_video' )
params['updating'] = True
if return_url[ 0 : len( '/runner/' ) ] == '/runner/':
params['from_runnerpage'] = True
else:
params['from_runnerpage'] = False
self.render( "updatebkt.html", **params )
示例6: get_runinfo
def get_runinfo( self, username, game, category, no_refresh=False ):
key = self.get_runinfo_memkey( username, game, category )
runinfo = memcache.get( key )
if runinfo is None and not no_refresh:
# Not in memcache, so constrcut the runinfo dictionary
pb_run = None
avg_seconds = 0
num_runs = 0
try:
q = db.Query( runs.Runs,
projection=('seconds', 'date', 'video',
'version') )
q.ancestor( runs.key() )
q.filter('username =', username)
q.filter('game =', game)
q.filter('category =', category)
q.order('-date') # Cut off old runs
for run in q.run( limit = 100000 ):
num_runs += 1
avg_seconds += ( 1.0 / num_runs ) * (
run.seconds - avg_seconds )
if( pb_run is None or run.seconds <= pb_run.seconds ):
pb_run = run
runinfo = dict( username = username,
username_code = util.get_code( username ),
category = category,
category_code = util.get_code( category ),
pb_seconds = None,
pb_time = None,
pb_date = None,
num_runs = num_runs,
avg_seconds = avg_seconds,
avg_time = util.seconds_to_timestr(
avg_seconds, dec_places=0),
video = None )
except apiproxy_errors.OverQuotaError, msg:
logging.error( msg )
return self.OVER_QUOTA_ERROR
# Set the pb time
if pb_run:
runinfo['pb_seconds'] = pb_run.seconds
runinfo['pb_time'] = util.seconds_to_timestr( pb_run.seconds )
runinfo['pb_date'] = pb_run.date
runinfo['video'] = pb_run.video
runinfo['version'] = pb_run.version
if memcache.set( key, runinfo ):
logging.debug( "Set " + key + " in memcache" )
else:
logging.warning( "Failed to set " + key + " in memcache" )
示例7: get_runinfo
def get_runinfo(self, username, game, category, no_refresh=False):
key = self.get_runinfo_memkey(username, game, category)
runinfo = memcache.get(key)
if runinfo is None and not no_refresh:
# Not in memcache, so constrcut the runinfo dictionary
q = db.Query(runs.Runs, projection=("seconds", "date", "video", "version"))
q.ancestor(runs.key())
q.filter("username =", username)
q.filter("game =", game)
q.filter("category =", category)
q.order("-date") # Cut off old runs
pb_run = None
avg_seconds = 0
num_runs = 0
for run in q.run(limit=100000):
num_runs += 1
avg_seconds += (1.0 / num_runs) * (run.seconds - avg_seconds)
if pb_run is None or run.seconds <= pb_run.seconds:
pb_run = run
runinfo = dict(
username=username,
username_code=util.get_code(username),
category=category,
category_code=util.get_code(category),
pb_seconds=None,
pb_time=None,
pb_date=None,
num_runs=num_runs,
avg_seconds=avg_seconds,
avg_time=util.seconds_to_timestr(avg_seconds),
video=None,
)
# Set the pb time
if pb_run:
runinfo["pb_seconds"] = pb_run.seconds
runinfo["pb_time"] = util.seconds_to_timestr(pb_run.seconds)
runinfo["pb_date"] = pb_run.date
runinfo["video"] = pb_run.video
runinfo["version"] = pb_run.version
if memcache.set(key, runinfo):
logging.debug("Set " + key + " in memcache")
else:
logging.warning("Failed to set " + key + " in memcache")
elif runinfo is not None:
logging.debug("Got " + key + " from memcache")
return runinfo
示例8: update_runlist_for_runner_put
def update_runlist_for_runner_put( self, params ):
user = params[ 'user' ]
game = params[ 'game' ]
game_code = params[ 'game_code' ]
category = params[ 'category' ]
time = params[ 'time' ]
video = params[ 'video' ]
version = params[ 'version' ]
notes = params[ 'notes' ]
date = params[ 'date' ]
datetime_created = params[ 'datetime_created' ]
run_id = params[ 'run_id' ]
# Update runlist for runner in memcache
runlist = self.get_runlist_for_runner( user.username,
no_refresh=True )
if runlist is not None:
runlist.insert( 0, dict( run_id = run_id,
game = game,
game_code = game_code,
category = category,
category_code = util.get_code( category ),
time = time,
date = date,
datetime_created = datetime_created,
video = video,
version = version,
notes = notes ) )
runlist.sort( key=lambda x: util.get_valid_date( x['date'] ),
reverse=True )
self.update_cache_runlist_for_runner( user.username, runlist )
示例9: get_gamelist
def get_gamelist(self, no_refresh=False):
key = self.get_gamelist_memkey()
gamelist = memcache.get(key)
if gamelist is None and not no_refresh:
# Build the gamelist, which is a list of dictionaries where each
# dict gives the game, game_code and number of pbs for that game.
# The list is sorted by numbers of pbs for the game
gamelist = []
q = db.Query(games.Games, projection=("game", "num_pbs"))
q.ancestor(games.key())
q.order("-num_pbs")
q.order("game")
for game_model in q.run(limit=10000):
if game_model.num_pbs <= 0:
break
gamelist.append(
dict(game=game_model.game, game_code=util.get_code(game_model.game), num_pbs=game_model.num_pbs)
)
if memcache.set(key, gamelist):
logging.debug("Set gamelist in memcache")
else:
logging.warning("Failed to set new gamelist in memcache")
elif gamelist is not None:
logging.debug("Got gamelist from memcache")
return gamelist
示例10: get_runnerlist
def get_runnerlist(self, no_refresh=False):
key = self.get_runnerlist_memkey()
runnerlist = memcache.get(key)
if runnerlist is None and not no_refresh:
# Build the runnerlist, which is a list of dictionaries where each
# dict gives the username and number of pbs for that user.
# The list is sorted by numbers of pbs for the user.
runnerlist = []
q = db.Query(runners.Runners, projection=("username", "gravatar", "num_pbs"))
q.ancestor(runners.key())
q.order("-num_pbs")
q.order("username")
for runner in q.run(limit=100000):
runnerlist.append(
dict(
username=runner.username,
username_code=util.get_code(runner.username),
num_pbs=runner.num_pbs,
gravatar_url=util.get_gravatar_url(runner.gravatar),
)
)
if memcache.set(key, runnerlist):
logging.debug("Set runnerlist in memcache")
else:
logging.warning("Failed to set new runnerlist in memcache")
elif runnerlist is not None:
logging.debug("Got runnerlist from memcache")
return runnerlist
示例11: get
def get( self, username_code ):
user = self.get_user( )
# Make sure this is the correct user
if user is None:
self.error( 404 )
self.render( "404.html", user=user )
return
elif user == self.OVER_QUOTA_ERROR:
self.error( 403 )
self.render( "403.html" )
return
elif util.get_code( user.username ) != username_code:
self.error( 404 )
self.render( "404.html", user=user )
return
# Get the stored visible columns, or get the default ones
if user.visible_columns:
visible_columns = json.loads( user.visible_columns )
else:
visible_columns = util.get_default_visible_columns( )
self.render( "edit_table.html", user=user, username_code=username_code,
visible_columns=visible_columns )
示例12: get_runnerlist
def get_runnerlist( self, no_refresh=False ):
key = self.get_runnerlist_memkey( )
runnerlist = memcache.get( key )
if runnerlist is None and not no_refresh:
# Build the runnerlist, which is a list of dictionaries where each
# dict gives the username and number of pbs for that user.
# The list is sorted by numbers of pbs for the user.
runnerlist = [ ]
try:
q = db.Query( runners.Runners,
projection=('username', 'gravatar', 'num_pbs') )
q.ancestor( runners.key() )
q.order( '-num_pbs' )
q.order( 'username' )
for runner in q.run( limit=100000 ):
runnerlist.append(
dict( username = runner.username,
username_code = util.get_code( runner.username ),
num_pbs = runner.num_pbs,
gravatar_url = util.get_gravatar_url(
runner.gravatar ) ) )
except apiproxy_errors.OverQuotaError, msg:
logging.error( msg )
return self.OVER_QUOTA_ERROR
if memcache.set( key, runnerlist ):
logging.debug( "Set runnerlist in memcache" )
else:
logging.warning( "Failed to set new runnerlist in memcache" )
示例13: get_gamelist
def get_gamelist( self, no_refresh=False, get_num_pbs=True ):
key = self.get_gamelist_memkey( get_num_pbs )
gamelist = memcache.get( key )
if gamelist is None and not no_refresh:
# Build the gamelist, which is a list of dictionaries where each
# dict gives the game, game_code and number of pbs for that game.
# The list is sorted by numbers of pbs for the game
gamelist = [ ]
projection = [ 'game' ]
if get_num_pbs:
projection.append( 'num_pbs' )
try:
q = db.Query( games.Games, projection=projection )
q.ancestor( games.key() )
if get_num_pbs:
q.order( '-num_pbs' )
q.order( 'game' )
for game_model in q.run( limit=10000 ):
if get_num_pbs and game_model.num_pbs <= 0:
break
if get_num_pbs:
d = dict( game = game_model.game,
game_code = util.get_code( game_model.game ),
num_pbs = game_model.num_pbs )
gamelist.append( d )
else:
gamelist.append( str( game_model.game ) )
except apiproxy_errors.OverQuotaError, msg:
logging.error( msg )
return self.OVER_QUOTA_ERROR
if memcache.set( key, gamelist ):
logging.debug( "Set " + key + " in memcache" )
else:
logging.warning( "Failed to set new " + key + " in memcache" )
示例14: post
def post( self, username_code ):
user = self.get_user( )
# Make sure this is the correct user
if user is None:
self.error( 404 )
self.render( "404.html", user=user )
return
elif user == self.OVER_QUOTA_ERROR:
self.error( 403 )
self.render( "403.html" )
return
elif util.get_code( user.username ) != username_code:
self.error( 404 )
self.render( "404.html", user=user )
return
# Get the visible columns
visible_columns = util.get_default_visible_columns( )
for key in visible_columns:
checked = self.request.get( key + '_visible', default_value="no" )
if checked == "yes":
visible_columns[ key ] = True
else:
visible_columns[ key ] = False
# Store
user.visible_columns = json.dumps( visible_columns )
user.put( )
# Update memcache
self.update_cache_runner( username_code, user )
# That's all
self.redirect( "/runner/" + username_code )
示例15: get
def get( self, game_code ):
user = self.get_user( )
# Make sure this game exists
game_model = self.get_game_model( game_code )
if not game_model:
self.error( 404 )
self.render( "404.html", user=user )
return
# Find out if this user has run this game
if user is not None:
user_has_run = self.get_user_has_run( user.username,
game_model.game )
else:
user_has_run = False
gamepage = self.get_gamepage( game_model.game )
# Add gravatar images to the gamepage
for d in gamepage:
for run in d['infolist']:
runner = self.get_runner( util.get_code( run['username'] ) )
if runner is not None:
run['gravatar_url'] = util.get_gravatar_url(
runner.gravatar, size=20 )
if self.format == 'html':
self.render( "gamepage.html", user=user, game=game_model.game,
game_code=game_code, gamepage=gamepage,
user_has_run=user_has_run )
elif self.format == 'json':
self.render_json( gamepage )