def get_current_whales(
        self,
        num_whales=30
    ):  #by default, fetch only 30 whales off the book either way
        whales = []

        for index, bid_whale in self._bid_whales.items(True):
            if index == num_whales:
                break
            else:
                whales.append({
                    'price': Decimal128(bid_whale.get_price()),
                    'volume': Decimal128(bid_whale.get_volume()),
                    'id': bid_whale.get_id(),
                })

        for index, ask_whale in self._ask_whales.items():
            if index == num_whales:
                break
            else:
                whales.append({
                    'price': Decimal128(ask_whale.get_price()),
                    'volume': Decimal128(ask_whale.get_volume()),
                    'id': ask_whale.get_id(),
                })

        whales_frame = pd.DataFrame(whales)
        return whales_frame
Exemple #2
0
def positive_sample():
    return {
        FIELD_RESULT: POSITIVE_RESULT_VALUE,
        FIELD_ROOT_SAMPLE_ID: "MCM001",
        FIELD_CH1_CQ: Decimal128("5.12345678"),
        FIELD_CH2_CQ: Decimal128("6.12345678"),
        FIELD_CH3_CQ: Decimal128("7.12345678"),
    }
Exemple #3
0
def test_v2_is_positive_returns_false_all_ct_values_greater_than_30():
    identifier = FilteredPositiveIdentifierV2()

    sample = positive_sample()
    sample[FIELD_CH1_CQ] = Decimal128("40.12345678")
    sample[FIELD_CH2_CQ] = Decimal128("41.12345678")
    sample[FIELD_CH3_CQ] = Decimal128("42.12345678")
    assert identifier.is_positive(sample) is False
def main():
    mongo_client = MongoClient(MDB_CONNECTION)
    db = mongo_client[MDB_DATABASE]
    my_collection = db[MDB_COLLECTION]

    print('Begin generating txns documents.')
    print('Number of documents to generate: ' + str(NUM_DOCS))

    for index in range(int(NUM_DOCS)):
        fake_timestamp = fake.date_between(start_date='-1y', end_date='today')
        txn_types = ['deposit', 'withdrawal']
        txns = random.choice(txn_types)

        my_txn_document = {
            "customerId":
            fake.ssn(),
            "name":
            fake.name(),
            "address":
            fake.street_address(),
            "city":
            fake.city(),
            "state":
            fake.state(),
            "postalCode":
            fake.postcode(),
            "email":
            fake.email(),
            "lastLocation": {
                "type":
                "Point",
                "coordinates":
                [Decimal128(fake.longitude()),
                 Decimal128(fake.latitude())]
            },
            "txnType":
            txns,
            "txnAmount":
            random.randint(0, 10000),
            "txnDate":
            datetime.datetime(fake_timestamp.year, fake_timestamp.month,
                              fake_timestamp.day)
        }

        # Print example doc on first doc creation
        if index == 1:
            print('Example Document')
            print(my_txn_document)

        # Indicate how many docs inserted
        if index % 100 == 0:
            print('Docs inserted: ' + str(index))

        my_collection.insert_one(my_txn_document)
Exemple #5
0
async def set_escrow_sum(message: types.Message, offer: EscrowOffer):
    """Set sum and ask for fee payment agreement."""
    try:
        offer_sum = money(message.text)
    except MoneyValueError as exception:
        await tg.send_message(message.chat.id, str(exception))
        return

    order = await database.orders.find_one({"_id": offer.order})
    order_sum = order.get(offer.sum_currency)
    if order_sum and offer_sum > order_sum.to_decimal():
        await tg.send_message(message.chat.id, i18n("exceeded_order_sum"))
        return

    update_dict = {offer.sum_currency: Decimal128(offer_sum)}
    new_currency = "sell" if offer.sum_currency == "sum_buy" else "buy"
    update_dict[f"sum_{new_currency}"] = Decimal128(
        normalize(offer_sum * order[f"price_{new_currency}"].to_decimal()))
    escrow_sum = update_dict[f"sum_{offer.type}"]
    escrow_fee = Decimal(config.ESCROW_FEE_PERCENTS) / Decimal("100")
    update_dict["sum_fee_up"] = Decimal128(
        normalize(escrow_sum.to_decimal() * (Decimal("1") + escrow_fee)))
    update_dict["sum_fee_down"] = Decimal128(
        normalize(escrow_sum.to_decimal() * (Decimal("1") - escrow_fee)))
    offer = replace(offer, **update_dict)  # type: ignore

    if offer.sum_currency == offer.type:
        insured = await get_insurance(offer)
        update_dict["insured"] = Decimal128(insured)
        if offer_sum > insured:
            keyboard = InlineKeyboardMarkup()
            keyboard.add(
                InlineKeyboardButton(
                    i18n("continue"),
                    callback_data=f"accept_insurance {offer._id}"),
                InlineKeyboardButton(i18n("cancel"),
                                     callback_data=f"init_cancel {offer._id}"),
            )
            answer = i18n("exceeded_insurance {amount} {currency}").format(
                amount=insured, currency=offer.escrow)
            answer += "\n" + i18n("exceeded_insurance_options")
            await tg.send_message(message.chat.id,
                                  answer,
                                  reply_markup=keyboard)
    else:
        await ask_fee(message.from_user.id, message.chat.id, offer)

    await offer.update_document({
        "$set": update_dict,
        "$unset": {
            "sum_currency": True
        }
    })
Exemple #6
0
def main():
    print('NUM DOCS TO GENERATE: ' + str(NUM_DOCS))

    mongo_client = MongoClient(MDB_CONNECTION)
    db = mongo_client[MDB_DATABASE]
    my_collection = db[MDB_COLLECTION]

    for index in range(int(NUM_DOCS)):
        # Define Subscription Doc
        my_document = {
            "ApplicationID": fake.uuid4(),
            "UserID": fake.uuid4(),
            "Type": fake.random_int(1, 10),
            "Address": "MyDeviceAddr",
            "Units": fake.random_int(1, 10),
            "Culture": {
                "LanguageCode": fake.language_code(),
                "CountryCode": fake.country_code()
            },
            "Target": {
                "IsFollowMe": True,
                "LocationInfo": {
                    "Point": {
                        "type": "Point",
                        "coordinates": [
                            Decimal128(fake.longitude()),
                            Decimal128(fake.latitude())
                        ]
                    },
                    "PostalKey": fake.postcode(),
                    "CountryCode": fake.country_code(),
                    "TimeZone": fake.timezone(),
                    "DmaID": "524",
                    "CityID": fake.uuid4(),
                    "CountyID": "GAC057",
                    "PollenID": "ATL",
                    "TideID": "",
                    "ZoneID": "GAZ021",
                    "AdminDistrict": "GA:US"
                },
                "SavedLocationType": fake.random_int(1, 10)
            },
            "Schedule": {
                "IsActive": False,
                "Days": None,
                "Hours": None,
                "TimeZone": ""

            },
            "CreatedOn": datetime.datetime.now()
        }

        my_collection.insert_one(my_document)
Exemple #7
0
def test_update_inc_decimal128(monty_update, mongo_update):
    docs = [
        {"a": Decimal128("1.5")}
    ]
    spec = {"$inc": {"a": 1}}

    monty_c = monty_update(docs, spec)
    mongo_c = mongo_update(docs, spec)

    assert next(mongo_c) == next(monty_c)
    monty_c.rewind()
    assert next(monty_c) == {"a": Decimal128("2.5")}
Exemple #8
0
def test_update_mul_decimal128(monty_update, mongo_update):
    docs = [
        {"a": Decimal128("1.5")}
    ]
    spec = {"$mul": {"a": 2}}

    monty_c = monty_update(docs, spec)
    mongo_c = mongo_update(docs, spec)

    assert next(mongo_c) == next(monty_c)
    monty_c.rewind()
    assert next(monty_c) == {"a": Decimal128("3.0")}
 def get_last_operation(self, email):
     finded = self.operations_collection.find_one({"email": email},
                                                  sort=[("date",
                                                         pymongo.DESCENDING)
                                                        ])
     if (finded != None):
         return Operation(finded['email'], finded['value'], finded['date'],
                          finded['type'], finded['balance'])
     else:
         return Operation(email, Decimal128('0'),
                          datetime.timestamp(datetime.now()),
                          'Initial  balance', Decimal128('0'))
Exemple #10
0
 def decimal128(s):
     """
     浮点数或者字符串转化为decimal128
     :param s:
     :return:
     """
     if isinstance(s, Decimal128):
         money = s
     else:
         try:
             money = Decimal128(s)
         except (ValueError, InvalidOperation):
             money = Decimal128("0")
     return Decimal128(money.to_decimal().quantize(Decimal("0.00")))
    def withdraw(self, email, value):
        last_op = self.get_last_operation(email)
        new_balance = Decimal128(last_op.balance.to_decimal() -
                                 Decimal128(value).to_decimal())
        wit = {
            'email': email,
            'value': Decimal128(value),
            'date': datetime.timestamp(datetime.now()),
            'type': 'Withdraw',
            'balance': new_balance
        }

        insert = self.insert_operation(wit)
        return 'withdraw successful'
Exemple #12
0
def main():

    mongo_client = MongoClient(MDB_CONNECTION)
    db = mongo_client[MDB_DATABASE]
    my_collection = db[MDB_COLLECTION]

    print('Delete existing documents.')
    result = my_collection.delete_many({})
    print('Num docs deleted: ' + str(result.deleted_count))

    print('Begin generating IOT documents.')
    print('Number of documents to generate: ' + str(NUM_DOCS))

    for index in range(int(NUM_DOCS)):
        # create timestamp
        fake_timestamp = fake.date_this_year()

        # Define IoT Document
        my_iot_document = {
            "username":
            fake.user_name(),
            "remote_ipv4":
            fake.ipv4(),
            "httpMethod":
            fake.http_method(),
            "hostName":
            fake.hostname(),
            "portNum":
            fake.port_number(),
            "location": {
                "type":
                "Point",
                "coordinates":
                [Decimal128(fake.longitude()),
                 Decimal128(fake.latitude())]
            },
            "dateAccessed":
            datetime.datetime(fake_timestamp.year, fake_timestamp.month,
                              fake_timestamp.day)
        }

        if index == 1:
            print('Example Document')
            print(my_iot_document)

        # Indicate how many docs inserted every 100 iterations
        if index % 100 == 0:
            print('Docs inserted: ' + str(index))

        my_collection.insert_one(my_iot_document)
Exemple #13
0
    def insert_record(self,
                      name,
                      nric,
                      age,
                      income,
                      cpf,
                      addr,
                      lease_id,
                      img_str,
                      detected_income=None,
                      img_highlight=None,
                      verified=False,
                      renewable=False,
                      verify_fail=None,
                      renew_fail=None):
        record = {
            'person': {
                'nric': nric,
                'first_name': name,
                'age': age,
                'income': {
                    'declared': Decimal128(income),
                    'cpf': Decimal128(cpf)
                }
            },
            'property': {
                'address': addr,
                'lease_id': lease_id
            },
            'date': datetime.datetime.utcnow(),
            'original': img_str,
            'verified': verified,
            'renewable': renewable
        }

        if img_highlight is not None:
            record['annotated'] = img_highlight

        if detected_income is not None:
            record['detected_income'] = Decimal128(detected_income)

        if verified == False and verify_fail is not None:
            record['verify_fail'] = verify_fail

        if renewable == False and renew_fail is not None:
            record['renew_fail'] = renew_fail

        self.payslip_col.insert_one(record)
Exemple #14
0
    def get_real_time_price(symbol: Union[str, List[str]] = None):
        if symbol is None:
            return []
        if isinstance(symbol, str):
            symbol = [symbol, ]

        res = requests.get(GetStockData.URL,
                           params={
                               'function': 'BATCH_STOCK_QUOTES',
                               'symbols': ','.join(symbol),
                               'apikey': Utils.get_env('ALPHAVANTAG_API_KEY')
                           })
        j = res.json()  #type: dict
        ret_list = []
        if 'Stock Quotes' in j:
            tz = j['Meta Data']['3. Time Zone']

            for info in j['Stock Quotes']:
                tmp_dict = {'timestamp': arrow.get(info['4. timestamp']).replace(tzinfo=tz).datetime,
                            'symbol': info['1. symbol'],
                            'price': Decimal128(info['2. price'])}
                try:
                    tmp_dict['volume'] = Int64(info['3. volume'])
                except (BSONError, ValueError):
                    tmp_dict['volume'] = Int64(0)
                ret_list.append(tmp_dict)
        else:
            print(arrow.utcnow().isoformat())
            print(json.dumps(j, indent=1))
        return ret_list
Exemple #15
0
 def info(self):
     return {
         "firstname": self.firstname,
         "lastname": self.lastname,
         "username": self.username,
         "email": self.email,
         "password": self.password,
         "fitness": self.fitness,
         "weight": Decimal128(self.weight),
         "goalweight": Decimal128(self.goalweight),
         "goal": self.goal,
         "history": [],
         #"email_confirmation_sent_on" : self.email_confirmation_sent_on,
         "email_confirmed": self.email_confirmed,
         "email_confirmed_on": self.email_confirmed_on
     }
Exemple #16
0
def test_sort_18(monty_sort, mongo_sort):
    docs = [
        {"a": ["x", True]},
        {"a": None},
        {"a": []},
        {"a": [5, []]},
        {"a": {"s": 7}},
        {"a": {"s": [9]}},
        {"a": {"s": 10}},
        {"a": 6},
        {"a": 4},
        {"a": [5, None]},
        {"a": [5, [1]]},
        {"a": [Decimal128("4.5"), Binary(b"0")]},
        {"a": [{"s": 5}, False]},
        {"a": [{"s": 9}]},
        {"a": [True, "y"]},
        {"a": []},
    ]
    sort = [("a", -1)]

    monty_c = monty_sort(docs, sort)
    mongo_c = mongo_sort(docs, sort)

    for i in range(len(docs)):
        assert next(mongo_c)["_id"] == next(monty_c)["_id"]
Exemple #17
0
def add_product():
    """
    A view to return the add product page, add the product to the database and
    update the relevant category's brands list in the database.
    """
    if request.method == "POST":
        """
        Gets the details entered into the form and convert them into a
        dictionary, converts the price to the decimal 128 data type and adds
        the default rating values to the dictionary. Decimal128 method is from
        https://pymongo.readthedocs.io/en/stable/api/bson/decimal128.html
        """
        product = request.form.to_dict()
        product['price'] = Decimal128(product['price'])
        product['overall_rating'] = 0
        product['performance_rating'] = 0
        product['usability_rating'] = 0
        product['price_rating'] = 0
        product['quality_rating'] = 0
        product['five_stars'] = 0
        product['four_stars'] = 0
        product['three_stars'] = 0
        product['two-stars'] = 0
        product['one_star'] = 0
        """
        Deletes any products from the dictionary which have a value of an empty
        string. Code is from https://stackoverflow.com/questions/6307394/
        removing-dictionary-entries-with-no-values-python
        """
        keys = list(product.keys())

        for key in keys:
            if product[key] == "":
                del product[key]
        """
        Adds the product details to the database and Adds the brand to the
        relevant brands list in the database. Add to set method is from
        https://docs.mongodb.com/manual/reference/operator/update/addToSet/
        """
        mongo.db.products.insert_one(product)

        mongo.db.categories.update_one(
            {"name": product['category']},
            {"$addToSet": {
                "brands": product['brand']
            }})
        """
        Code for message categories is from https://flask.palletsprojects.com/
        en/1.1.x/patterns/flashing/
        """
        flash("Product Successfully Added", "success")

        return redirect(url_for('product_management'))

    # Get a list of categories from the database
    categories = mongo.db.categories.find()

    return render_template('add_product.html',
                           page_title='Add Product',
                           categories=categories)
Exemple #18
0
    def test_special(self):
        dnan = Decimal('NaN')
        dnnan = Decimal('-NaN')
        dsnan = Decimal('sNaN')
        dnsnan = Decimal('-sNaN')
        dnan128 = Decimal128(dnan)
        dnnan128 = Decimal128(dnnan)
        dsnan128 = Decimal128(dsnan)
        dnsnan128 = Decimal128(dnsnan)

        # Due to the comparison rules for decimal.Decimal we have to
        # compare strings.
        self.assertEqual(str(dnan), str(dnan128.to_decimal()))
        self.assertEqual(str(dnnan), str(dnnan128.to_decimal()))
        self.assertEqual(str(dsnan), str(dsnan128.to_decimal()))
        self.assertEqual(str(dnsnan), str(dnsnan128.to_decimal()))
def object_hook(dct, json_options=DEFAULT_JSON_OPTIONS):
    if "$oid" in dct:
        return ObjectId(str(dct["$oid"]))
    if "$ref" in dct:
        return DBRef(dct["$ref"], dct["$id"], dct.get("$db", None))
    if "$date" in dct:
        return _get_date(dct, json_options)
    if "$regex" in dct:
        flags = 0
        # PyMongo always adds $options but some other tools may not.
        for opt in dct.get("$options", ""):
            flags |= _RE_OPT_TABLE.get(opt, 0)
        return Regex(dct["$regex"], flags)
    if "$minKey" in dct:
        return MinKey()
    if "$maxKey" in dct:
        return MaxKey()
    if "$binary" in dct:
        return _get_binary(dct, json_options)
    if "$code" in dct:
        return Code(dct["$code"], dct.get("$scope"))
    if "$uuid" in dct:
        return uuid.UUID(dct["$uuid"])
    if "$undefined" in dct:
        return None
    if "$numberLong" in dct:
        return Int64(dct["$numberLong"])
    if "$timestamp" in dct:
        tsp = dct["$timestamp"]
        return Timestamp(tsp["t"], tsp["i"])
    if "$numberDecimal" in dct:
        return Decimal128(dct["$numberDecimal"])
    return dct
Exemple #20
0
def cart():
    orders = []
    if 'order_products' not in session:
        session['order_products'] = []
    else:
        for item in session['order_products']:
            orders.append({
                "product": list(get_product(item["id"]))[0],
                "number": item["number"]
            })

    D128_CTX = create_decimal128_context()
    with decimal.localcontext(D128_CTX):
        total_price = sum([
            order["product"]['price'].to_decimal() *
            Decimal128(order['number']).to_decimal() for order in orders
        ])

    if "order_products" not in session:
        session["order_products"] = []
    num = len(session["order_products"])

    return render_template('blog/cart.html',
                           orders=orders,
                           total_price=total_price,
                           badge_num=num)
Exemple #21
0
def add_game():
    if "appid" in request.form and "name" in request.form:
        new_game = {
            "appid": int(request.form["appid"]),
            "name": request.form["name"],
            "release_date": datetime(2010, 12, 3),
            "english": "1",
            "developer": [],
            "publisher": [],
            "platforms": [],
            "required_age": 0,
            "categories": [],
            "genres": [],
            "steamspy_tags": [],
            "achievements": 0,
            "positive_ratings": 1111,
            "negative_ratings": 1111,
            "average_playtime": 1111,
            "median_playtime": 1111,
            "owners": "test",
            "price": Decimal128('7.19'),
            "comments": []
        }
        # Get object id, if needed, not necessary since appid unique indexing
        new_game_id = games.insert_one(new_game)
        new_game_link = "http://localhost:5000/api/v1.0/games/" + \
            request.form["appid"]
        return make_response(jsonify({"url": new_game_link}), 201)
    else:
        return make_response(jsonify({"error": "Missing form data"}), 404)
def _parse_canonical_decimal128(doc: Any) -> Decimal128:
    """Decode a JSON decimal128 to bson.decimal128.Decimal128."""
    d_str = doc["$numberDecimal"]
    if len(doc) != 1:
        raise TypeError("Bad $numberDecimal, extra field(s): %s" % (doc, ))
    if not isinstance(d_str, str):
        raise TypeError("$numberDecimal must be string: %s" % (doc, ))
    return Decimal128(d_str)
Exemple #23
0
def convert_value(v):
    if v is None:
        return ('n', False)

    if type(v) is not str:
        return ('s', False)

    return Decimal128(Decimal(v)), True
def test_validate_bson_decimal_valid_bson_decimal():
    class MyModel(Model):
        field: Decimal128

    value = Decimal128("3.152345596")
    instance = MyModel(field=value)
    assert isinstance(instance.field, Decimal128)
    assert instance.field == value
Exemple #25
0
class EncodeDecimalsTestCase:
    @pytest.mark.parametrize(
        "data, expected",
        [
            pytest.param(
                {"money_amount": Decimal("13.37")},
                {"money_amount": Decimal128("13.37")},
                id="simple_dict",
            ),
            pytest.param(
                [{
                    "money_amount": Decimal("13.37")
                }],
                [{
                    "money_amount": Decimal128("13.37")
                }],
                id="simple_list",
            ),
            pytest.param(
                {"author": {
                    "money_amount": Decimal("13.37")
                }},
                {"author": {
                    "money_amount": Decimal128("13.37")
                }},
                id="nested",
            ),
            pytest.param(
                {
                    "title": "test",
                    "contributors": [{
                        "money_amount": Decimal("13.37")
                    }],
                },
                {
                    "title": "test",
                    "contributors": [{
                        "money_amount": Decimal128("13.37")
                    }],
                },
                id="list_in_nested",
            ),
        ],
    )
    async def test__convert_decimals(self, data, expected):
        assert mongodb_encoders._convert_decimals(data) == expected
Exemple #26
0
 def to_mongo(self, value):
     if isinstance(value, Decimal128):
         return value
     try:
         return Decimal128(decimal.Decimal(value))
     except decimal.DecimalException as exc:
         raise ValidationError('Cannot convert value %r to Decimal128: %s' %
                               (value, exc.__class__.__name__))
Exemple #27
0
def _parse_canonical_decimal128(doc):
    """Decode a JSON decimal128 to bson.decimal128.Decimal128."""
    d_str = doc['$numberDecimal']
    if len(doc) != 1:
        raise TypeError('Bad $numberDecimal, extra field(s): %s' % (doc, ))
    if not isinstance(d_str, string_type):
        raise TypeError('$numberDecimal must be string: %s' % (doc, ))
    return Decimal128(d_str)
Exemple #28
0
 def add(self):
     user = {
         "firstname": self.firstname,
         "lastname": self.lastname,
         "username": self.username,
         "email": self.email,
         "password": self.password,
         "fitness": self.fitness,
         "weight": Decimal128(self.weight),
         "goalweight": Decimal128(self.goalweight),
         "goal": self.goal,
         "history": [],
         #"email_confirmation_sent_on" : self.email_confirmation_sent_on,
         "email_confirmed": self.email_confirmed,
         "email_confirmed_on": self.email_confirmed_on
     }
     DB.insert("users", user)
Exemple #29
0
 def numeric_type(wgt, val):
     if isinstance(value, (Decimal128, _cmp_decimal)):
         if isinstance(val, _cmp_decimal):
             val = val._dec
         if val in _decimal128_NaN_ls:
             val = Decimal128('NaN')  # MongoDB does not sort them
         return (wgt, _cmp_decimal(val))
     else:
         return (wgt, val)
Exemple #30
0
def test_sort_5(monty_sort, mongo_sort):
    docs = [{"a": 0}, {"a": 1.1}, {"a": Int64(2)}, {"a": Decimal128("3.3")}]
    sort = [("a", -1)]

    monty_c = monty_sort(docs, sort)
    mongo_c = mongo_sort(docs, sort)

    for i in range(len(docs)):
        assert next(mongo_c)["_id"] == next(monty_c)["_id"]
    def test_round_trip(self):
        if not client_context.version.at_least(3, 3, 6):
            raise unittest.SkipTest(
                'Round trip test requires MongoDB >= 3.3.6')

        coll = client_context.client.pymongo_test.test
        coll.drop()

        dec128 = Decimal128.from_bid(
            b'\x00@cR\xbf\xc6\x01\x00\x00\x00\x00\x00\x00\x00\x1c0')
        coll.insert_one({'dec128': dec128})
        doc = coll.find_one({'dec128': dec128})
        self.assertIsNotNone(doc)
        self.assertEqual(doc['dec128'], dec128)
 def test_pickle(self):
     dec128 = Decimal128.from_bid(
         b'\x00@cR\xbf\xc6\x01\x00\x00\x00\x00\x00\x00\x00\x1c0')
     for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
         pkl = pickle.dumps(dec128, protocol=protocol)
         self.assertEqual(dec128, pickle.loads(pkl))
Exemple #33
0
def _get_decimal128(data, position, dummy0, dummy1, dummy2):
    """Decode a BSON decimal128 to bson.decimal128.Decimal128."""
    end = position + 16
    return Decimal128.from_bid(data[position:end]), end