Exemple #1
0
    def get(object_type=None, object_id=None):
        error_message = None

        # validate desired object_type
        if object_type is None or object_type not in constants.OBJECT_TYPES:
            error_message = "object_type in /mongo/<object_type>/<ObjectId> must be one of {}".format(constants.OBJECT_TYPES.keys())
            return error_message, httplib.BAD_REQUEST

        # determine whether to return desired object or all objects
        get_all = False if object_id else True

        # try to get data
        result = None
        try:
            collection = MONGO_COLLECTION["ripe-" + object_type]
            if get_all:
                result = {}
                for doc in collection.find():
                    converted_doc = utils.serialize_dict(doc)
                    doc_id = converted_doc["_id"]
                    result[doc_id] = converted_doc
            else:
                item = utils.serialize_dict(collection.find_one({"_id": ObjectId(object_id)}))
                result = item
        except Exception as e:
            error_message = "Error finding ObjectId={}; {}".format(object_id, e)
            return error_message, httplib.BAD_REQUEST

        return result, httplib.CREATED
Exemple #2
0
 def _get_random_doc(collection):
     random_pipeline = [{"$sample": {
         "size": 1}
     }]
     random_cursor = {"batchSize": 1}
     sample_document = collection.aggregate(pipeline=random_pipeline, cursor=random_cursor)
     sample_document = sample_document.next()
     sample_document = utils.serialize_dict(sample_document)
     return sample_document
Exemple #3
0
    def get():
        all_docs = {}
        data_sets = [MONGO_USERS, MONGO_MEDIA]

        for data_set in data_sets:
            for doc in data_set.find():
                converted_doc = utils.serialize_dict(doc)
                doc_id = converted_doc["_id"]
                all_docs[doc_id] = converted_doc

        if len(all_docs.keys()) is 0:
            return "no objects"

        return all_docs
Exemple #4
0
 def view(self, curr_price=None):
     """
     Print position with/without unrealized pnl.
     :param curr_price: double; current price.
         <Default>: None; do not print unrealized pnl.
     :return:
     """
     if (curr_price is not None) and (self.status == PositionStatus.open):
         d = {'unrealized_pnl': self.unrealized_pnl(curr_price)}
         print json.dumps(serialize_merge_dict(self.body, d),
                          indent=4, sort_keys=True)
     else:
         print json.dumps(serialize_dict(self.body),
                          indent=4, sort_keys=True)
 def view(self, curr_price=None):
     """
     Print position with/without unrealized pnl.
     :param curr_price: double; current price.
         <Default>: None; do not print unrealized pnl.
     :return:
     """
     if (curr_price is not None) and (self.status == PositionStatus.open):
         d = {'unrealized_pnl': self.unrealized_pnl(curr_price)}
         print json.dumps(serialize_merge_dict(self.body, d),
                          indent=4,
                          sort_keys=True)
     else:
         print json.dumps(serialize_dict(self.body),
                          indent=4,
                          sort_keys=True)
 def view(self, curr_prices=None):
     """
     View account.
     :param curr_prices: dict; {instrument: current price} pairs.
     :return:
     """
     prompt = {
         'base': self.__base,
         'leverage': self.__leverage,
         'marginRate': self.__margin_rate,
         'balance': self.curr_balance,
         'longPositions': [p.body for p in self.longs],
         'shortPositions': [p.body for p in self.shorts],
         'closedPositions': [p.body for p in self.closed]
     }
     if curr_prices:
         additional = {
             'nav': self.nav(curr_prices),
             'marginUsed': self.margin_used(curr_prices),
             'marginAvailable': self.margin_available(curr_prices)
         }
         prompt.update(additional)
     print json.dumps(serialize_dict(prompt), indent=4, sort_keys=True)
Exemple #7
0
 def view(self, curr_prices=None):
     """
     View account.
     :param curr_prices: dict; {instrument: current price} pairs.
     :return:
     """
     prompt = {
         'base': self.__base,
         'leverage': self.__leverage,
         'marginRate': self.__margin_rate,
         'balance': self.curr_balance,
         'longPositions': [p.body for p in self.longs],
         'shortPositions': [p.body for p in self.shorts],
         'closedPositions': [p.body for p in self.closed]
     }
     if curr_prices:
         additional = {
             'nav': self.nav(curr_prices),
             'marginUsed': self.margin_used(curr_prices),
             'marginAvailable': self.margin_available(curr_prices)
         }
         prompt.update(additional)
     print json.dumps(serialize_dict(prompt), indent=4, sort_keys=True)
 def view(self):
     """
     Print order.
     :return:
     """
     print json.dumps(serialize_dict(self.body), indent=4, sort_keys=True)
Exemple #9
0
 def dump_equivalent_state(self):
     filtered_dict = {}
     filtered_dict["enabled_actions"] = utils.copy_state(
         self.enabled_actions)
     filtered_dict["ports"] = utils.serialize_dict(self.ports)
     return filtered_dict
Exemple #10
0
 def view(self):
     """
     Print order.
     :return:
     """
     print json.dumps(serialize_dict(self.body), indent=4, sort_keys=True)
Exemple #11
0
 def dump_equivalent_state(self):
     filtered_dict = {}
     filtered_dict["enabled_actions"] = utils.copy_state(self.enabled_actions)
     filtered_dict["ports"] = utils.serialize_dict(self.ports)
     return filtered_dict