Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 25 commits into from

Conversation

zhudotexe
Copy link
Contributor

@zhudotexe zhudotexe commented Oct 29, 2019

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
Copy link

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
Copy link

Coverage Status

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

@coveralls
Copy link

coveralls commented Oct 29, 2019

Coverage Status

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

@zhudotexe zhudotexe closed this Jul 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants