本文整理汇总了Python中models.Stock.get_by_id方法的典型用法代码示例。如果您正苦于以下问题:Python Stock.get_by_id方法的具体用法?Python Stock.get_by_id怎么用?Python Stock.get_by_id使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Stock
的用法示例。
在下文中一共展示了Stock.get_by_id方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _process_removed_stocks
# 需要导入模块: from models import Stock [as 别名]
# 或者: from models.Stock import get_by_id [as 别名]
def _process_removed_stocks(sheet, ssheet_id, symbols):
"""
Update database to reflect sheet's new stocks, changed bounds, or changed order.
:param sheet: Instance of models.Sheet to modify.
:param ssheet_id: Spreadsheet id.
:param symbols: Iterable of symbols.
:return: list of ndb Entities to put, list of ndb Entities to delete.
"""
to_put, to_delete = [], []
symbols_old = {k.id() for k in sheet.stock_keys}
for symbol in symbols_old - set(symbols):
stock = Stock.get_by_id(symbol)
sheetstock_key = ndb.Key('SheetStock', '_'.join([ssheet_id, symbol]))
to_delete.append(sheetstock_key)
if stock and sheet.key in stock.sheet_keys:
stock.sheet_keys.remove(sheet.key)
if stock.sheet_keys:
to_put.append(stock)
else:
to_delete.append(stock.key)
return to_put, to_delete
示例2: _update_stocks
# 需要导入模块: from models import Stock [as 别名]
# 或者: from models.Stock import get_by_id [as 别名]
def _update_stocks(symbols):
"""
Update models.Stock entities.
:param symbols: Iterable of stock symbols to update.
:return: dict mapping symbols to new prices, dict mapping symbols to (old price, new price) for changed symbols.
"""
to_put = []
symbol_price_updated_dict = {}
if symbols is None:
stocks_ = list(Stock.query())
symbols = [s.key.id() for s in stocks_]
else:
stocks_ = [Stock.get_by_id(s) for s in symbols]
symbol_price_dict = stocks.get_stock_prices(symbols)
for stock in stocks_:
if stock is None:
continue
symbol = stock.key.id()
price = symbol_price_dict.get(symbol, None)
if stock.price != price:
symbol_price_updated_dict[symbol] = (stock.price, price)
stock.price = price
to_put.append(stock)
ndb.put_multi(to_put)
return symbol_price_dict, symbol_price_updated_dict