1
0

Prefer decimal
All checks were successful
Build container / Package-Python (push) Successful in 24s
Build container / Package-Container (push) Successful in 1m22s

This commit is contained in:
Jon Michael Aanes 2024-05-16 00:29:06 +02:00
parent 0f3ccb7b7d
commit ee6aa7e527
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
3 changed files with 10 additions and 9 deletions

View File

@ -83,14 +83,14 @@ class MpcBalance(Scraper):
'account.update_time': date, 'account.update_time': date,
} }
data_point['balance.MPC'] = str(Decimal(account_data['mpcTokens']) / 1000) data_point['balance.MPC'] = Decimal(account_data['mpcTokens']) / 1000
for coin_idx, amount_data in enumerate(account_data['accountCoins']): for coin_idx, amount_data in enumerate(account_data['accountCoins']):
coin_data = coins[coin_idx] coin_data = coins[coin_idx]
byoc_balance = Decimal(amount_data['balance']) byoc_balance = Decimal(amount_data['balance'])
denominator = Decimal(coin_data['conversionRate']['denominator']) denominator = Decimal(coin_data['conversionRate']['denominator'])
native_balance = byoc_balance / denominator native_balance = byoc_balance / denominator
data_point['balance.' + coin_data['symbol']] = str(native_balance) data_point['balance.' + coin_data['symbol']] = native_balance
del coin_idx, coin_data del coin_idx, coin_data
yield data_point yield data_point

View File

@ -2,6 +2,8 @@ import csv
import datetime import datetime
import io import io
import logging import logging
import decimal
from decimal import Decimal
import requests import requests
import requests_cache import requests_cache
@ -40,7 +42,7 @@ logger.setLevel('INFO')
def try_value(fn, s: str) -> object: def try_value(fn, s: str) -> object:
try: try:
return fn(s) return fn(s)
except ValueError as e: except (ValueError, decimal.InvalidOperation):
return None return None
@ -48,9 +50,7 @@ def to_value(s: str) -> object:
s = s.strip() s = s.strip()
if len(s) == 0: if len(s) == 0:
return None return None
if (v := try_value(int, s)) is not None: if (v := try_value(Decimal, s)) is not None:
return v
if (v := try_value(float, s)) is not None:
return v return v
if v := try_value(datetime.date.fromisoformat, s): if v := try_value(datetime.date.fromisoformat, s):
return v return v

View File

@ -1,5 +1,6 @@
import datetime import datetime
from decimal import Decimal
import pytest import pytest
from personal_data.main import to_value from personal_data.main import to_value
@ -14,9 +15,9 @@ PARSE_MAPPINGS = [
'0003791e9f5f3691b8bbbe0d12a7ae9c3f2e89db38', '0003791e9f5f3691b8bbbe0d12a7ae9c3f2e89db38',
), ),
('', None), ('', None),
('0', 0), ('0', Decimal(0)),
('200', 200), ('200', Decimal(200)),
('52.5474', 52.5474), ('52.5474', Decimal('52.5474')),
('true', True), ('true', True),
('True', True), ('True', True),
('TRUE', True), ('TRUE', True),