1
0

Fixed parsing corner case
All checks were successful
Build container / Package-Python (push) Successful in 24s
Build container / Package-Container (push) Successful in 1m21s

This commit is contained in:
Jon Michael Aanes 2024-05-16 00:18:29 +02:00
parent 4c94cffe1b
commit 0f3ccb7b7d
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
2 changed files with 42 additions and 4 deletions

View File

@ -27,7 +27,6 @@ import personal_data.fetchers.crunchyroll
import personal_data.fetchers.ffxiv_lodestone
import personal_data.fetchers.partisia_blockchain
import personal_data.fetchers.psnprofiles
from personal_data import mailgun
from . import mailgun
@ -41,7 +40,7 @@ logger.setLevel('INFO')
def try_value(fn, s: str) -> object:
try:
return fn(s)
except ValueError:
except ValueError as e:
return None
@ -49,7 +48,9 @@ def to_value(s: str) -> object:
s = s.strip()
if len(s) == 0:
return None
if v := try_value(int, s):
if (v := try_value(int, s)) is not None:
return v
if (v := try_value(float, s)) is not None:
return v
if v := try_value(datetime.date.fromisoformat, s):
return v
@ -76,10 +77,13 @@ def extend_csv_file(
reader = csv.DictReader(csvfile, dialect=CSV_DIALECT)
for row in reader:
for k in list(row.keys()):
row[k] = to_value(row[k])
orig = row[k]
row[k] = to_value(orig)
if row[k] is None:
del row[k]
del k, orig
dicts.append(frozendict(row))
del row
del csvfile
except FileNotFoundError as e:
logger.info('Creating file: %s', filename)

34
test/test_parsing.py Normal file
View File

@ -0,0 +1,34 @@
import datetime
import pytest
from personal_data.main import to_value
PARSE_MAPPINGS = [
(
'2024-04-28 21:35:40+00:00',
datetime.datetime(2024, 4, 28, 21, 35, 40, tzinfo=datetime.timezone.utc),
),
(
'0003791e9f5f3691b8bbbe0d12a7ae9c3f2e89db38',
'0003791e9f5f3691b8bbbe0d12a7ae9c3f2e89db38',
),
('', None),
('0', 0),
('200', 200),
('52.5474', 52.5474),
('true', True),
('True', True),
('TRUE', True),
('false', False),
('False', False),
('FALSE', False),
('none', None),
('None', None),
('NONE', None),
]
@pytest.mark.parametrize('text,parsed', PARSE_MAPPINGS)
def test_to_value(text, parsed):
assert to_value(text) == parsed, text