This is my custom expression parser (as my programming exercise). The functions are similar to eval()
in Python.
In other words, this is my attempt to reimplement the eval()
by myself without any guidance, so please don't expect
the blazingly fast performance on this lib.
- Import my lib
from expression_parser import SimpleParser
- Create the
SimpleParser
objectexprs = SimpleParser()
- You can declare some custom variable inside your expression via callback function. Lambda function also accepted.
def randomness(): r = random.randint(0, 10) return math.sin(r * 0.01 * math.pi) exprs.define(var_name="random_var", callback=randomness) exprs.define(var_name="กะเพราไข่ดาว", callback=lambda: 60)
- Let's rock! with your expression
exprs.eval("(กะเพราไข่ดาว*2)*50/100") exprs.eval("random_var/(-2.00)")
- I have introduced some weird operation which is
%
. This operation basically does the multiplication with0.01
- For example:
50%
equals to0.5
- For example:
- Compared to Python's
eval()
, my expression parser has notoo many nested parentheses
error when the expression is too long - The callback function is tied to my method
define
. Therefore, it will perform the callback function multiple times if you put your variable in more than one occurrence in your expression.- In my opinion, this is good in some cases, such as random variables..? maybe
- Also, it may be worse in case that you let your callback perform some time-consuming task.
- Of course, it is slower than Python's
eval()
in many cases.