Reputation: 2183
I'm looking for a way to let end users process some data from the user interface. A scripting-engine with a syntax similar to Excel would do.
Example:
The input would be a dict
which contains the data (which is basically variable name for the script) and a string
with the rule.
input = {'a':1, 'b':2, 'c':'add'}
rule_example_1 = "if(c == 'add',a+b,if(c=='sub',a-b,raise ERROR))"'
rule_example_2 = "if c == 'add' return a+b else if c == 'sub' return a-b else raise error()"
The script must be extendable with own functions. Also maybe some definition of types which can be used by users.
Use case:
Basically the users have some 2 dimensional data in their account (product details). And using some scripting rules I would like to let them change product data.
For example if a user wants to exclude all products starting with letter X
, they would create a rule:
set('product_status', if(starts_with(product_title,'X'), 'skip', ''))
or if the user wants to raise the price by 20% if the product price is lower then 10:
set('product_price', if(product_price < 10, product_price*1.20, product_price)
Upvotes: 0
Views: 174
Reputation: 7230
If you want something more user-friendly, perhaps you could try Blockly: https://developers.google.com/blockly/
You can easily integrate it with your application and customize it, creating your own blocks to serve your user's needs.
You may also want to take a look at Mesh: https://github.com/chrispsn/mesh
Or perhaps something simpler: https://pypi.python.org/pypi/simpleeval
Or, even simpler (but less safe), you can use ast.literal_eval()
; this one is part of Python's standard library:
https://docs.python.org/3/library/ast.html#ast.literal_eval
Also this other question may help you: Safe expression parser in Python
Upvotes: 1