From 43db3fc5d1d287236209de88031ab4373f655c6e Mon Sep 17 00:00:00 2001 From: unexcellent <> Date: Thu, 14 Nov 2024 05:27:30 +0100 Subject: [PATCH] feat: implement Num.to_json() --- raillabel/format/num.py | 10 ++++++++++ tests/test_raillabel/format/test_num.py | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/raillabel/format/num.py b/raillabel/format/num.py index 21e7a23..acd2d62 100644 --- a/raillabel/format/num.py +++ b/raillabel/format/num.py @@ -4,6 +4,7 @@ from __future__ import annotations from dataclasses import dataclass +from uuid import UUID from raillabel.json_format import JSONNum @@ -29,3 +30,12 @@ def from_json(cls, json: JSONNum) -> Num: val=json.val, sensor_id=json.coordinate_system, ) + + def to_json(self, uid: UUID) -> JSONNum: + """Export this object into the RailLabel JSON format.""" + return JSONNum( + name=self.name, + val=self.val, + coordinate_system=self.sensor_id, + uid=uid, + ) diff --git a/tests/test_raillabel/format/test_num.py b/tests/test_raillabel/format/test_num.py index 2e0fae7..092c700 100644 --- a/tests/test_raillabel/format/test_num.py +++ b/tests/test_raillabel/format/test_num.py @@ -14,15 +14,20 @@ @pytest.fixture -def num_json() -> JSONNum: +def num_json(num_id) -> JSONNum: return JSONNum( - uid="78f0ad89-2750-4a30-9d66-44c9da73a714", + uid=num_id, name="velocity", val=49.21321, coordinate_system="gps_imu", ) +@pytest.fixture +def num_id() -> UUID: + return UUID("78f0ad89-2750-4a30-9d66-44c9da73a714") + + @pytest.fixture def num() -> Num: return Num( @@ -40,5 +45,10 @@ def test_from_json(num, num_json): assert actual == num +def test_to_json(num, num_json, num_id): + actual = num.to_json(num_id) + assert actual == num_json + + if __name__ == "__main__": pytest.main([__file__, "-v"])