Skip to content

Commit 9e5bc43

Browse files
committed
Add the is_nan FLOAT attribute
1 parent f2fc9a4 commit 9e5bc43

File tree

4 files changed

+40
-26
lines changed

4 files changed

+40
-26
lines changed

docs/source/attributes.rst

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,14 @@ The following attributes are builtin to the default :py:class:`~.Context` object
5151
+-------------------+-------------------------------------+
5252
| ``zone_name`` | :py:attr:`~.DataType.STRING` |
5353
+-------------------+-------------------------------------+
54-
| :py:attr:`~.DataType.TIMEDELTA` **Attributes** |
55-
+-------------------+-------------------------------------+
56-
| ``days`` | :py:attr:`~.DataType.FLOAT` |
57-
+-------------------+-------------------------------------+
58-
| ``seconds`` | :py:attr:`~.DataType.FLOAT` |
59-
+-------------------+-------------------------------------+
60-
| ``microseconds`` | :py:attr:`~.DataType.FLOAT` |
61-
+-------------------+-------------------------------------+
62-
| ``total_seconds`` | :py:attr:`~.DataType.FLOAT` |
63-
+-------------------+-------------------------------------+
6454
| :py:attr:`~.DataType.FLOAT` **Attributes** :sup:`1` |
6555
+-------------------+-------------------------------------+
6656
| ``ceiling`` | :py:attr:`~.DataType.FLOAT` |
6757
+-------------------+-------------------------------------+
6858
| ``floor`` | :py:attr:`~.DataType.FLOAT` |
6959
+-------------------+-------------------------------------+
60+
| ``is_nan`` | :py:attr:`~.DataType.BOOLEAN` |
61+
+-------------------+-------------------------------------+
7062
| ``to_flt`` | :py:attr:`~.DataType.FLOAT` |
7163
+-------------------+-------------------------------------+
7264
| ``to_str`` | :py:attr:`~.DataType.STRING` |
@@ -111,6 +103,16 @@ The following attributes are builtin to the default :py:class:`~.Context` object
111103
+-------------------+-------------------------------------+
112104
| ``length`` | :py:attr:`~.DataType.FLOAT` |
113105
+-------------------+-------------------------------------+
106+
| :py:attr:`~.DataType.TIMEDELTA` **Attributes** |
107+
+-------------------+-------------------------------------+
108+
| ``days`` | :py:attr:`~.DataType.FLOAT` |
109+
+-------------------+-------------------------------------+
110+
| ``seconds`` | :py:attr:`~.DataType.FLOAT` |
111+
+-------------------+-------------------------------------+
112+
| ``microseconds`` | :py:attr:`~.DataType.FLOAT` |
113+
+-------------------+-------------------------------------+
114+
| ``total_seconds`` | :py:attr:`~.DataType.FLOAT` |
115+
+-------------------+-------------------------------------+
114116

115117
FLOAT Attributes :sup:`1`
116118
^^^^^^^^^^^^^^^^^^^^^^^^^

docs/source/change_log.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ this list is curated by the development team for note worthy changes.
77
Version 4.x.x
88
-------------
99

10+
Version 4.3.0
11+
^^^^^^^^^^^^^
12+
13+
Released :release:`4.3.0` on January 15th, 2024
14+
15+
* Added the ``is_nan`` attribute for ``FLOAT`` values
16+
1017
Version 4.2.0
1118
^^^^^^^^^^^^^
1219

lib/rule_engine/engine.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -241,22 +241,6 @@ def datetime_year(self, value):
241241
def datetime_zone_name(self, value):
242242
return value.tzname()
243243

244-
@attribute('days', ast.DataType.TIMEDELTA, result_type=ast.DataType.FLOAT)
245-
def timedelta_days(self, value):
246-
return value.days
247-
248-
@attribute('seconds', ast.DataType.TIMEDELTA, result_type=ast.DataType.FLOAT)
249-
def timedelta_seconds(self, value):
250-
return value.seconds
251-
252-
@attribute('microseconds', ast.DataType.TIMEDELTA, result_type=ast.DataType.FLOAT)
253-
def timedelta_microseconds(self, value):
254-
return value.microseconds
255-
256-
@attribute('total_seconds', ast.DataType.TIMEDELTA, result_type=ast.DataType.FLOAT)
257-
def timedelta_total_seconds(self, value):
258-
return value.total_seconds()
259-
260244
@attribute('ceiling', ast.DataType.FLOAT, result_type=ast.DataType.FLOAT)
261245
def float_ceiling(self, value):
262246
return _float_op(value, math.ceil)
@@ -265,6 +249,10 @@ def float_ceiling(self, value):
265249
def float_floor(self, value):
266250
return _float_op(value, math.floor)
267251

252+
@attribute('is_nan', ast.DataType.FLOAT, result_type=ast.DataType.BOOLEAN)
253+
def float_is_nan(self, value):
254+
return math.isnan(value)
255+
268256
@attribute('to_flt', ast.DataType.FLOAT, result_type=ast.DataType.FLOAT)
269257
def float_to_flt(self, value):
270258
return value
@@ -312,6 +300,22 @@ def string_to_int(self, value):
312300
raise errors.EvaluationError('data type mismatch (not an integer number)')
313301
return value
314302

303+
@attribute('days', ast.DataType.TIMEDELTA, result_type=ast.DataType.FLOAT)
304+
def timedelta_days(self, value):
305+
return value.days
306+
307+
@attribute('seconds', ast.DataType.TIMEDELTA, result_type=ast.DataType.FLOAT)
308+
def timedelta_seconds(self, value):
309+
return value.seconds
310+
311+
@attribute('microseconds', ast.DataType.TIMEDELTA, result_type=ast.DataType.FLOAT)
312+
def timedelta_microseconds(self, value):
313+
return value.microseconds
314+
315+
@attribute('total_seconds', ast.DataType.TIMEDELTA, result_type=ast.DataType.FLOAT)
316+
def timedelta_total_seconds(self, value):
317+
return value.total_seconds()
318+
315319
@attribute('is_empty', ast.DataType.ARRAY, ast.DataType.STRING, ast.DataType.MAPPING, ast.DataType.SET, result_type=ast.DataType.BOOLEAN)
316320
def value_is_empty(self, value):
317321
return len(value) == 0

tests/ast/expression/attribute.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ def test_ast_expression_float_attributes(self):
144144
attributes = {
145145
'ceiling': decimal.Decimal('4'),
146146
'floor': decimal.Decimal('3'),
147+
'is_nan': False,
147148
'to_flt': flt,
148149
'to_str': '3.14159'
149150
}

0 commit comments

Comments
 (0)