diff --git a/CHANGELOG.md b/CHANGELOG.md index bce7dfeb0..31527104e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ ## next release ### Features + +* add possibility to convert hex to in in `calculator` processor with new added function `from_hex` + ### Improvements ### Bugfix diff --git a/logprep/processor/calculator/fourFn.py b/logprep/processor/calculator/fourFn.py index 65612d716..2e28f2143 100644 --- a/logprep/processor/calculator/fourFn.py +++ b/logprep/processor/calculator/fourFn.py @@ -46,6 +46,7 @@ "exp": math.exp, "abs": abs, "trunc": int, + "from_hex": lambda a: int(a, 16), "round": round, "sgn": lambda a: -1 if a < -epsilon else 1 if a > epsilon else 0, # functionsl with multiple arguments @@ -80,7 +81,7 @@ class BNF(Forward): # Optional(e + Word("+-"+nums, nums))) # or use provided pyparsing_common.number, but convert back to str: # fnumber = ppc.number().addParseAction(lambda t: str(t[0])) - fnumber = Regex(r"[+-]?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?") + fnumber = Regex(r"[+-]?[a-z0-9]+(?:\.\d*)?(?:[eE][+-]?\d+)?") ident = Word(alphas, alphanums + "_$") plus, minus, mult, div = map(Literal, "+-*/") @@ -124,7 +125,10 @@ def evaluate_stack(self): try: return int(op) except ValueError: - return float(op) + try: + return float(op) + except ValueError: + return op def __new__(cls): if not hasattr(cls, "instance"): diff --git a/tests/unit/processor/calculator/test_calculator.py b/tests/unit/processor/calculator/test_calculator.py index 459abefba..94292ae99 100644 --- a/tests/unit/processor/calculator/test_calculator.py +++ b/tests/unit/processor/calculator/test_calculator.py @@ -176,12 +176,24 @@ { "filter": "message", "calculator": { - "calc": "int(${field1}, 16)", + "calc": "from_hex(0x${field1})", "target_field": "new_field", }, }, - {"message": "This is a message", "field1": "01e15"}, - {"message": "This is a message", "field1": "1", "new_field": 485}, + {"message": "This is a message", "field1": "ff"}, + {"message": "This is a message", "field1": "ff", "new_field": 255}, + ), + ( + "convert hex to int with prefix", + { + "filter": "message", + "calculator": { + "calc": "from_hex(${field1})", + "target_field": "new_field", + }, + }, + {"message": "This is a message", "field1": "0xff"}, + {"message": "This is a message", "field1": "0xff", "new_field": 255}, ), ] @@ -362,6 +374,7 @@ def test_testcases_failure_handling( ("10+sin(PI/4)^2", 10 + math.sin(math.pi / 4) ** 2), ("trunc(E)", int(math.e)), ("trunc(-E)", int(-math.e)), + ("from_hex(0xff)", 255), ("round(E)", round(math.e)), ("round(-E)", round(-math.e)), ("E^PI", math.e**math.pi),