diff --git a/personal_data/main.py b/personal_data/main.py index 7d0326b..3cc0ee4 100644 --- a/personal_data/main.py +++ b/personal_data/main.py @@ -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) diff --git a/test/test_parsing.py b/test/test_parsing.py new file mode 100644 index 0000000..ba4c932 --- /dev/null +++ b/test/test_parsing.py @@ -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