473,324 Members | 2,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Python switch for syntax checking

Hi all.

I've been wondering why python itself doesn't provide a switch to
check a file for valid syntax. I know that you can currently call

python -c "import py_compile; py_compile.compile(r'MyApp.py')"

but in order to manage this effectively you have to add a shell alias,
write a script, mess about with your editor, or what have you. This
becomes yet another small annoyance I'd like to get rid of. I'm also
aware of PyChecker and other lint like tools, but these run the code and
often I don't want that.

Being able to do

python -s MyApp.py

and see any syntax errors would be a very useful thing. Is there a
specific reason that this doesn't exist (or perhaps it does and I'm
uninformed)? I don't see any relevant PEPs on the list, either.

Jeff Duffy
Jul 18 '05 #1
6 25025
Jeff Duffy wrote:
I've been wondering why python itself doesn't provide a switch to check
a file for valid syntax. I know that you can currently call
python -c "import py_compile; py_compile.compile(r'MyApp.py')"
...

I suspect the reason is threefold.

First, "python MyApp.py" does a syntax check anyway. If MyApp is
not a main program, all you get is the syntax check. As for main
programs, larger applications are often split into smaller files.
I seldom have large main program files to check.

Second, lots of extreme programmer (XP) practitioners hang out here,
and even more who have adopted at leasrt some of the XP techniques.
Those people tend to run unit tests rather than syntax checks.

Third, pychecker provides better analysis if you want static analysis.

I pretty much try to go the unit test way myself. While I would
suggest you try it out, the three reasons above are meant to explain
why nobody has put effort into a syntax check switch.

--Scott David Daniels
Sc***********@Acm.Org

Jul 18 '05 #2
On Sat, 2004-11-20 at 02:46, Jeff Duffy wrote:
Hi all.

I've been wondering why python itself doesn't provide a switch to
check a file for valid syntax. I know that you can currently call

python -c "import py_compile; py_compile.compile(r'MyApp.py')"

but in order to manage this effectively you have to add a shell alias,
write a script, mess about with your editor, or what have you. This
becomes yet another small annoyance I'd like to get rid of. I'm also
aware of PyChecker and other lint like tools, but these run the code and
often I don't want that.


The talk of a new switch to import a module would do the job nicely. If
you just import your script as a module, you know it loads and the main
program runs. Just add the usual

if __name__ == '__main__':
main(sys.argv)

so your code loads as a module without executing its normal functions,
and you have your syntax check plus a bit of other sanity checking (all
modules import, etc).

In the mean time, it's fairly trivial to:

python -c "import myscript"

and not significantly longer / more complex than some sort of check
flag.

That said, I'm all in favour of unit tests to make sure your code
actually runs. Especially in Python, where things like late binding of
names means that you can't check at load time whether names exit, unit
tests are IMO the way to go.

pylint / pychecker may also be worth looking into.

--
Craig Ringer

Jul 18 '05 #3
Jeff Duffy wrote:
Hi all.

I've been wondering why python itself doesn't provide a switch to check
a file for valid syntax. I know that you can currently call

python -c "import py_compile; py_compile.compile(r'MyApp.py')"


If you look at the py_compile module, if it's run as a script it will
compile any filenames you pass to it, so you should be able to do:

python /path/to/py_compile.py MyApp.py

And with Python 2.4 I think it will be:

python -m py_compile MyApp.py

--
Ian Bicking / ia**@colorstudy.com / http://blog.ianbicking.org
Jul 18 '05 #4
On Fri, 19 Nov 2004 12:08:00 -0800, Scott David Daniels <Sc***********@Acm.Org> wrote:
Jeff Duffy wrote:
I've been wondering why python itself doesn't provide a switch to check
a file for valid syntax. I know that you can currently call
python -c "import py_compile; py_compile.compile(r'MyApp.py')"
...I suspect the reason is threefold.

First, "python MyApp.py" does a syntax check anyway. If MyApp is
not a main program, all you get is the syntax check. As for main

??? What does "not a main program mean"? I'm not sure what you mean, e.g.,

[14:00] C:\pywk\clp>python notmain.py
notmain.py executing

[14:00] C:\pywk\clp>python
Python 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import notmain notmain.py executing ^Z

[14:00] C:\pywk\clp>py24
Python 2.4b1 (#56, Nov 3 2004, 01:47:27)
[GCC 3.2.3 (mingw special 20030504-1)] on win32
Type "help", "copyright", "credits" or "license" for more information. import notmain notmain.py executing

Oops, forgot to show you what notmain.py was:
print '%s\n%s%s' % ('-'*40, open('notmain.py').read(), '-'*40)

----------------------------------------
print 'notmain.py executing'

----------------------------------------
programs, larger applications are often split into smaller files.
I seldom have large main program files to check.

Second, lots of extreme programmer (XP) practitioners hang out here,
and even more who have adopted at leasrt some of the XP techniques.
Those people tend to run unit tests rather than syntax checks.

Third, pychecker provides better analysis if you want static analysis.

I pretty much try to go the unit test way myself. While I would
suggest you try it out, the three reasons above are meant to explain
why nobody has put effort into a syntax check switch.

--Scott David Daniels
Sc***********@Acm.Org


Regards,
Bengt Richter
Jul 18 '05 #5
Bengt Richter wrote:
On Fri, 19 Nov 2004 12:08:00 -0800, Scott David Daniels <Sc***********@Acm.Org> wrote:
First, "python MyApp.py" does a syntax check anyway. If MyApp is
not a main program, all you get is the syntax check. As for main

??? What does "not a main program mean"? I'm not sure what you mean, e.g.,


I believe that Scott is referring to the difference between a file run
from the commandline, in which __name__ is set to "__main__", and a file
that's imported as a module, in which __name__ is set to the name of the
module, which is normally the filename minus the .py[c|o|d] extension.

Of course, this would also imply that Scott is presuming that one is
following the good programming practice of not putting any significant
code (other than function, class, and global variable definitions) at
module level, except where protected by an 'if __name__ == "__main__":'
statement. That *is* good practice, and most people do it, but it's not
required so perhaps not a safe presumption...

Jeff Shannon
Technician/Programmer
Credit International
Jul 18 '05 #6
Jeff Duffy originally wrote:
I've been wondering why python itself doesn't provide a switch to
check a file for valid syntax.... Jeff Shannon wrote:
Bengt Richter wrote:
On Fri, 19 Nov 2004 12:08:00 -0800, Scott David Daniels
<Sc***********@Acm.Org> wrote:
First, "python MyApp.py" does a syntax check anyway. If MyApp is
not a main program, all you get is the syntax check. As for main
??? What does "not a main program mean"? I'm not sure what you mean,


I believe that Scott is referring to the difference between a file run
from the commandline, in which __name__ is set to "__main__", and a file
that's imported as a module, in which __name__ is set to the name of the
module, which is normally the filename minus the .py[c|o|d] extension.

I do mean something like that. Essentially, I mean "if the file is
meant to be imported, rather than run."
Of course, this would also imply that Scott is presuming that one is
following the good programming practice of not putting any significant
code (other than function, class, and global variable definitions) at
module level, except where protected by an 'if __name__ == "__main__":'
statement. That *is* good practice, and most people do it, but it's not
required so perhaps not a safe presumption...


Remember, I am explaining why we normally don't have a syntax-check-only
option, and my thesis is that there is no perceived need on the part of
the core implementers.

--Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 393 open (+15) / 3315 closed (+17) / 3708 total (+32) Bugs : 908 open (+22) / 5975 closed (+49) / 6883 total (+71) RFE : 223 open...
3
by: Kinokunya | last post by:
Hi guys, My group and I will be working on our final year project, the scope to do a program/web-based application similar areas of functionalities like the PyLint and PyChecker; a Python syntax...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.