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,
}
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']):
coin_data = coins[coin_idx]
byoc_balance = Decimal(amount_data['balance'])
denominator = Decimal(coin_data['conversionRate']['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
yield data_point

View File

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

View File

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