I would like to build something into my Python module that can throw a warning if the user has not done something correctly. For example if the user has a while loop with nothing in it that will block threads, or a user does not initialize something correctly.
Currently I am loading the python file before it runs with a "pre-runtime" script to check these warnings. However it still requires the user to run this pre-check script. I would like to figure out how to do this in my main module. Aside from loading the python script as a string and then parsing it I can't think of a good way to do this.
For example if the user runs this code
import myModule
test = myModule.module()
print("Hello")
while 1:
True
They would get
>> Warning, you used a print statement, they have no power here.
Or the alternative example in the other direction
import myModule
test = myModule.module()
They would get
>> Warning there is no while True loop, your code will just exit
-
Most IDEs will check this automatically for you...12944qwerty– 12944qwerty2021年04月23日 12:34:00 +00:00Commented Apr 23, 2021 at 12:34
-
Yes they will check normally errors that are pythonic. I am looking for special case handling related to this specific application. As a super basic example let's say I never want a user to use "print("hello")", again silly example but exactly the kind of case I am looking at. Python will not catch this as an error but in the context of my system it is an error.Meozaa– Meozaa2021年04月23日 12:37:57 +00:00Commented Apr 23, 2021 at 12:37
-
1So you're trying to look for usage errors?12944qwerty– 12944qwerty2021年04月23日 12:40:58 +00:00Commented Apr 23, 2021 at 12:40
-
1if you are sure you are going to input a valid python code, something from docs.python.org/3/library/ast.html can helppython_user– python_user2021年04月23日 12:42:07 +00:00Commented Apr 23, 2021 at 12:42
-
1BTW, edit your question elaborating exactly what you want, instead of in the comment section.12944qwerty– 12944qwerty2021年04月23日 13:03:58 +00:00Commented Apr 23, 2021 at 13:03
1 Answer 1
I am just writing this as a solution because I found this interesting, there may be better ways to accomplish.
check.py
import ast
with open('temp.py') as file:
module_node = ast.parse(file.read())
for node in ast.walk(module_node):
# check if there is `while True` loop
if isinstance(node, ast.While) and isinstance(node.test, ast.Constant) and node.test.value is True:
print('There is a `while True` in the code', node.lineno)
# check if there is `print` call in the code
elif isinstance(node, ast.Call) and node.func.id == 'print':
print('There is a `print` call in the code', node.lineno)
temp.py
import foo # check.py does not run this import
while 1 > 2:
while True:
print('some text', '12321')
Output
There is a `while True` in the code 4
There is a `print` call in the code 5
So you get the idea, you just add in conditions in the for loop and have a message printed. Again, there may be better alternatives.
2 Comments
os.path.basename(__file__) to open the script automatically so the user doesn't have to import it. Solution found! Thanks!