Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/icalendar/prop/float.py: 52%
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
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
1"""FLOAT values from :rfc:`5545`."""
3import math
4from typing import Any, ClassVar
6from icalendar.compatibility import Self
7from icalendar.error import JCalParsingError
8from icalendar.parser import Parameters
11class vFloat(float):
12 """Float
14 Value Name:
15 FLOAT
17 Purpose:
18 This value type is used to identify properties that contain
19 a real-number value.
21 Format Definition:
22 This value type is defined by the following notation:
24 .. code-block:: text
26 float = (["+"] / "-") 1*DIGIT ["." 1*DIGIT]
28 Description:
29 If the property permits, multiple "float" values are
30 specified by a COMMA-separated list of values.
32 Example:
34 .. code-block:: text
36 1000000.0000001
37 1.333
38 -3.14
40 .. code-block:: pycon
42 >>> from icalendar.prop import vFloat
43 >>> float = vFloat.from_ical('1000000.0000001')
44 >>> float
45 1000000.0000001
46 >>> float = vFloat.from_ical('1.333')
47 >>> float
48 1.333
49 >>> float = vFloat.from_ical('+1.333')
50 >>> float
51 1.333
52 >>> float = vFloat.from_ical('-3.14')
53 >>> float
54 -3.14
55 """
57 default_value: ClassVar[str] = "FLOAT"
58 params: Parameters
60 def __new__(
61 cls, *args: Any, params: dict[str, Any] | None = None, **kwargs: Any
62 ) -> Self:
63 self = super().__new__(cls, *args, **kwargs)
64 self.params = Parameters(params)
65 return self
67 def to_ical(self) -> bytes:
68 return str(self).encode("utf-8")
70 @classmethod
71 def from_ical(cls, ical: str | float) -> Self:
72 try:
73 self = cls(ical)
74 except Exception as e:
75 raise ValueError(f"Expected float value, got: {ical}") from e
76 if not math.isfinite(self):
77 raise ValueError(f"Expected finite float value, got: {ical}")
78 return self
80 @classmethod
81 def examples(cls) -> list[Self]:
82 """Examples of vFloat."""
83 return [cls(3.1415)]
85 from icalendar.param import VALUE
87 def to_jcal(self, name: str) -> list:
88 """The jCal representation of this property according to :rfc:`7265`."""
89 return [name, self.params.to_jcal(), self.VALUE.lower(), float(self)]
91 @property
92 def ical_value(self) -> float:
93 """Converts the FLOAT property type according to :rfc:`5545#section-3.3.7` to a Python float."""
94 return float(self)
96 @classmethod
97 def from_jcal(cls, jcal_property: list) -> Self:
98 """Parse jCal from :rfc:`7265`.
100 Parameters:
101 jcal_property: The jCal property to parse.
103 Raises:
104 ~error.JCalParsingError: If the jCal provided is invalid.
105 """
106 JCalParsingError.validate_property(jcal_property, cls)
107 if jcal_property[0].upper() == "GEO":
108 from icalendar.prop import vGeo
110 return vGeo.from_jcal(jcal_property)
111 JCalParsingError.validate_value_type(jcal_property[3], float, cls, 3)
112 return cls(
113 jcal_property[3],
114 params=Parameters.from_jcal_property(jcal_property),
115 )
118__all__ = ["vFloat"]