Python pymongo.DESCENDING Examples
The following are 30
code examples of pymongo.DESCENDING().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pymongo
, or try the search function
.
Example #1
Source File: metadata_store.py From arctic with GNU Lesser General Public License v2.1 | 6 votes |
def pop(self, symbol): """ Delete current metadata of `symbol` Parameters ---------- symbol : `str` symbol name to delete Returns ------- Deleted metadata """ last_metadata = self.find_one({'symbol': symbol}, sort=[('start_time', pymongo.DESCENDING)]) if last_metadata is None: raise NoDataFoundException('No metadata found for symbol {}'.format(symbol)) self.find_one_and_delete({'symbol': symbol}, sort=[('start_time', pymongo.DESCENDING)]) mongo_retry(self.find_one_and_update)({'symbol': symbol}, {'$unset': {'end_time': ''}}, sort=[('start_time', pymongo.DESCENDING)]) return last_metadata
Example #2
Source File: test_version_store_corruption.py From arctic with GNU Lesser General Public License v2.1 | 6 votes |
def test_restore_append_overlapping_corrupts_last(library, library_name): large_ts = create_test_data(size=2000, cols=100, index=True, multiindex=False, random_data=True, random_ids=True) library.write(symbol, large_ts[0:1000]) library.snapshot('snap_write_a') library.append(symbol, large_ts[1000:1010]) library.restore_version(symbol, as_of='snap_write_a', prune_previous_version=True) library.append(symbol, large_ts[1000:1012]) last_v = library._versions.find_one(sort=[('version', pymongo.DESCENDING)]) vsu.analyze_symbol(library, symbol, 0, last_v['version'] + 1) # Verify no versions have been corrupted for v in library._versions.find(sort=[('version', pymongo.DESCENDING)]): library.read(symbol, as_of=v['version']) # This is not necessary to fix, but the Exception thrown is quite confusing.
Example #3
Source File: test_version_store_corruption.py From arctic with GNU Lesser General Public License v2.1 | 6 votes |
def test_restore_append_overlapping_corrupts_old(library, library_name): large_ts = create_test_data(size=2000, cols=100, index=True, multiindex=False, random_data=True, random_ids=True) library.write(symbol, large_ts[0:1000]) library.snapshot('snap_write_a') library.append(symbol, large_ts[1000:1010]) library.restore_version(symbol, as_of='snap_write_a', prune_previous_version=True) library.append(symbol, large_ts[1000:1009]) last_v = library._versions.find_one(sort=[('version', pymongo.DESCENDING)]) vsu.analyze_symbol(library, symbol, 0, last_v['version'] + 1) # Verify no versions have been corrupted for v in library._versions.find(sort=[('version', pymongo.DESCENDING)]): library.read(symbol, as_of=v['version'])
Example #4
Source File: test_version_store_corruption.py From arctic with GNU Lesser General Public License v2.1 | 6 votes |
def test_append_fail_after_delete_noupsert(library, library_name): large_ts = create_test_data(size=2000, cols=100, index=True, multiindex=False, random_data=True, random_ids=True) library.write(symbol, large_ts[0:1000]) #v1 library.snapshot('snap_a') library.append(symbol, large_ts[1000:1010]) #v2 library.snapshot('snap_b') library.append(symbol, large_ts[1010:1020]) #v3 library.snapshot('snap_c') library.append(symbol, large_ts[1030:1040]) #v4 library.delete(symbol) #v5 library.append(symbol, large_ts[1040:1050], upsert=False) # v6 last_v = library._versions.find_one(sort=[('version', pymongo.DESCENDING)]) vsu.analyze_symbol(library, symbol, 0, last_v['version'] + 1) # Verify no versions have been corrupted for v in library._versions.find(sort=[('version', pymongo.DESCENDING)]): library.read(symbol, as_of=v['version'])
Example #5
Source File: logger.py From picoCTF with MIT License | 6 votes |
def get_api_exceptions(result_limit=50): """ Retrieve the most recent logged exceptions. Args: result_limit: the maximum number of exceptions to return. Returns: list of exception dicts """ db = api.db.get_conn() results = ( db.exceptions.find({"visible": True}, {"_id": 0}) .sort([("time", pymongo.DESCENDING)]) .limit(result_limit) ) return list(results)
Example #6
Source File: files.py From fame with GNU General Public License v3.0 | 6 votes |
def index(self): """Get the list of objects. .. :quickref: File; Get the list of objects Response is paginated and will only contain 25 results. The most recent objects appear first. :query page: page number. :type page: int :>json list files: list of files (see :http:get:`/files/(id)` for details on the format of a file). """ page = int(request.args.get('page', 1)) files = current_user.files.find().sort('_id', DESCENDING).limit(PER_PAGE).skip((page - 1) * PER_PAGE) pagination = Pagination(page=page, per_page=PER_PAGE, total=files.count(), css_framework='bootstrap3') files = {'files': clean_files(list(files))} return render(files, 'files/index.html', ctx={'data': files, 'pagination': pagination})
Example #7
Source File: mongo.py From sacred with MIT License | 6 votes |
def insert(self): import pymongo.errors if self.overwrite: return self.save() autoinc_key = self.run_entry.get("_id") is None while True: if autoinc_key: c = self.runs.find({}, {"_id": 1}) c = c.sort("_id", pymongo.DESCENDING).limit(1) self.run_entry["_id"] = ( c.next()["_id"] + 1 if self.runs.count_documents({}, limit=1) else 1 ) try: self.runs.insert_one(self.run_entry) return except pymongo.errors.InvalidDocument as e: raise ObserverError( "Run contained an unserializable entry." "(most likely in the info)\n{}".format(e) ) except pymongo.errors.DuplicateKeyError: if not autoinc_key: raise
Example #8
Source File: DyStockMongoDbEngine.py From DevilYuan with MIT License | 6 votes |
def getAdjFactor(self, code, date, name=None): collection = self._getStockDaysDb()[code] dateEnd = datetime.strptime(date + ' 23:00:00', '%Y-%m-%d %H:%M:%S') flt = {'datetime':{'$lt':dateEnd}} try: cursor = collection.find(flt).sort('datetime', pymongo.DESCENDING).limit(1) except Exception as ex: self._info.print("MongoDB 异常({0}): 获取{1}:{2}, {3}复权因子".format(str(ex) + ', ' + str(ex.details), code, name, date), DyLogData.error) return None # get adjust factor for d in cursor: return d['adjfactor'] return None
Example #9
Source File: test_version_store.py From arctic with GNU Lesser General Public License v2.1 | 6 votes |
def test_append_corrupted_new_version(library, fw_pointers_cfg): with FwPointersCtx(fw_pointers_cfg): to_append = read_str_as_pandas(""" times | near 2012-11-09 17:06:11.040 | 30.0""") to_append_2 = read_str_as_pandas(""" times | near 2012-11-10 17:06:11.040 | 40.0""") library.write(symbol, ts1) # Append version library.append(symbol, to_append) # The append went wrong, and the new version document (written last), not available library._versions.find_one_and_delete({'symbol': symbol}, sort=[('version', pymongo.DESCENDING)]) # Should still be able to append new data library.append(symbol, to_append_2, upsert=True) assert library.read(symbol).data['near'][-1] == 40. assert len(library.read(symbol).data) == len(ts1) + 1
Example #10
Source File: DyStockMongoDbEngine.py From DevilYuan with MIT License | 6 votes |
def _getCodeDay(self, code, baseDate, name=None): """ 得到个股的当日交易日, 向前贪婪 """ collection = self._getStockDaysDb()[code] date = datetime.strptime(baseDate + ' 23:00:00', '%Y-%m-%d %H:%M:%S') flt = {'datetime': {'$lt': date}} sortMode = pymongo.DESCENDING try: cursor = collection.find(flt).sort('datetime', sortMode).limit(1) except Exception as ex: self._info.print("MongoDB Exception({0}): @_findOneCodeDaysByZeroRelative{1}:{2}, [{3}, {4}]日线数据".format(str(ex) + ', ' + str(ex.details), code, name, baseDate, n), DyLogData.error) return None for d in cursor: return d['datetime'].strftime('%Y-%m-%d') return None
Example #11
Source File: DyStockMongoDbEngine.py From DevilYuan with MIT License | 6 votes |
def _getTradeDaysByRelativeZero(self, baseDate): """ 基准日期向前找到第一个交易日 """ baseDateSave = baseDate collection = self._getTradeDayTableCollection() baseDate = datetime.strptime(baseDate, '%Y-%m-%d') flt = {'datetime':{'$lte':baseDate}} try: cursor = collection.find(flt).sort('datetime', pymongo.DESCENDING) except Exception as ex: self._info.print("MongoDB Exception({0}): @_getTradeDaysByRelativeZero({1})".format(str(ex) + ', ' + str(ex.details), baseDateSave), DyLogData.error) return None for d in cursor: if d['tradeDay']: return [d] return None
Example #12
Source File: mongomq.py From distributed_framework with Apache License 2.0 | 6 votes |
def next(self): """ Get next job from queue """ return self._wrap_one(self.collection.find_and_modify( query={"locked_by": None, "locked_at": None, "attempts": {"$lt": self.max_attempts}, "status": { "$eq": JobStatus.QUEUED} }, update={ "$set": { "locked_by": self.consumer_id, "locked_at": datetime.now(), 'status': JobStatus.STARTED } }, sort=[('priority', pymongo.DESCENDING)], new=1, limit=1 ))
Example #13
Source File: metadata_store.py From arctic with GNU Lesser General Public License v2.1 | 6 votes |
def read(self, symbol, as_of=None): """ Return current metadata saved for `symbol` Parameters ---------- symbol : `str` symbol name for the item as_of : `datetime.datetime` return entry valid at given time Returns ------- metadata """ if as_of is not None: res = self.find_one({'symbol': symbol, 'start_time': {'$lte': as_of}}, sort=[('start_time', pymongo.DESCENDING)]) else: res = self.find_one({'symbol': symbol}, sort=[('start_time', pymongo.DESCENDING)]) return res['metadata'] if res is not None else None
Example #14
Source File: Sharding.py From mongodb_consistent_backup with Apache License 2.0 | 6 votes |
def get_mongos(self, force=False): if not force and self.mongos_db: return self.mongos_db elif self.db.is_mongos(): return self.db else: db = self.connection['config'] for doc in db.mongos.find().sort('ping', DESCENDING): try: mongos_uri = MongoUri(doc['_id']) logging.debug("Found cluster mongos: %s" % mongos_uri) self.mongos_db = DB(mongos_uri, self.config, False, 'nearest') logging.info("Connected to cluster mongos: %s" % mongos_uri) return self.mongos_db except DBConnectionError: logging.debug("Failed to connect to mongos: %s, trying next available mongos" % mongos_uri) raise OperationError('Could not connect to any mongos!')
Example #15
Source File: database.py From counterblock with MIT License | 6 votes |
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)
Example #16
Source File: counterwallet.py From counterblock with MIT License | 6 votes |
def store_wallet_message(msg, msg_data, decorate=True): wallet_message = messages.decorate_message_for_feed(msg, msg_data=msg_data) if decorate else msg # use "optimistic loop" pattern to insert a new messages with an incrementing seq while True: last_seq = config.mongo_db.wallet_messages.find_one(sort=[("_id", pymongo.DESCENDING)])['_id'] new_seq = last_seq + 1 try: config.mongo_db.wallet_messages.insert({ '_id': new_seq, 'when': calendar.timegm(time.gmtime()), 'message': wallet_message, }) except pymongo.errors.DuplicateKeyError: continue else: logger.debug("store_wallet_message: stored {}".format(new_seq)) if config.state['cw_last_message_seq'] < new_seq: config.state['cw_last_message_seq'] = new_seq break # every so often, trim up the table if new_seq % 20 == 0: # for performance, don't do this every iteration if config.mongo_db.wallet_messages.count() > FUZZY_MAX_WALLET_MESSAGES_STORED: config.mongo_db.wallet_messages.remove({'_id': {'$lte': new_seq - FUZZY_MAX_WALLET_MESSAGES_STORED}})
Example #17
Source File: AnalysisReport.py From irwin with GNU Affero General Public License v3.0 | 5 votes |
def newestByUserId(self, userId): bson = self.playerReportColl.find_one( filter={'userId': userId}, sort=[('date', pymongo.DESCENDING)]) return None if bson is None else PlayerReportBSONHandler.reads(bson)
Example #18
Source File: db.py From cryptotrader with MIT License | 5 votes |
def create_indexes(self): self.clients.create_index([("date", pm.DESCENDING)]) self.funds.create_index([("date", pm.DESCENDING)]) self.withdrawals.create_index([("date", pm.DESCENDING)]) self.deposits.create_index([("date", pm.DESCENDING)])
Example #19
Source File: money_crawler.py From Bitcluster with MIT License | 5 votes |
def ensure_indexes(self): #Ensure index existence db = self.client.bitcoin collection = db.transactions collection.create_index([("source_n_id", ASCENDING)]) collection.create_index([("destination_n_id", ASCENDING)]) collection.create_index([("source", ASCENDING)]) collection.create_index([("destination", ASCENDING)]) collection.create_index([("block_id",DESCENDING)])
Example #20
Source File: EngineQueue.py From irwin with GNU Affero General Public License v3.0 | 5 votes |
def nextUnprocessed(self, name: AuthID) -> Opt[EngineQueue]: """find the next job to process against owner's name""" incompleteBSON = self.engineQueueColl.find_one({'owner': name, 'completed': {'$ne': True}}) if incompleteBSON is not None: # owner has unfinished business logging.debug(f'{name} is returning to complete {incompleteBSON}') return EngineQueueBSONHandler.reads(incompleteBSON) engineQueueBSON = self.engineQueueColl.find_one_and_update( filter={'owner': None, 'completed': False}, update={'$set': {'owner': name}}, sort=[("precedence", pymongo.DESCENDING), ("date", pymongo.ASCENDING)]) return None if engineQueueBSON is None else EngineQueueBSONHandler.reads(engineQueueBSON)
Example #21
Source File: AnalysisReport.py From irwin with GNU Affero General Public License v3.0 | 5 votes |
def byPlayerId(self, userId): return [PlayerReportBSONHandler.reads(bson) for bson in self.playerReportColl.find( filter={'userId': userId}, sort=[('date', pymongo.DESCENDING)])]
Example #22
Source File: EngineQueue.py From irwin with GNU Affero General Public License v3.0 | 5 votes |
def top(self, amount: int = 20) -> List[EngineQueue]: """Return the top `amount` of players, ranked by precedence""" bsons = self.engineQueueColl.find( filter={'complete': False}, sort=[("precedence", pymongo.DESCENDING), ("date", pymongo.ASCENDING)]).limit(amount) return [EngineQueueBSONHandler.reads(b) for b in bsons]
Example #23
Source File: assets.py From counterblock with MIT License | 5 votes |
def init(): # init db and indexes # asset_extended_info config.mongo_db.asset_extended_info.ensure_index('asset', unique=True) config.mongo_db.asset_extended_info.ensure_index('info_status') # balance_changes config.mongo_db.balance_changes.ensure_index('block_index') config.mongo_db.balance_changes.ensure_index([ ("address", pymongo.ASCENDING), ("asset", pymongo.ASCENDING), ("block_index", pymongo.DESCENDING), ("_id", pymongo.DESCENDING) ]) #config.mongo_db.balance_changes.ensure_index([ # ("address", pymongo.ASCENDING), # ("asset_longname", pymongo.ASCENDING), #]) try: # drop unnecessary indexes if they exist config.mongo_db.balance_changes.drop_index('address_1_asset_1_block_time_1') except: pass # tracked_assets config.mongo_db.tracked_assets.ensure_index('asset', unique=True) config.mongo_db.tracked_assets.ensure_index('_at_block') # for tracked asset pruning config.mongo_db.tracked_assets.ensure_index([ ("owner", pymongo.ASCENDING), ("asset", pymongo.ASCENDING), ]) # feeds (also init in betting module) config.mongo_db.feeds.ensure_index('source') config.mongo_db.feeds.ensure_index('owner') config.mongo_db.feeds.ensure_index('category') config.mongo_db.feeds.ensure_index('info_url')
Example #24
Source File: DyStockMongoDbEngine.py From DevilYuan with MIT License | 5 votes |
def getDaysLatestTradeDay(self): """ 获取数据库里交易日数据的最新交易日 """ cursor = self._findTradeDays() if cursor is None: return None cursor = cursor.sort('datetime', pymongo.DESCENDING) for d in cursor: if d['tradeDay']: return d return None
Example #25
Source File: DyStockMongoDbEngine.py From DevilYuan with MIT License | 5 votes |
def _findOneCodeDaysByRelative(self, code, baseDate, n=0, name=None): """ 包含当日,也就是说offset 0总是被包含的 """ # 获取当日日期 baseDay = self._getCodeDay(code, baseDate, name) if baseDay is None: return None collection = self._getStockDaysDb()[code] if n <= 0: date = datetime.strptime(baseDay + ' 23:00:00', '%Y-%m-%d %H:%M:%S') flt = {'datetime':{'$lt':date}} sortMode = pymongo.DESCENDING else: date = datetime.strptime(baseDay, '%Y-%m-%d') flt = {'datetime':{'$gte':date}} # ignore baseDate, no matter its in DB or not sortMode = pymongo.ASCENDING # 向前贪婪 n = abs(n) + 1 try: cursor = collection.find(flt).sort('datetime', sortMode).limit(n) except Exception as ex: self._info.print("MongoDB Exception({0}): @_findOneCodeDaysByRelative{1}:{2}, [{3}, {4}]日线数据".format(str(ex) + ', ' + str(ex.details), code, name, baseDate, n), DyLogData.error) return None # We don't check any thing about if we actually get n days data. # The reason is that we don't know future, as well as 何时股票上市 return cursor
Example #26
Source File: DyStockMongoDbEngine.py From DevilYuan with MIT License | 5 votes |
def _getTradeDaysByRelativeNegative(self, baseDate, n): baseDateSave = baseDate nSave = n # always get 0 offset trade day baseDate = self._getTradeDaysByRelativeZero(baseDate) if baseDate is None: return None # find forward n trade days collection = self._getTradeDayTableCollection() flt = {'datetime':{'$lt':baseDate[0]['datetime']}} try: cursor = collection.find(flt).sort('datetime', pymongo.DESCENDING) except Exception as ex: self._info.print("MongoDB Exception({0}): @_getTradeDaysByRelativeNegative({1}, {2})".format(str(ex) + ', ' + str(ex.details), baseDateSave, nSave), DyLogData.error) return None dates = [baseDate[0]] for d in cursor: if d['tradeDay']: dates.append(d) n += 1 if n == 0: return dates self._info.print("数据库里没有{0}向前{1}个交易日的日期数据".format(baseDateSave, abs(nSave)), DyLogData.error) return None
Example #27
Source File: DB.py From mongodb_consistent_backup with Apache License 2.0 | 5 votes |
def get_oplog_tail_ts(self): logging.debug("Gathering youngest 'ts' in %s oplog" % self.uri) return self.get_oplog_rs().find_one(sort=[('$natural', DESCENDING)])['ts']
Example #28
Source File: messages.py From counterblock with MIT License | 5 votes |
def decorate_message(message, for_txn_history=False): # insert custom fields in certain events... # even invalid actions need these extra fields for proper reporting to the client (as the reporting message # is produced via PendingActionViewModel.calcText) -- however make it able to deal with the queried data not existing in this case assert '_category' in message if for_txn_history: message['_command'] = 'insert' # history data doesn't include this block_index = message['block_index'] if 'block_index' in message else message['tx1_block_index'] message['_block_time'] = database.get_block_time(block_index) message['_tx_index'] = message['tx_index'] if 'tx_index' in message else message.get('tx1_index', None) if message['_category'] in ['bet_expirations', 'order_expirations', 'bet_match_expirations', 'order_match_expirations']: message['_tx_index'] = 0 # add tx_index to all entries (so we can sort on it secondarily in history view), since these lack it # include asset extended information (longname and divisible) for attr in ('asset', 'get_asset', 'give_asset', 'forward_asset', 'backward_asset', 'dividend_asset'): if attr not in message: continue asset_info = config.mongo_db.tracked_assets.find_one({'asset': message[attr]}) message['_{}_longname'.format(attr)] = asset_info['asset_longname'] if asset_info else None message['_{}_divisible'.format(attr)] = asset_info['divisible'] if asset_info else None if message['_category'] in ['credits', 'debits']: # find the last balance change on record bal_change = config.mongo_db.balance_changes.find_one( {'address': message['address'], 'asset': message['asset']}, sort=[("block_time", pymongo.DESCENDING)]) message['_quantity_normalized'] = abs(bal_change['quantity_normalized']) if bal_change else None message['_balance'] = bal_change['new_balance'] if bal_change else None message['_balance_normalized'] = bal_change['new_balance_normalized'] if bal_change else None if message['_category'] in ['orders', 'order_matches', ]: message['_btc_below_dust_limit'] = ( ('forward_asset' in message and message['forward_asset'] == config.BTC and message['forward_quantity'] <= config.ORDER_BTC_DUST_LIMIT_CUTOFF) or ('backward_asset' in message and message['backward_asset'] == config.BTC and message['backward_quantity'] <= config.ORDER_BTC_DUST_LIMIT_CUTOFF) ) if message['_category'] in ['issuances', ]: message['_quantity_normalized'] = blockchain.normalize_quantity(message['quantity'], message['divisible']) return message
Example #29
Source File: transaction_stats.py From counterblock with MIT License | 5 votes |
def init(): # init db and indexes # transaction_stats config.mongo_db.transaction_stats.ensure_index([ # blockfeed.py, api.py ("when", pymongo.ASCENDING), ("category", pymongo.DESCENDING) ]) config.mongo_db.transaction_stats.ensure_index('block_index')
Example #30
Source File: models.py From iHealth_site with GNU General Public License v2.0 | 5 votes |
def find_recommendArticle(self, labels): '''根据用户的兴趣label选择文章序列并返回''' #labels按照值降序排列,放在label_list中 label_list = sorted(labels.items(), key=lambda e:e[1], reverse=True) #只取前5个兴趣label if len(label_list) > 5: label_list = label_list[0:5] #返回用户感兴趣的所有文章 #article_C 为放置各个分类文章的容器 article_C = [] for category in label_list: article_C.append((self.articles.find({'category':category[0] }).sort('_id', pymongo.DESCENDING))) return article_C