danthedeckie / simpleeval

Simple Safe Sandboxed Extensible Expression Evaluator for Python
Other
451 stars 85 forks source link

[RFC] Executors - the SimpleEval equivalent of exec() #66

Closed zhudotexe closed 2 years ago

zhudotexe commented 5 years ago

Note: This PR includes the changes in #62, #63, and #64.

I'd like to hear any comments on the idea of extending SimpleEval to execute a full body of statements, akin to py3's exec() function! This draft PR includes an implementation (starting on L746) that builds off of my changes in #63 of SimpleExecutor.execute(expression).

Additionally, I implemented multi-line statements and control flow (if, for, while, break, etc) in ExecutorWithControl.

Let me know if this sounds like something that's out of this library's scope, or if I should write up some test cases for executors!

Here's some examples of executors in action:

If-else

>>> import simpleeval
>>> e = simpleeval.ExecutorWithControl(functions={'range': range, 'print': print})
>>> expr = """
... if 1 < 0:
...     print("true")
... else:
...     print("false")
... """.strip()
>>> e.execute(expr)
false

For

>>> import simpleeval
>>> e = simpleeval.ExecutorWithControl(functions={'range': range, 'print': print})
>>> expr = """
... for i in range(5):
...     print(i)
... print(i)
... """.strip()
>>> e.execute(expr)
0
1
2
3
4
4

Nested control

>>> import simpleeval
>>> e = simpleeval.ExecutorWithControl(functions={'range': range, 'print': print})
>>> expr = """
... for i in range(5):
...     if i < 2:
...         print(f"{i} < 2")
...     elif i == 2:
...         print(f"{i} == 2")
...     else:
...         print(f"{i} > 2")
... """.strip()
>>> e.execute(expr)
0 < 2
1 < 2
2 == 2
3 > 2
4 > 2

Keyword control

>>> import simpleeval
>>> e = simpleeval.ExecutorWithControl(functions={'range': range, 'print': print})
>>> expr = """
... for i in range(5):
...     if i == 2:
...         break
... 
... return i
... """.strip()
>>> e.execute(expr)
2
pep8speaks commented 5 years ago

Hello @mommothazaz123! Thanks for opening this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 366:80: E501 line too long (82 > 79 characters) Line 368:80: E501 line too long (103 > 79 characters) Line 610:80: E501 line too long (97 > 79 characters) Line 611:80: E501 line too long (109 > 79 characters) Line 612:80: E501 line too long (81 > 79 characters) Line 626:80: E501 line too long (82 > 79 characters) Line 680:80: E501 line too long (86 > 79 characters) Line 684:80: E501 line too long (101 > 79 characters) Line 707:80: E501 line too long (86 > 79 characters) Line 726:80: E501 line too long (103 > 79 characters) Line 736:80: E501 line too long (112 > 79 characters) Line 738:80: E501 line too long (119 > 79 characters) Line 752:80: E501 line too long (101 > 79 characters) Line 784:80: E501 line too long (85 > 79 characters) Line 801:80: E501 line too long (105 > 79 characters) Line 803:80: E501 line too long (106 > 79 characters) Line 806:80: E501 line too long (93 > 79 characters) Line 838:80: E501 line too long (112 > 79 characters)

Line 16:80: E501 line too long (110 > 79 characters) Line 17:80: E501 line too long (83 > 79 characters) Line 597:45: E231 missing whitespace after ':' Line 597:50: E231 missing whitespace after ':' Line 597:55: E231 missing whitespace after ':' Line 602:59: E231 missing whitespace after ':' Line 602:64: E231 missing whitespace after ':' Line 602:69: E231 missing whitespace after ':' Line 605:66: E231 missing whitespace after ',' Line 607:68: E231 missing whitespace after ':' Line 607:73: E231 missing whitespace after ':' Line 622:53: E231 missing whitespace after ':' Line 622:58: E231 missing whitespace after ':' Line 627:68: E231 missing whitespace after ':' Line 627:73: E231 missing whitespace after ':' Line 656:80: E501 line too long (83 > 79 characters) Line 823:80: E501 line too long (90 > 79 characters) Line 858:23: E231 missing whitespace after ':' Line 858:26: E231 missing whitespace after ':'

coveralls commented 5 years ago

Coverage Status

Coverage decreased (-4.6%) to 95.094% when pulling ed11c10ec3706caf497aab3518c3f71ae4dd47fc on mommothazaz123:executors into b47858d7153a05f64da16c4425c38f481e510d12 on danthedeckie:master.

coveralls commented 5 years ago

Coverage Status

Coverage decreased (-4.6%) to 95.114% when pulling ed11c10ec3706caf497aab3518c3f71ae4dd47fc on mommothazaz123:executors into b47858d7153a05f64da16c4425c38f481e510d12 on danthedeckie:master.