Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/icalendar/prop/float.py: 54%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

35 statements  

1"""FLOAT values from :rfc:`5545`.""" 

2 

3from typing import Any, ClassVar 

4 

5from icalendar.compatibility import Self 

6from icalendar.error import JCalParsingError 

7from icalendar.parser import Parameters 

8 

9 

10class vFloat(float): 

11 """Float 

12 

13 Value Name: 

14 FLOAT 

15 

16 Purpose: 

17 This value type is used to identify properties that contain 

18 a real-number value. 

19 

20 Format Definition: 

21 This value type is defined by the following notation: 

22 

23 .. code-block:: text 

24 

25 float = (["+"] / "-") 1*DIGIT ["." 1*DIGIT] 

26 

27 Description: 

28 If the property permits, multiple "float" values are 

29 specified by a COMMA-separated list of values. 

30 

31 Example: 

32 

33 .. code-block:: text 

34 

35 1000000.0000001 

36 1.333 

37 -3.14 

38 

39 .. code-block:: pycon 

40 

41 >>> from icalendar.prop import vFloat 

42 >>> float = vFloat.from_ical('1000000.0000001') 

43 >>> float 

44 1000000.0000001 

45 >>> float = vFloat.from_ical('1.333') 

46 >>> float 

47 1.333 

48 >>> float = vFloat.from_ical('+1.333') 

49 >>> float 

50 1.333 

51 >>> float = vFloat.from_ical('-3.14') 

52 >>> float 

53 -3.14 

54 """ 

55 

56 default_value: ClassVar[str] = "FLOAT" 

57 params: Parameters 

58 

59 def __new__( 

60 cls, *args: Any, params: dict[str, Any] | None = None, **kwargs: Any 

61 ) -> Self: 

62 self = super().__new__(cls, *args, **kwargs) 

63 self.params = Parameters(params) 

64 return self 

65 

66 def to_ical(self) -> bytes: 

67 return str(self).encode("utf-8") 

68 

69 @classmethod 

70 def from_ical(cls, ical: str | float) -> Self: 

71 try: 

72 return cls(ical) 

73 except Exception as e: 

74 raise ValueError(f"Expected float value, got: {ical}") from e 

75 

76 @classmethod 

77 def examples(cls) -> list[Self]: 

78 """Examples of vFloat.""" 

79 return [cls(3.1415)] 

80 

81 from icalendar.param import VALUE 

82 

83 def to_jcal(self, name: str) -> list: 

84 """The jCal representation of this property according to :rfc:`7265`.""" 

85 return [name, self.params.to_jcal(), self.VALUE.lower(), float(self)] 

86 

87 @classmethod 

88 def from_jcal(cls, jcal_property: list) -> Self: 

89 """Parse jCal from :rfc:`7265`. 

90 

91 Parameters: 

92 jcal_property: The jCal property to parse. 

93 

94 Raises: 

95 ~error.JCalParsingError: If the jCal provided is invalid. 

96 """ 

97 JCalParsingError.validate_property(jcal_property, cls) 

98 if jcal_property[0].upper() == "GEO": 

99 from icalendar.prop import vGeo 

100 

101 return vGeo.from_jcal(jcal_property) 

102 JCalParsingError.validate_value_type(jcal_property[3], float, cls, 3) 

103 return cls( 

104 jcal_property[3], 

105 params=Parameters.from_jcal_property(jcal_property), 

106 ) 

107 

108 

109__all__ = ["vFloat"]