Skip to content

Commit

Permalink
feat: implement Num.to_json()
Browse files Browse the repository at this point in the history
  • Loading branch information
unexcellent committed Nov 14, 2024
1 parent 6db3bed commit 43db3fc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
10 changes: 10 additions & 0 deletions raillabel/format/num.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import annotations

from dataclasses import dataclass
from uuid import UUID

from raillabel.json_format import JSONNum

Expand All @@ -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,
)
14 changes: 12 additions & 2 deletions tests/test_raillabel/format/test_num.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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"])

0 comments on commit 43db3fc

Please sign in to comment.