my dog learned polymorphism*
The moose likes Jython/Python and the fly likes how to Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Languages » Jython/Python
Bookmark "how to "compile" python" Watch "how to "compile" python" New topic
Author

how to "compile" python

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30130
    
150

Since Steve wants something to research, I have a question I've been thinking about looking at.

The question is whether there is a way to "compile" Python to check the syntax and method calls are valid without running it? In particular when it calls other libraries.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

First, I think when you import a Python module, it compiles it to Python byte code immediately (and produces a .pyc file). So from an interactive Python command line you could import your module, then check for the .pyc file. This may be implementation specific though.

Second, there is a py_compile module which lets you compile your python script without executing it. So again from an interactive command line, you could do:

To produce the compiled .pyc files. I think you can also run it from an OS command prompt like:

to compile multiple files at once.


Steve
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Jeanne Boyarsky wrote:The question is whether there is a way to "compile" Python to check the syntax and method calls are valid without running it? In particular when it calls other libraries.

As for what compiling does - it makes sure the compiled code has proper syntax, but it does not process method calls, so I don't think it will be able to double check method call parameter counts, etc... those types of errors will pass through to run time still. But the syntax errors should get caught.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30130
    
150

A high school team wrote a wrapper to avoid this problem. It makes sense there isn't something more generic. Unfortunate though. Not everything can be unit tested.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15960
    
  19

The whole point of late-binding languages like Python, Perl, Ruby, et. al., is that you don't have to have everything nailed down until the very last moment. You just "Git 'er Dun!" and worry about the consequences later. If you'd prefer to be humiliated, before you place the code into production, the best way to do that is to employ an early-binding language such as Java. Or COBOL. Or something like that.

Not to say that you can't try, but the community as a whole has other goals, so you're sort of swimming upstream.

Actually, it's the lack of strong typing that usually gets me, and that really can't be compensated for in a compiler or even in unit testing, since testing would only demonstrate success/failure for the objects you submitted for test, not stuff coming in from Left Field the way it does in real life.


Customer surveys are for companies who didn't pay proper attention to begin with.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30130
    
150

Tim Holloway wrote:The whole point of late-binding languages like Python, Perl, Ruby, et. al., is that you don't have to have everything nailed down until the very last moment. You just "Git 'er Dun!" and worry about the consequences later. If you'd prefer to be humiliated, before you place the code into production, the best way to do that is to employ an early-binding language such as Java. Or COBOL. Or something like that.

Not to say that you can't try, but the community as a whole has other goals, so you're sort of swimming upstream.

Actually, it's the lack of strong typing that usually gets me, and that really can't be compensated for in a compiler or even in unit testing, since testing would only demonstrate success/failure for the objects you submitted for test, not stuff coming in from Left Field the way it does in real life.

I agree. I think Java is a better choice than Python for this project. The experiment with Python is for the students to show that Python makes development "faster." I buy that for prototyping stuff. For the "production" robot where it blows up in competition because you are calling a method that doesn't exist on code that hasn't been run before, this seems like a sure way to lose a match.

High school students are better about worrying about the consequences later than I am. Hence looking at ways to mitigate it.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15960
    
  19

High School students think about consequences?

Good luck with that. You end up with the hacker mentality. Oh, we can fix that! (repeat, ad nauseum.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30130
    
150

Tim Holloway wrote:High School students think about consequences?

Sure. After the problem occurs.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to "compile" python
 
Similar Threads
which is best platform form for Cloud devlopement ?
How to find unused Jar libs in a huge application?
WA #1.....word association
Calling python function from java . NEED HELP URGENT
Access html table data in python