I would like to know if it's possible to check if a Python code has a correct syntax, without running it, and from a Python program. The source code could be in a file or better in a variable.
The application is the following: I have a simple text editor, embedded in my application, used for editing Python scripts. And I'd like to add warnings when the syntax is not correct.
My current idea would be to try importing the file and catch a SyntaxError exception that would contains the erroneous line. But I don't want it to execute at all. Any idea?
That's the job for ast.parse:
>>> import ast
>>> ast.parse('print 1') # does not execute
<_ast.Module at 0x222af10>
>>> ast.parse('garbage(')
File "<unknown>", line 1
garbage(
^
SyntaxError: unexpected EOF while parsing
I just found out that I could probably use the compile function. But I'm open to better suggestions.
import sys
# filename is the path of my source.
source = open(filename, 'r').read() + '\n'
try:
compile(source, filename, 'exec')
except SyntaxError as e:
# do stuff.
You could use PyLint and call it when the file is saved, and it'll check that file for errors (it can detect much more than just syntax errors, look at the documentation).
Related
I'm working on a web application where candidates submit python code and the platform passes them through a unit test which is written by the recruiters.
I'm using Django and I'm having trouble validating the unit-test file.
How to test if a file is a python file and that it's correctly formed (no Syntax or lexical errors)? using Python ?
Thanks
You can use the built-in function compile. Pass the content of the file as a string as the first param, file from which the code was read (If it wasn't read from a file, you can give a name yourself) as the second param and mode as the third.
Try:
with open("full/file/path.py") as f:
try:
compile(f.read(), 'your_code_name', 'exec')
except Exception as ex:
print(ex)
In case the content of the test file is:
print 1
The compile function will throw a SyntaxError and the following message will be printed:
Missing parentheses in call to 'print'. Did you mean print(1)? (your_code_name, line 1)
I am new to python and programming. Starting to try few things for my project..
My problem is as below
p=subprocess.Popen(Some command which gives me output],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
p.wait()
content=p.stdout.readlines()
for line in content:
filedata=line.lstrip().rstrip()
-----> I want this filedata output to open and save it to a file.
If i use print filedata it works and gives me exactly what i wanted but i donot want to print and wanted to use this data later.
Thanks in advance..
You can do that in following two ways.
Option one uses more traditional way of file handling, I have used with statement, using with statement you don't have to worry about closing the file
Option two, which makes use of pathlib module and this is new in version 3.4 (I recommend using this)
somefile.txt is the full file path in file system. I've also included documentation links and I highly recommend going through those.
OPTION ONE
p=subprocess.Popen(Some command which gives me output],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
p.wait()
content=p.stdout.readlines()
for line in content:
filedata=line.lstrip().rstrip()
with open('somefile.txt', 'a') as file:
file.write(filedata + '\n')
Documentation for The with Statement
OPTION TWO - For Python 3.4 or above
import pathlib
p=subprocess.Popen(Some command which gives me output],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
p.wait()
content=p.stdout.readlines()
for line in content:
filedata=line.lstrip().rstrip()
pathlib.Path('somefile.txt').write_text(filedata + '\n')
Documentation on Pathlib module
How can I run a text file use Python?
For example; if the text file is called textfile.txt, and it contains the line:
print("Hello")
How can I use Python to run this as lines of code?
You can use exec: https://docs.python.org/3/library/functions.html#exec
so your code would look somehow like this:
filehandle = open("filename.txt","r")
code_to_execute = filehandle.read()
exec(code_to_execute)
remeber that your file has to have correct syntax so no syntax error occours (mostly its about indentation)
Simply call the python interpreter and pass it the file as such:
python yourfile.txt
I recently made a Twitter-bot that takes a specified .txt file and tweets it out, line by line. A lot of the features I built into the program to troubleshoot some formatting issues actually allows the program to work with pretty much any text file.
I would like build in a feature where I can "import" a .txt file to use.
I put that in quotes because the program runs in the command line at them moment.
I figured there are two ways I can tackle this problem but need some guidance on each:
A) I begin the program with a prompt asking which file the user want to use. This is stored as a string (lets say variable string) and the code looks like this-
file = open(string,'r')
There are two main issues with. The first is I'm unsure how to keep the program from crashing if the program specified is misspelled or does not exist. The second is that it won't mesh with future development (eventually I'd like to build app functionality around this program)
B) Somehow specify the desired file somehow in the command line. While the program will still occasionally crash, it isn't as inconvenient to the user. Also, this would lend itself to future development, as it'll be easier to pass a value in through the command line than an internal prompt.
Any ideas?
For the first part of the question, exception handling is the way to go . Though for the second part you can also use a module called argparse.
import argparse
# creating command line argument with the name file_name
parser = argparse.ArgumentParser()
parser.add_argument("file_name", help="Enter file name")
args = parser.parse_args()
# reading the file
with open(args.file_name,'r') as f:
file = f.read()
You can read more about the argparse module on its documentation page.
Regarding A), you may want to investigate
try:
with open(fname) as f:
blah = f.read()
except Exception as ex:
# handle error
and for B) you can, e.g.
import sys
fname = sys.argv[1]
You could also combine the both to make sure that the user has passed an argument:
#!/usr/bin/env python
# encoding: utf-8
import sys
def tweet_me(text):
# your bot goes here
print text
if __name__ == '__main__':
try:
fname = sys.argv[1]
with open(fname) as f:
blah = f.read()
tweet_me(blah)
except Exception as ex:
print ex
print "Please call this as %s <name-of-textfile>" % sys.argv[0]
Just in case someone wonders about the # encoding: utf-8 line. This allows the source code to contain utf-8 characters. Otherwise only ASCII is allowed, which would be ok for this script. So the line is not necessary. I was, however, testing the script on itself (python x.py x.py) and, as a little test, added a utf-8 comment (# รค). In real life, you will have to care a lot more for character encoding of your input...
Beware, however, that just catching any Exception that may arise from the whole program is not considered good coding style. While Python encourages to assume the best and try it, it might be wise to catch expectable errors right where they happen. For example , accessing a file which does not exist will raise an IOError. You may end up with something like:
except IndexError as ex:
print "Syntax: %s <text-file>" % sys.argv[0]
except IOError as ex:
print "Please provide an existing (and accessible) text-file."
except Exception as ex:
print "uncaught Exception:", type(ex)
Let me first say that I'm a newbie to python, and I've written a python shell named test.py. The content is as follows:
#!/usr/bin/python
import os
cur_dir = os.getcwd();
print 'hello world' > cur_dir+"/test.log" 2>&1
When I run test.py by "python test.py", it says:
File "test.py", line 4
print 'hello world' > cur_dir+"/test.log" 2>&1
^
SyntaxError: invalid syntax
Could anyone give me some idea? Thanks!
P.S.
what I want is to write a string in test.log file. That's all. As I said first, I'm REALLY new to python, so please don't be so harsh to me :), I'm gonna read through the tutorial first to have a glimpse of python, Thanks to all you guys!
You are using invalid syntax. As a result Python gives you the error:
SyntaxError: invalid syntax
To fix this, stop using the invalid syntax.
Also in the future, explain what you are trying to achieve, and your answers are likely to tell you how to achieve that.
The invalid syntax looks like it's a bash redirection of stderr to stdout which makes no sense in the context above, so it's not possible to figure out what you are actually trying to do.
To write to a file you do this:
thefile = open('path_to_the_file', 'wt')
thefile.write('The file text.\n')
However, to do logging, you are better off using the logging module.
Use '>>' with print to redirect it to an alternate stream.
See How to print to stderr in Python?