Skip to content

Commit

Permalink
implement from_hex function
Browse files Browse the repository at this point in the history
  • Loading branch information
ekneg54 committed Oct 26, 2023
1 parent afca891 commit 8bd3a3f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions logprep/processor/calculator/fourFn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, "+-*/")
Expand Down Expand Up @@ -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"):
Expand Down
19 changes: 16 additions & 3 deletions tests/unit/processor/calculator/test_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
),
]

Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 8bd3a3f

Please sign in to comment.