storage2.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:InplusTrader_Linux 作者: zhengwsh 项目源码 文件源码
def fetch(self, dbName, ticker, start, end, output='list'):
        """

        """
        # check inputs' validity.
        if output not in ['df', 'list', 'json']:
            raise ValueError('[MONGOD]: Unsupported output type.')
        if dbName not in self._dbNames:
            raise ValueError('[MONGOD]: Unable to locate database name.')

        db = self._dbs[dbName]
        dbSelf = db['self']
        dbIndex = db['index']
        try:
            coll = db[ticker]
            if len(start)==8 and len(end)==8:
                # yyyymmdd, len()=8
                start = datetime.strptime(start, '%Y%m%d')
                end = datetime.strptime(end, '%Y%m%d')
            elif len(start)==14 and len(end)==14:
                # yyyymmdd HH:MM, len()=14
                start = datetime.strptime(start, '%Y%m%d %H:%M')
                end = datetime.strptime(end, '%Y%m%d %H:%M')
            else:
                pass
            docs = []

            # find in MongoDB.
            for doc in coll.find(filter={dbIndex: {'$lte': end,
                '$gte': start}}, projection={'_id': False}):
                docs.append(doc)

            if output == 'list':
                return docs[::-1]

        except Exception, e:
            msg = '[MONGOD]: Error encountered when fetching data' + \
                  'from MongoDB; '+ str(e)
            return -1
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号