def scoring():
#go through all predictions, check if should be scored
predictions = Prediction.query(
ndb.AND(Prediction.outcome != "UNKNOWN", Prediction.resolved == False)).fetch()
audit = []
# Get all trades by prediction_id
for p in predictions:
resolve = p.outcome
trades = Trade.query(Trade.prediction_id == p.key).fetch()
# Get user id from those trades
users = [trade.user_id.get() for trade in trades]
for u in users:
# check user ledger, map outcome to 1 or 0 based on prediction outcome
ledger = [i for i in u.user_ledger if i.prediction_id == p.key.urlsafe()]
if resolve == 'CONTRACT_ONE':
earned = ledger[0].contract_one
else:
earned = ledger[0].contract_two
u.balance += earned
audit.append({'user': u, 'earned': earned})
u.put()
p.resolved = True
p.put()
return audit
python类AND的实例源码
def gae_ndb_delete_expired_sessions(dormant_for=86400, limit=500):
"""Deletes expired sessions
A session is expired if it expires date is set and has passed or
if it has not been accessed for a given period of time.
max_age: seconds since last access to delete sessions, defaults
to 24 hours.
limit: amount to delete in one call of the method, the maximum
and default for this is the NDB fetch limit of 500"""
from vishnu.backend.client.ndb.gae import VishnuSession
from google.appengine.ext import ndb
from datetime import datetime
from datetime import timedelta
now = datetime.utcnow()
last_accessed = now - timedelta(seconds=dormant_for)
query = VishnuSession.query(ndb.OR(
ndb.AND(VishnuSession.expires <= now, VishnuSession.expires != None),
VishnuSession.last_accessed <= last_accessed
))
results = query.fetch(keys_only=True, limit=limit)
ndb.delete_multi(results)
return len(results) < limit
def query_display(cls, user_id, delta_minutes=60):
"""
Query all jobs that have state scheduled, queued or sent (but not done)
OR are done and have been scheduled for no longer than delta_minutes
ago.
"""
shortly_ago = datetime.datetime.utcnow() - datetime.timedelta(
minutes=delta_minutes)
# query all jobs that are
return cls.query(ndb.OR(cls.state.IN(['scheduled', 'queued', 'sent']),
ndb.AND(cls.scheduled_at >= shortly_ago,
cls.state == 'done')),
cls.user_id == user_id)
def price():
predictions = Prediction.query(
ndb.AND(Prediction.outcome == "UNKNOWN", Prediction.resolved == False)).fetch()
input_date = datetime.datetime.now()
for p in predictions:
price = Price(prediction_id=p.key,
date=input_date,
value=p.GetPriceByPredictionId())
price.put()
def GetTradesForPredictionId(prediction_id):
user = users.get_current_user()
trades = Trade.query(ndb.AND(Trade.prediction_id == ndb.Key(urlsafe=prediction_id),
Trade.user_id == ndb.Key('Profile', user.user_id()))).fetch()
return str(trades)
def query_article_nested():
query = Article.query(ndb.AND(Article.tags == 'python',
ndb.OR(Article.tags.IN(['ruby', 'jruby']),
ndb.AND(Article.tags == 'php',
Article.tags != 'perl'))))
return query
def exist_by_user_ids( cls, user_a_id, user_b_id ):
count = cls.query( ndb.AND( cls.friends == user_a_id, cls.friends == user_b_id )).count( 1 )
return count > 0
def get_key_by_user_ids( cls, user_a_id, user_b_id ):
return cls.query( ndb.AND( cls.friends == user_a_id, cls.friends == user_b_id )).get( keys_only = True )
#=== UTILITIES ================================================================
def query_by_user_id( cls, user_id ):
return cls.query( ndb.OR( ndb.AND( cls.user_id == user_id,
cls.time_updated > ( datetime.datetime.now() - datetime.timedelta( seconds = settings.SESSION_MAX_IDLE_AGE ))),
ndb.AND( cls.user_id == user_id,
cls.stay_logged_in == True,
cls.time_updated > ( datetime.datetime.now() - datetime.timedelta( seconds = settings.SESSION_MAX_IDLE_AGE_STAY_LOGGED_IN )))))
def fetch_keys_expired( cls ):
return cls.query( ndb.OR( cls.time_updated > ( datetime.datetime.now() - datetime.timedelta( seconds = settings.SESSION_MAX_IDLE_AGE_STAY_LOGGED_IN )),
ndb.AND( cls.stay_logged_in == False,
cls.time_updated > ( datetime.datetime.now() - datetime.timedelta( seconds = settings.SESSION_MAX_IDLE_AGE ))))
).fetch( keys_only = True )
#=== UTILITIES ================================================================
def get_by_user_id_app_id_data_type_data_id( cls, user_id, app_id, data_type, data_id ):
return cls.query( ndb.AND( cls.user_id == user_id, cls.app_id == app_id,
cls.data_type == data_type, cls.data_id == data_id )).get()
def fetch_by_user_id_app_id( cls, user_id, app_id ):
return cls.query( ndb.AND( cls.user_id == user_id,
cls.app_id == app_id )).fetch( keys_only = True )
def fetch_by_user_id_app_id_data_type_read_access_not_expired( cls, user_id, app_id, data_type, read_access ):
return cls.query( ndb.AND( cls.user_id == user_id,
cls.app_id == app_id,
cls.data_type == data_type,
cls.read_access == read_access,
cls.time_expires > datetime.datetime.now())).fetch()
def fetch_by_app_id_data_type_read_access_not_expired( cls, app_id, data_type, read_access ):
return cls.query( ndb.AND( cls.app_id == app_id,
cls.data_type == data_type,
cls.read_access == read_access,
cls.time_expires > datetime.datetime.now())).fetch()
def fetch_by_user_id_app_id_data_type_data_id( cls, user_id, app_id, data_type, data_id ):
return cls.query( ndb.AND( cls.user_id == user_id,
cls.app_id == app_id,
cls.data_type == data_type,
cls.data_id == data_id )).fetch( keys_only = True )
def exist_product_by_activator( cls, user_id, product_name ):
count = cls.query( ndb.AND( cls.activated_by_user == user_id, cls.product_name == product_name )).count( 1 )
return count > 0
def exist_by_purchaser_not_activated( cls, user_id ):
count = cls.query( ndb.AND( cls.purchaser_user_id == user_id, cls.activated_by_user == -1 )).count( 1 )
return count > 0
def count_by_purchaser_not_activated( cls, user_id ):
return cls.query( ndb.AND( cls.purchaser_user_id == user_id, cls.activated_by_user == -1)).count()
def count_by_shop_name_order_type_activated( cls, shop_name, order_type ):
return cls.query( ndb.AND( cls.shop_name == shop_name, cls.order_type == order_type, cls.activated_by_user >= 0 )).count()
def count_by_shop_name_order_type_not_activated( cls, shop_name, order_type ):
return cls.query( ndb.AND( cls.shop_name == shop_name, cls.order_type == order_type, cls.activated_by_user == -1 )).count()
#=== UTILITIES ================================================================
def get_by_user_id_email( cls, user_id, email ):
return cls.query( ndb.AND( cls.user_id == user_id, cls.email == email )).get()
def get_by_user_id_route( cls, user_id, route ):
return cls.query( ndb.AND( cls.user_id == user_id, cls.route == route )).get()
def exist_by_sender_recipient( cls, sender_id, recipient_id ):
count = cls.query( ndb.AND( cls.sender == sender_id, cls.recipient == recipient_id )).count( 1 )
return count > 0
def get_key_by_sender_recipient( cls, sender_id, recipient_id ):
return cls.query( ndb.AND( cls.sender == sender_id, cls.recipient == recipient_id )).get( keys_only = True )
def get_by_sender_recipient( cls, sender_id, recipient_id ):
return cls.query( ndb.AND( cls.sender == sender_id, cls.recipient == recipient_id )).get()
def get_by_user_id_token( cls, user_id, token ):
return cls.query( ndb.AND( cls.user_id == user_id, cls.token == token )).get()
def fetch_by_prefix_lower_current( cls, prefix_lower ):
return cls.query( ndb.AND( cls.prefix_lower == prefix_lower, cls.current == True )).fetch()
def exist_by_prefix_lower_suffix( cls, prefix_lower, suffix ):
count = cls.query( ndb.AND( cls.prefix_lower == prefix_lower, cls.suffix == suffix )).count( 1 )
return count > 0
def get_by_user_id_email_type( cls, user_id, email, type ):
return cls.query( ndb.AND( cls.user_id == user_id, cls.email == email, cls.type == type )).get()
def get_by_user_id_auth_id_type( cls, user_id, auth_id, type ):
return cls.query( ndb.AND( cls.user_id == user_id, cls.auth_ids_provider == auth_id, cls.type == type )).get()