Coverage for src/ipyvizzu/data/converters/converter.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-02-26 10:12 +0000

1""" 

2This module provides the `ToSeriesListConverter` abstract class. 

3""" 

4 

5from abc import ABC, abstractmethod 

6from typing import Any, List, Optional, Tuple, Union 

7 

8from ipyvizzu.data.infer_type import InferType 

9from ipyvizzu.data.type_alias import ( 

10 DimensionValue, 

11 MeasureValue, 

12 Series, 

13 SeriesValues, 

14) 

15 

16 

17class ToSeriesListConverter(ABC): 

18 """ 

19 Converts data into a list of dictionaries representing series. 

20 Each dictionary contains information about the series `name`, `values` and `type`. 

21 """ 

22 

23 # pylint: disable=too-few-public-methods 

24 

25 def __init__( 

26 self, 

27 default_measure_value: MeasureValue, 

28 default_dimension_value: DimensionValue, 

29 ) -> None: 

30 self._default_measure_value = default_measure_value 

31 self._default_dimension_value = default_dimension_value 

32 

33 @abstractmethod 

34 def get_series_list(self) -> List[Series]: 

35 """ 

36 Convert data to a list of dictionaries representing series. 

37 

38 Returns: 

39 A list of dictionaries representing series, 

40 where each dictionary has `name`, `values` and `type` keys. 

41 """ 

42 

43 @abstractmethod 

44 def _convert_to_series_values_and_type( 

45 self, obj: Any 

46 ) -> Tuple[SeriesValues, InferType]: 

47 """ 

48 Convert object to SeriesValues and InferType. 

49 """ 

50 

51 @abstractmethod 

52 def _convert_to_measure_values(self, obj: Any) -> List[MeasureValue]: 

53 """ 

54 Convert object to a list of MeasureValue. 

55 """ 

56 

57 @abstractmethod 

58 def _convert_to_dimension_values(self, obj: Any) -> List[DimensionValue]: 

59 """ 

60 Convert object to a list of DimensionValue. 

61 """ 

62 

63 def _convert_to_series( 

64 self, 

65 name: Union[str, int], 

66 values: SeriesValues, 

67 infer_type: InferType, 

68 unit: Optional[str] = None, 

69 ) -> Series: 

70 series = { 

71 "name": str(name), 

72 "values": values, 

73 "type": infer_type.value, 

74 } 

75 if unit is not None: 

76 series["unit"] = unit 

77 return series