Skip to content

Commit

Permalink
Add the min and max builtin functions
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroSteiner committed Jul 8, 2023
1 parent a804ace commit 2b511e3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/rule_engine/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ def from_defaults(cls, values=None, **kwargs):
'all': all,
'sum': sum,
'map': _builtin_map,
'max': max,
'min': min,
'filter': _builtin_filter,
'parse_datetime': BuiltinValueGenerator(lambda builtins: functools.partial(_builtin_parse_datetime, builtins)),
'parse_timedelta': parse_timedelta,
Expand All @@ -176,6 +178,8 @@ def from_defaults(cls, values=None, **kwargs):
'any': ast.DataType.FUNCTION('any', return_type=ast.DataType.BOOLEAN, argument_types=(ast.DataType.ARRAY,)),
'sum': ast.DataType.FUNCTION('sum', return_type=ast.DataType.FLOAT, argument_types=(ast.DataType.ARRAY(ast.DataType.FLOAT),)),
'map': ast.DataType.FUNCTION('map', argument_types=(ast.DataType.FUNCTION, ast.DataType.ARRAY)),
'max': ast.DataType.FUNCTION('max', return_type=ast.DataType.FLOAT, argument_types=(ast.DataType.ARRAY(ast.DataType.FLOAT),)),
'min': ast.DataType.FUNCTION('min', return_type=ast.DataType.FLOAT, argument_types=(ast.DataType.ARRAY(ast.DataType.FLOAT),)),
'filter': ast.DataType.FUNCTION('filter', argument_types=(ast.DataType.FUNCTION, ast.DataType.ARRAY)),
'parse_datetime': ast.DataType.FUNCTION('parse_datetime', return_type=ast.DataType.DATETIME, argument_types=(ast.DataType.STRING,)),
'parse_timedelta': ast.DataType.FUNCTION('parse_timedelta', return_type=ast.DataType.TIMEDELTA, argument_types=(ast.DataType.STRING,)),
Expand Down
6 changes: 6 additions & 0 deletions tests/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ def test_engine_buitins_function_map(self):
self.assertBuiltinFunction('map', (2, 4, 6), lambda i: i * 2, [1, 2, 3])
self.assertBuiltinFunction('map', ('A', 'B'), lambda c: c.upper(), ['A', 'B'])

def test_engine_builtins_function_max(self):
self.assertBuiltinFunction('max', 10, [1, 10, -1, 1.5])

def test_engine_builtins_function_min(self):
self.assertBuiltinFunction('min', -1, [1, 10, -1, 1.5])

def test_engine_buitins_function_filter(self):
self.assertBuiltinFunction('filter', (1, 3), lambda i: i % 2, [1, 2, 3])
self.assertBuiltinFunction('filter', ('A', 'B'), lambda c: len(c), ['', 'A', 'B'])
Expand Down

0 comments on commit 2b511e3

Please sign in to comment.