本文整理匯總了Python中pymongo.ASCENDING屬性的典型用法代碼示例。如果您正苦於以下問題:Python pymongo.ASCENDING屬性的具體用法?Python pymongo.ASCENDING怎麽用?Python pymongo.ASCENDING使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類pymongo
的用法示例。
在下文中一共展示了pymongo.ASCENDING屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _get_collection
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import ASCENDING [as 別名]
def _get_collection(self, collection_name=None):
if collection_name:
if collection_name in self.allowed_collections:
coins = self.client.ratesx[collection_name]
else:
raise KeyError("Not allowed collection name: %s" % collection_name)
else:
coins = self.coins
if collection_name \
and collection_name not in self.client.ratesx.collection_names():
if collection_name.startswith('coins_'):
coins.create_index([('symbol', ASCENDING)], unique=False)
coins.create_index([('timestamp', ASCENDING)], unique=False)
coins.create_index([('symbol', ASCENDING),
('timestamp', ASCENDING)], unique=True)
if collection_name.startswith('currencies_'):
coins.create_index([('timestamp', ASCENDING)], unique=False)
return coins
示例2: _ensure_index
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import ASCENDING [as 別名]
def _ensure_index(collection):
try:
collection.create_index([('symbol', pymongo.HASHED)], background=True)
# We keep it only for its uniqueness
collection.create_index([('symbol', pymongo.ASCENDING),
('sha', pymongo.ASCENDING)], unique=True, background=True)
# TODO: When/if we remove the segments->versions pointers implementation and keep only the forward pointers,
# we can remove the 'parent' from the index.
collection.create_index([('symbol', pymongo.ASCENDING),
('parent', pymongo.ASCENDING),
('segment', pymongo.ASCENDING)], unique=True, background=True)
# Used for efficient SHA-based read queries that have index ranges
collection.create_index([('symbol', pymongo.ASCENDING),
('sha', pymongo.ASCENDING),
('segment', pymongo.ASCENDING)], unique=True, background=True)
except OperationFailure as e:
if "can't use unique indexes" in str(e):
return
raise
示例3: test_rename
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import ASCENDING [as 別名]
def test_rename(chunkstore_lib):
df = create_test_data(size=10, cols=5)
chunkstore_lib.write('test', df, chunk_size='D')
assert_frame_equal(chunkstore_lib.read('test'), df)
chunkstore_lib.rename('test', 'new_name')
assert_frame_equal(chunkstore_lib.read('new_name'), df)
with pytest.raises(Exception) as e:
chunkstore_lib.rename('new_name', 'new_name')
assert('already exists' in str(e.value))
with pytest.raises(NoDataFoundException) as e:
chunkstore_lib.rename('doesnt_exist', 'temp')
assert('No data found for doesnt_exist' in str(e.value))
assert('test' not in chunkstore_lib.list_symbols())
# read out all chunks that have symbol set to 'test'. List should be empty
chunks = []
for x in chunkstore_lib._collection.find({SYMBOL: 'test'}, sort=[(START, pymongo.ASCENDING)],):
chunks.append(x)
assert(len(chunks) == 0)
示例4: list
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import ASCENDING [as 別名]
def list(self, request):
await require(request, Permissions.view)
possible_fields = [k.name for k in self._schema.keys]
q = validate_query(request.query, possible_fields)
paging = calc_pagination(q, self._primary_key)
filters = q.get('_filters')
query = {}
if filters:
query = create_filter(filters, self._schema)
sort_direction = ASCENDING if paging.sort_dir == ASC else DESCENDING
cursor = (self._collection.find(query)
.skip(paging.offset)
.limit(paging.limit)
.sort(paging.sort_field, sort_direction))
entities = await cursor.to_list(paging.limit)
count = await self._collection.count_documents(query)
headers = {'X-Total-Count': str(count)}
return json_response(entities, headers=headers)
示例5: get_block_indexes_for_dates
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import ASCENDING [as 別名]
def get_block_indexes_for_dates(start_dt=None, end_dt=None):
"""Returns a 2 tuple (start_block, end_block) result for the block range that encompasses the given start_date
and end_date unix timestamps"""
if start_dt is None:
start_block_index = config.BLOCK_FIRST
else:
start_block = config.mongo_db.processed_blocks.find_one({"block_time": {"$lte": start_dt}}, sort=[("block_time", pymongo.DESCENDING)])
start_block_index = config.BLOCK_FIRST if not start_block else start_block['block_index']
if end_dt is None:
end_block_index = config.state['my_latest_block']['block_index']
else:
end_block = config.mongo_db.processed_blocks.find_one({"block_time": {"$gte": end_dt}}, sort=[("block_time", pymongo.ASCENDING)])
if not end_block:
end_block_index = config.mongo_db.processed_blocks.find_one(sort=[("block_index", pymongo.DESCENDING)])['block_index']
else:
end_block_index = end_block['block_index']
return (start_block_index, end_block_index)
示例6: updateTradeDays
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import ASCENDING [as 別名]
def updateTradeDays(self, dates):
collection = self._getTradeDayTableCollection()
# create index
collection.create_index([('datetime', pymongo.ASCENDING)], unique=True)
# update into DB
try:
for date in dates:
flt = {'datetime': date['datetime']}
result = collection.update_one(flt, {'$set':{'tradeDay': date['tradeDay']}}, upsert=True)
if not (result.acknowledged and (result.matched_count == 1 or result.upserted_id is not None)):
self._info.print("更新交易日數據到MongoDB失敗: date={}, raw_result={}".format(date, result.raw_result), DyLogData.error)
return False
except Exception as ex:
self._info.print("更新交易日數據到MongoDB異常: {}".format(str(ex) + ', ' + str(ex.details)), DyLogData.error)
return False
return True
示例7: updateStockCodes
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import ASCENDING [as 別名]
def updateStockCodes(self, codes):
collection = self._getCodeTableCollection()
# create index
collection.create_index([('code', pymongo.ASCENDING)], unique=True)
# update into DB
try:
for code in codes:
flt = {'code': code['code']}
collection.update_one(flt, {'$set':{'name': code['name']}}, upsert=True)
except Exception as ex:
self._info.print("更新股票代碼數據到MongoDB異常:{0}".format(str(ex) + ', ' + str(ex.details)), DyLogData.error)
return False
return True
示例8: getStockMarketDate
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import ASCENDING [as 別名]
def getStockMarketDate(self, code, name=None):
"""
獲取個股上市日期
由於數據庫的數據限製,有可能是個股數據在數據庫裏的最早信息
"""
collection = self._getStockDaysDb()[code]
flt = {'datetime': {'$lt':datetime.now()}}
try:
cursor = collection.find(flt).sort('datetime', pymongo.ASCENDING).limit(1)
except Exception as ex:
self._info.print("MongoDB Exception({0}): @getStockMarketDate{1}:{2}, 日線數據".format(str(ex) + ', ' + str(ex.details),
code, name),
DyLogData.error)
return None
for d in cursor:
return d['datetime'].strftime('%Y-%m-%d')
return None
示例9: updateSectorStockCodes
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import ASCENDING [as 別名]
def updateSectorStockCodes(self, sectorCode, date, codes):
collection = self._client[self.sectorCodeDbMap[sectorCode]][date]
# create index
collection.create_index([('code', pymongo.ASCENDING)], unique=True)
# update into DB
try:
for code in codes:
flt = {'code': code['code']}
collection.update_one(flt, {'$set': {'name': code['name']}}, upsert=True)
except Exception as ex:
self._info.print("更新[{0}]股票代碼數據[{1}]到MongoDB異常:{2}".format(DyStockCommon.sectors[sectorCode], date, str(ex) + ', ' + str(ex.details)), DyLogData.error)
return False
return True
示例10: save_position
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import ASCENDING [as 別名]
def save_position(message, collection=DATABASE.positions):
"""save account
Arguments:
message {[type]} -- [description]
Keyword Arguments:
collection {[type]} -- [description] (default: {DATABASE})
"""
try:
collection.create_index(
[("account_cookie", ASCENDING), ("portfolio_cookie", ASCENDING), ("user_cookie", ASCENDING), ("position_id", ASCENDING)], unique=True)
except:
pass
collection.update(
{'account_cookie': message['account_cookie'], 'position_id': message['position_id'],
'portfolio_cookie': message['portfolio_cookie'], 'user_cookie': message['user_cookie']},
{'$set': message},
upsert=True
)
示例11: save_account
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import ASCENDING [as 別名]
def save_account(message, collection=DATABASE.account):
"""save account
Arguments:
message {[type]} -- [description]
Keyword Arguments:
collection {[type]} -- [description] (default: {DATABASE})
"""
try:
collection.create_index(
[("account_cookie", ASCENDING), ("user_cookie", ASCENDING), ("portfolio_cookie", ASCENDING)], unique=True)
except:
pass
collection.update(
{'account_cookie': message['account_cookie'], 'portfolio_cookie':
message['portfolio_cookie'], 'user_cookie': message['user_cookie']},
{'$set': message},
upsert=True
)
示例12: extract
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import ASCENDING [as 別名]
def extract(self): # TODO : Should be an exporter plugin
graph = {
'meta': {}, # self.__meta,
'properties': {} # self.__properties
}
graph['nodes'] = list()
for v in self.__vertices.find().sort('id', pymongo.ASCENDING):
v.pop("_id") # Remove MongoDB document ID
graph['nodes'].append(v)
graph['edges'] = list()
for e in self.__edges.find().sort("src", pymongo.ASCENDING):
e.pop("_id") # Remove MongoDB document ID
graph['edges'].append(e)
graph['tokens'] = list();
for t in self.__tokens.find().sort('id', pymongo.ASCENDING):
t.pop("_id") # Remove MongoDB document ID
t['id'] = str(t['id'])
t['ts'] = time.mktime(t['ts'].timetuple())
graph['tokens'].append(t)
return graph
示例13: get_data
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import ASCENDING [as 別名]
def get_data(self, symbol, start, end, symbol_in_column=True):
"""
Returns a dataframe of the symbol data requested.
"""
from pymongo import ASCENDING
symbol = str(symbol).upper()
results = self.database[symbol].find({'_id': \
{'$gte': start, '$lte': end}}\
).sort('datetime', ASCENDING)
ret = pd.DataFrame.from_dict(list(results))
if len(ret) < 1:
raise NoDataException()
ret.rename(columns={'open': 'Open', \
'high': 'High', \
'low': 'Low', \
'close': 'Close', \
'volume': 'Volume', \
'adj_close': 'Adj Close', \
'_id': 'Date'}, \
inplace=True)
ret = ret.set_index('Date')
if symbol_in_column:
ret.rename(columns=lambda name: '%s_%s' %(symbol, name), inplace=True)
return ret
示例14: _ensure_index
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import ASCENDING [as 別名]
def _ensure_index(self):
collection = self._collection
collection.create_index([(SYMBOL, pymongo.ASCENDING),
(START, pymongo.ASCENDING)], background=True)
collection.create_index([(START, pymongo.ASCENDING)], background=True)
self._metadata.create_index([(SYMBOL, pymongo.ASCENDING)], background=True, unique=True)
示例15: min_date
# 需要導入模塊: import pymongo [as 別名]
# 或者: from pymongo import ASCENDING [as 別名]
def min_date(self, symbol):
"""
Return the minimum datetime stored for a particular symbol
Parameters
----------
symbol : `str`
symbol name for the item
"""
res = self._collection.find_one({SYMBOL: symbol}, projection={ID: 0, START: 1},
sort=[(START, pymongo.ASCENDING)])
if res is None:
raise NoDataFoundException("No Data found for {}".format(symbol))
return utc_dt_to_local_dt(res[START])