Skip to content

Commit ca80b95

Browse files
committed
Fix serialization of latitude, longitude to TOML
1 parent cee2277 commit ca80b95

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
- Changed type of ``Series`` fields ``alternative_names``,
99
``country_codes`` from ``set[str]`` to ``list[str]``.
1010

11+
- Fixed serialization of party location's latitude and longitude.
12+
1113
- Added testing infrastructure (pytest, GitHub Action).
1214

1315
- Added tests for party, series reading.

src/lanpartydb/writing.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
def serialize_party(party: Party) -> str:
2222
"""Serialize party to TOML document."""
2323
party_dict = _party_to_sparse_dict(party)
24+
25+
location = party_dict.get('location', None)
26+
if location is not None:
27+
_convert_decimal_to_float(location, 'latitude')
28+
_convert_decimal_to_float(location, 'longitude')
29+
2430
return _write_toml(party_dict)
2531

2632

@@ -30,6 +36,12 @@ def _party_to_sparse_dict(party: Party) -> dict[str, Any]:
3036
return data
3137

3238

39+
def _convert_decimal_to_float(d: dict[str, Any], key: str) -> None:
40+
value = d.get(key)
41+
if value is not None:
42+
d[key] = float(value)
43+
44+
3345
# util
3446

3547

tests/writing/test_serialize_party.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
from datetime import date
7+
from decimal import Decimal
78
from textwrap import dedent
89

910
import pytest
@@ -46,6 +47,8 @@
4647
city='Los Angeles',
4748
zip_code='90099',
4849
street='123 North Hill Street',
50+
latitude=Decimal('34.06101057935884'),
51+
longitude=Decimal('-118.23974355902666'),
4952
),
5053
links=Links(
5154
website=Resource(
@@ -70,6 +73,8 @@
7073
city = "Los Angeles"
7174
zip_code = "90099"
7275
street = "123 North Hill Street"
76+
latitude = 34.06101057935884
77+
longitude = -118.23974355902666
7378
7479
[links.website]
7580
url = "https://www.superlan.example/"

0 commit comments

Comments
 (0)