Coverage for backpack/config/geometry.py: 0%

14 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-30 23:12 +0000

1''' This module contains config serdes for objects from backpack.geometry module. ''' 

2 

3from typing import Optional, Mapping, Any 

4import dataclasses 

5import json 

6 

7from backpack.geometry import PolyLine 

8 

9from .serde import ConfigSerDeBase 

10 

11class PolyLineSerDe(ConfigSerDeBase): 

12 ''' De/serializes a json string containing a polygon definition. 

13 

14 Example string: ``[[0.1, 0.1], [0.9, 0.1], [0.9, 0.9], [0.5, 0.5], [0.1, 0.9]]`` 

15 ''' 

16 description : str = 'JSON encoded Polygon' 

17 example: str = '[[0.1, 0.1], [0.9, 0.1], [0.9, 0.9], [0.5, 0.5], [0.1, 0.9]]' 

18 

19 @staticmethod 

20 def serialize(value: PolyLine, metadata: Mapping[str, Any]={}) -> str: 

21 ''' Serializes a PolyLine into a string. 

22 

23 Args: 

24 value (PolyLine): The PolyLine to be serialized. 

25 

26 Returns: 

27 The PolyLine serialized into a string. 

28 ''' 

29 return json.dumps(dataclasses.astuple(value)[0]) 

30 

31 @staticmethod 

32 def deserialize(value: str, metadata: Mapping[str, Any]={}) -> Optional[PolyLine]: 

33 ''' 

34 Restores a PolyLine from a string. 

35 

36 Args: 

37 value (str): A string containing a serialized PolyLine. 

38 

39 Returns: 

40 The PolyLine restored from the string or None if the string was empty. 

41 

42 Raises: 

43 Exception: exceptions related to invalid string format. 

44 ''' 

45 return ( 

46 PolyLine.from_value(json.loads(value)) 

47 if value is not None and len(value) > 0 else 

48 None 

49 )