Skip to content

Commit

Permalink
Add the is_nan FLOAT attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroSteiner committed Jan 15, 2024
1 parent f2fc9a4 commit 9e5bc43
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
22 changes: 12 additions & 10 deletions docs/source/attributes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,14 @@ The following attributes are builtin to the default :py:class:`~.Context` object
+-------------------+-------------------------------------+
| ``zone_name`` | :py:attr:`~.DataType.STRING` |
+-------------------+-------------------------------------+
| :py:attr:`~.DataType.TIMEDELTA` **Attributes** |
+-------------------+-------------------------------------+
| ``days`` | :py:attr:`~.DataType.FLOAT` |
+-------------------+-------------------------------------+
| ``seconds`` | :py:attr:`~.DataType.FLOAT` |
+-------------------+-------------------------------------+
| ``microseconds`` | :py:attr:`~.DataType.FLOAT` |
+-------------------+-------------------------------------+
| ``total_seconds`` | :py:attr:`~.DataType.FLOAT` |
+-------------------+-------------------------------------+
| :py:attr:`~.DataType.FLOAT` **Attributes** :sup:`1` |
+-------------------+-------------------------------------+
| ``ceiling`` | :py:attr:`~.DataType.FLOAT` |
+-------------------+-------------------------------------+
| ``floor`` | :py:attr:`~.DataType.FLOAT` |
+-------------------+-------------------------------------+
| ``is_nan`` | :py:attr:`~.DataType.BOOLEAN` |
+-------------------+-------------------------------------+
| ``to_flt`` | :py:attr:`~.DataType.FLOAT` |
+-------------------+-------------------------------------+
| ``to_str`` | :py:attr:`~.DataType.STRING` |
Expand Down Expand Up @@ -111,6 +103,16 @@ The following attributes are builtin to the default :py:class:`~.Context` object
+-------------------+-------------------------------------+
| ``length`` | :py:attr:`~.DataType.FLOAT` |
+-------------------+-------------------------------------+
| :py:attr:`~.DataType.TIMEDELTA` **Attributes** |
+-------------------+-------------------------------------+
| ``days`` | :py:attr:`~.DataType.FLOAT` |
+-------------------+-------------------------------------+
| ``seconds`` | :py:attr:`~.DataType.FLOAT` |
+-------------------+-------------------------------------+
| ``microseconds`` | :py:attr:`~.DataType.FLOAT` |
+-------------------+-------------------------------------+
| ``total_seconds`` | :py:attr:`~.DataType.FLOAT` |
+-------------------+-------------------------------------+

FLOAT Attributes :sup:`1`
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
7 changes: 7 additions & 0 deletions docs/source/change_log.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ this list is curated by the development team for note worthy changes.
Version 4.x.x
-------------

Version 4.3.0
^^^^^^^^^^^^^

Released :release:`4.3.0` on January 15th, 2024

* Added the ``is_nan`` attribute for ``FLOAT`` values

Version 4.2.0
^^^^^^^^^^^^^

Expand Down
36 changes: 20 additions & 16 deletions lib/rule_engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,22 +241,6 @@ def datetime_year(self, value):
def datetime_zone_name(self, value):
return value.tzname()

@attribute('days', ast.DataType.TIMEDELTA, result_type=ast.DataType.FLOAT)
def timedelta_days(self, value):
return value.days

@attribute('seconds', ast.DataType.TIMEDELTA, result_type=ast.DataType.FLOAT)
def timedelta_seconds(self, value):
return value.seconds

@attribute('microseconds', ast.DataType.TIMEDELTA, result_type=ast.DataType.FLOAT)
def timedelta_microseconds(self, value):
return value.microseconds

@attribute('total_seconds', ast.DataType.TIMEDELTA, result_type=ast.DataType.FLOAT)
def timedelta_total_seconds(self, value):
return value.total_seconds()

@attribute('ceiling', ast.DataType.FLOAT, result_type=ast.DataType.FLOAT)
def float_ceiling(self, value):
return _float_op(value, math.ceil)
Expand All @@ -265,6 +249,10 @@ def float_ceiling(self, value):
def float_floor(self, value):
return _float_op(value, math.floor)

@attribute('is_nan', ast.DataType.FLOAT, result_type=ast.DataType.BOOLEAN)
def float_is_nan(self, value):
return math.isnan(value)

@attribute('to_flt', ast.DataType.FLOAT, result_type=ast.DataType.FLOAT)
def float_to_flt(self, value):
return value
Expand Down Expand Up @@ -312,6 +300,22 @@ def string_to_int(self, value):
raise errors.EvaluationError('data type mismatch (not an integer number)')
return value

@attribute('days', ast.DataType.TIMEDELTA, result_type=ast.DataType.FLOAT)
def timedelta_days(self, value):
return value.days

@attribute('seconds', ast.DataType.TIMEDELTA, result_type=ast.DataType.FLOAT)
def timedelta_seconds(self, value):
return value.seconds

@attribute('microseconds', ast.DataType.TIMEDELTA, result_type=ast.DataType.FLOAT)
def timedelta_microseconds(self, value):
return value.microseconds

@attribute('total_seconds', ast.DataType.TIMEDELTA, result_type=ast.DataType.FLOAT)
def timedelta_total_seconds(self, value):
return value.total_seconds()

@attribute('is_empty', ast.DataType.ARRAY, ast.DataType.STRING, ast.DataType.MAPPING, ast.DataType.SET, result_type=ast.DataType.BOOLEAN)
def value_is_empty(self, value):
return len(value) == 0
Expand Down
1 change: 1 addition & 0 deletions tests/ast/expression/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def test_ast_expression_float_attributes(self):
attributes = {
'ceiling': decimal.Decimal('4'),
'floor': decimal.Decimal('3'),
'is_nan': False,
'to_flt': flt,
'to_str': '3.14159'
}
Expand Down

0 comments on commit 9e5bc43

Please sign in to comment.