Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/icalendar/prop/float.py: 55%
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`."""
3from typing import Any, ClassVar
5from icalendar.compatibility import Self
6from icalendar.error import JCalParsingError
7from icalendar.parser import Parameters
10class vFloat(float):
11 """Float
13 Value Name:
14 FLOAT
16 Purpose:
17 This value type is used to identify properties that contain
18 a real-number value.
20 Format Definition:
21 This value type is defined by the following notation:
23 .. code-block:: text
25 float = (["+"] / "-") 1*DIGIT ["." 1*DIGIT]
27 Description:
28 If the property permits, multiple "float" values are
29 specified by a COMMA-separated list of values.
31 Example:
33 .. code-block:: text
35 1000000.0000001
36 1.333
37 -3.14
39 .. code-block:: pycon
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 """
56 default_value: ClassVar[str] = "FLOAT"
57 params: Parameters
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
66 def to_ical(self) -> bytes:
67 return str(self).encode("utf-8")
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
76 @classmethod
77 def examples(cls) -> list[Self]:
78 """Examples of vFloat."""
79 return [cls(3.1415)]
81 from icalendar.param import VALUE
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)]
87 @property
88 def ical_value(self) -> float:
89 """Converts the FLOAT property type according to :rfc:`5545#section-3.3.7` to a Python float."""
90 return float(self)
92 @classmethod
93 def from_jcal(cls, jcal_property: list) -> Self:
94 """Parse jCal from :rfc:`7265`.
96 Parameters:
97 jcal_property: The jCal property to parse.
99 Raises:
100 ~error.JCalParsingError: If the jCal provided is invalid.
101 """
102 JCalParsingError.validate_property(jcal_property, cls)
103 if jcal_property[0].upper() == "GEO":
104 from icalendar.prop import vGeo
106 return vGeo.from_jcal(jcal_property)
107 JCalParsingError.validate_value_type(jcal_property[3], float, cls, 3)
108 return cls(
109 jcal_property[3],
110 params=Parameters.from_jcal_property(jcal_property),
111 )
114__all__ = ["vFloat"]