473,239 Members | 1,490 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,239 software developers and data experts.

Assertion for python scripts

Howdy !

I started using the assert() stmt and found it quite useful :-) I
have only one problem: I don't
know how to turn them off again.

I know that "-O" turns off assertions in general. However, how do I
pass thus parameter to
python to an executable script ?

I have tried the following:

1.
!#/usr/bin/env python -O

-Fails with this msg: /usr/bin/env: python -O: No such file or
directory

Also, putting it in quotes won't do it.

2.
Passing the "-O" to the runnable script won't work either.
Here is my question: How do I maintain debug / release builds that
allow me to switch
debug stmts, like assert, on / off ?

Thanx,
Matthias

Nov 2 '07 #1
9 2746
On 11/2/07, matthias <ma*****************@yahoo.comwrote:
Howdy !

I started using the assert() stmt and found it quite useful :-) I
have only one problem: I don't
know how to turn them off again.

I know that "-O" turns off assertions in general. However, how do I
pass thus parameter to
python to an executable script ?

I have tried the following:

1.
!#/usr/bin/env python -O

-Fails with this msg: /usr/bin/env: python -O: No such file or
directory

Also, putting it in quotes won't do it.

2.
Passing the "-O" to the runnable script won't work either.
Here is my question: How do I maintain debug / release builds that
allow me to switch
debug stmts, like assert, on / off ?

Thanx,
Matthias
Use:
python -O -mcompileall path

That command will compile all of the files in the given path and
produce .pyo files. If the .pyo file is present and up-to-date it will
be used instead of the .py file.

Alternatively you could do this:

python -O -mpy_compile somefile.py

which can be used to compile one file at a time.

Many Python programs and modules include a compile step as part of
their installation process. There is also a -OO option, which will
strip doc-strings as well.

Matt
Nov 2 '07 #2
On Nov 2, 12:12 pm, "Matt McCredie" <mccre...@gmail.comwrote:
On 11/2/07, matthias <matthiasblankenh...@yahoo.comwrote:
Howdy !
I started using the assert() stmt and found it quite useful :-) I
have only one problem: I don't
know how to turn them off again.
I know that "-O" turns off assertions in general. However, how do I
pass thus parameter to
python to an executable script ?
I have tried the following:
1.
!#/usr/bin/env python -O
-Fails with this msg: /usr/bin/env: python -O: No such file or
directory
Also, putting it in quotes won't do it.
2.
Passing the "-O" to the runnable script won't work either.
Here is my question: How do I maintain debug / release builds that
allow me to switch
debug stmts, like assert, on / off ?
Thanx,
Matthias

Use:
python -O -mcompileall path

That command will compile all of the files in the given path and
produce .pyo files. If the .pyo file is present and up-to-date it will
be used instead of the .py file.

Alternatively you could do this:

python -O -mpy_compile somefile.py

which can be used to compile one file at a time.

Many Python programs and modules include a compile step as part of
their installation process. There is also a -OO option, which will
strip doc-strings as well.

Matt
Thanx for your reply , Matt !

However, I think I am missing something.

Here is my example prog, assert.py, with the executable bit set:

#!/usr/bin/env python

assert 1 1, "ASSERTTION !"

Running this:

# python ./assert.py
Traceback (most recent call last):
File "assert.py", line 3, in ?
assert 1 1, "ASSERTTION !"
AssertionError: ASSERTTION !

leads to the expected result.

Now I do this as you've recommended:

# python -O -mpy_compile assert.py

This indeed creates a file with the name assert.pyo. That must be the
optimized one.

Now I try this:

# ./assert.py
Traceback (most recent call last):
File "./assert.py", line 3, in ?
assert 1 1, "ASSERTTION !"
AssertionError: ASSERTTION !

Ok, so it still uses the unoptimized version.

Now I try this:

# chmod 755 assert.pyo
# ./assert.pyo
bash: ./assert.pyo: cannot execute binary file

Here is my problem: I want to have an optimized executable version of
assert.py.

I am assuming that I am thinking in an unconventional way wrt Python.
If so, then
how do you start your optimized python scripts ?
Thanx,
Matthias

Nov 2 '07 #3
Friday 02 of November 2007 20:53:12 matthias napisał(a):
On Nov 2, 12:12 pm, "Matt McCredie" <mccre...@gmail.comwrote:
On 11/2/07, matthias <matthiasblankenh...@yahoo.comwrote:
Howdy !
>
I started using the assert() stmt and found it quite useful :-) I
have only one problem: I don't
know how to turn them off again.
>
I know that "-O" turns off assertions in general. However, how do I
pass thus parameter to
python to an executable script ?
>
I have tried the following:
>
1.
!#/usr/bin/env python -O
>
-Fails with this msg: /usr/bin/env: python -O: No such file or
directory
>
Also, putting it in quotes won't do it.
>
2.
Passing the "-O" to the runnable script won't work either.
>
Here is my question: How do I maintain debug / release builds that
allow me to switch
debug stmts, like assert, on / off ?
>
Thanx,
Matthias
Use:
python -O -mcompileall path

That command will compile all of the files in the given path and
produce .pyo files. If the .pyo file is present and up-to-date it will
be used instead of the .py file.

Alternatively you could do this:

python -O -mpy_compile somefile.py

which can be used to compile one file at a time.

Many Python programs and modules include a compile step as part of
their installation process. There is also a -OO option, which will
strip doc-strings as well.

Matt

Thanx for your reply , Matt !

However, I think I am missing something.

Here is my example prog, assert.py, with the executable bit set:

#!/usr/bin/env python

assert 1 1, "ASSERTTION !"

Running this:

# python ./assert.py
Traceback (most recent call last):
File "assert.py", line 3, in ?
assert 1 1, "ASSERTTION !"
AssertionError: ASSERTTION !

leads to the expected result.

Now I do this as you've recommended:

# python -O -mpy_compile assert.py

This indeed creates a file with the name assert.pyo. That must be the
optimized one.

Now I try this:

# ./assert.py
Traceback (most recent call last):
File "./assert.py", line 3, in ?
assert 1 1, "ASSERTTION !"
AssertionError: ASSERTTION !

Ok, so it still uses the unoptimized version.

Now I try this:

# chmod 755 assert.pyo
# ./assert.pyo
bash: ./assert.pyo: cannot execute binary file

Here is my problem: I want to have an optimized executable version of
assert.py.

I am assuming that I am thinking in an unconventional way wrt Python.
If so, then
how do you start your optimized python scripts ?
Thanx,
Matthias
check this:

python ./assert.pyo

Nov 2 '07 #4
On Nov 2, 1:29 pm, "Bart." <uz...@o2.plwrote:
Friday 02 of November 2007 20:53:12 matthias napisa (a):
On Nov 2, 12:12 pm, "Matt McCredie" <mccre...@gmail.comwrote:
On 11/2/07, matthias <matthiasblankenh...@yahoo.comwrote:
Howdy !
I started using the assert() stmt and found it quite useful :-) I
have only one problem: I don't
know how to turn them off again.
I know that "-O" turns off assertions in general. However, how do I
pass thus parameter to
python to an executable script ?
I have tried the following:
1.
!#/usr/bin/env python -O
-Fails with this msg: /usr/bin/env: python -O: No such file or
directory
Also, putting it in quotes won't do it.
2.
Passing the "-O" to the runnable script won't work either.
Here is my question: How do I maintain debug / release builds that
allow me to switch
debug stmts, like assert, on / off ?
Thanx,
Matthias
Use:
python -O -mcompileall path
That command will compile all of the files in the given path and
produce .pyo files. If the .pyo file is present and up-to-date it will
be used instead of the .py file.
Alternatively you could do this:
python -O -mpy_compile somefile.py
which can be used to compile one file at a time.
Many Python programs and modules include a compile step as part of
their installation process. There is also a -OO option, which will
strip doc-strings as well.
Matt
Thanx for your reply , Matt !
However, I think I am missing something.
Here is my example prog, assert.py, with the executable bit set:
#!/usr/bin/env python
assert 1 1, "ASSERTTION !"
Running this:
# python ./assert.py
Traceback (most recent call last):
File "assert.py", line 3, in ?
assert 1 1, "ASSERTTION !"
AssertionError: ASSERTTION !
leads to the expected result.
Now I do this as you've recommended:
# python -O -mpy_compile assert.py
This indeed creates a file with the name assert.pyo. That must be the
optimized one.
Now I try this:
# ./assert.py
Traceback (most recent call last):
File "./assert.py", line 3, in ?
assert 1 1, "ASSERTTION !"
AssertionError: ASSERTTION !
Ok, so it still uses the unoptimized version.
Now I try this:
# chmod 755 assert.pyo
# ./assert.pyo
bash: ./assert.pyo: cannot execute binary file
Here is my problem: I want to have an optimized executable version of
assert.py.
I am assuming that I am thinking in an unconventional way wrt Python.
If so, then
how do you start your optimized python scripts ?
Thanx,
Matthias

check this:

python ./assert.pyo

Yes, I know that this works. Thanx. However, that's not what I was
going for.

Essentially, I am targeting a build system that allows me to build
debug / release versions
of my Python code. I know how to build the Python code, but how do I
package it so that I
can simply dump it on the test system with either build type and the
same calling interface ?

Thanx,
Matthias

Nov 2 '07 #5
Friday 02 of November 2007 22:07:42 matthias napisał(a):
check this:

python ./assert.pyo

Yes, I know that this works. Thanx. However, that's not what I was
going for.

Essentially, I am targeting a build system that allows me to build
debug / release versions
of my Python code. I know how to build the Python code, but how do I
package it so that I
can simply dump it on the test system with either build type and the
same calling interface ?
how about distutils ?
look on the egg paskages :)

Nov 2 '07 #6
En Fri, 02 Nov 2007 16:53:12 -0300, matthias
<ma*****************@yahoo.comescribi:
On Nov 2, 12:12 pm, "Matt McCredie" <mccre...@gmail.comwrote:
>On 11/2/07, matthias <matthiasblankenh...@yahoo.comwrote:
I know that "-O" turns off assertions in general. However, how do I
pass thus parameter to
python to an executable script ?
>Use:
python -O -mcompileall path
This indeed creates a file with the name assert.pyo. That must be the
optimized one.

Now I try this:

# ./assert.py
Traceback (most recent call last):
File "./assert.py", line 3, in ?
assert 1 1, "ASSERTTION !"
AssertionError: ASSERTTION !

Ok, so it still uses the unoptimized version.

Now I try this:

# chmod 755 assert.pyo
# ./assert.pyo
bash: ./assert.pyo: cannot execute binary file

Here is my problem: I want to have an optimized executable version of
assert.py.
Move all your code into modules; those modules can be pre-compiled with -O
as above.
Your main script should contain just a few lines; import the other modules
from inside your main script.
When you import "foo", if python finds "foo.pyo" and it's up to date, it
will use it instead of foo.py;
foo.py doesn't have to be available either.

--
Gabriel Genellina

Nov 3 '07 #7
On Nov 2, 2:14 pm, matthias <matthiasblankenh...@yahoo.comwrote:
Here is my question: How do I maintain debug / release builds that
allow me to switch
debug stmts, like assert, on / off ?
If you want to distribute a single-file optimized version, perhaps
embedding the Python code as a here-file in a shell script would work
for you. For example:

#!/bin/sh
exec python -O <<EOF

assert False

EOF
For anything larger, I second the advice elsewhere in this thread:
build pyo files and distribute them.
Carl Banks

Nov 3 '07 #8
On Nov 2, 11:22 pm, Carl Banks <pavlovevide...@gmail.comwrote:
On Nov 2, 2:14 pm, matthias <matthiasblankenh...@yahoo.comwrote:
Here is my question: How do I maintain debug / release builds that
allow me to switch
debug stmts, like assert, on / off ?

If you want to distribute a single-file optimized version, perhaps
embedding the Python code as a here-file in a shell script would work
for you. For example:

#!/bin/sh
exec python -O <<EOF

assert False

EOF
Not long after I made this suggestion, I suddenly found myself in a
situation where I wanted to do something like this (temporarily: I
needed to disable a pointer check that a buggy C++ extension was
causing). Here's what I ended up with, after falling in a few pits:

#!/bin/sh
MALLOC_CHECK_=0 /usr/bin/python2.4 - "$@" <<'SCRIPT'

<script goes here>

SCRIPT
Carl Banks

Nov 5 '07 #9
On Friday, Nov 2nd 2007 at 14:14 -0000, quoth matthias:

=>Howdy !
=>
=>I started using the assert() stmt and found it quite useful :-) I
=>have only one problem: I don't
=>know how to turn them off again.
=>
=>I know that "-O" turns off assertions in general. However, how do I
=>pass thus parameter to
=>python to an executable script ?

I had similar questions and I decided that what I really needed for my
app was a preprocessor. I looked around and found what I thought to be
really cool:

http://nedbatchelder.com/code/cog

It's a preprocessor written in python which allows you to embed python
code (called cogs) in your python such that the cogs execute and are
replaced by what they output.

I have print statements (effectively) which produce either output or
nothing, depending on what I pass from the Makefile. The files that I
write are called .cog.py and when I execute make, it produces .py files as
output. It's a hugely useful thing to have available. The only slight
disadvantage is that you have to (remember to) run make if you change your
..cog.py files before retrying something.

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
Nov 23 '07 #10

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

Similar topics

3
by: Todd Miller | last post by:
Hi, I recently discovered an assertion failure in the Python garbage collection system when scripts using our C extension (numarray) exit. The assertion is activated for Pythons configured using...
0
by: O'Neal Computer Programmer | last post by:
*** First of all, if there is a previous post on Temple of Elemental Evil, please provide a link to that/them so in order to create a larger resource on this game. Thanks! (Merci) ;-) *** Temple...
50
by: Edward K. Ream | last post by:
I would like to say a few (actually more than a few) words here about some recent discoveries I have made concerning the interaction of Leo and Python. If you don't want to hear an inventor enthuse...
4
by: Logan | last post by:
Several people asked me for the following HOWTO, so I decided to post it here (though it is still very 'alpha' and might contain many (?) mistakes; didn't test what I wrote, but wrote it - more or...
0
by: benevilent | last post by:
Hey, I'm getting an assertion error as a result of embedding python. "Modules/gcmodule.c:231: visit_decref: Assertion `gc->gc.gc_refs != 0' failed." I only get this assertion error with...
1
by: vmalhotra | last post by:
Hi All, I want to do verification in my scripts. So for that what i am doing here are shown below: 1. Telnet to one router. 2. Configure router. 3. Configure routing. Now after doing all...
34
by: Ben Sizer | last post by:
I've installed several different versions of Python across several different versions of MS Windows, and not a single time was the Python directory or the Scripts subdirectory added to the PATH...
24
by: Mark | last post by:
Hi, I'm new to python and looking for a better idiom to use for the manner I have been organising my python scripts. I've googled all over the place about this but found absolutely nothing. I'm...
7
by: Anthony | last post by:
Hi, I'm a FoxPro programmer, but I want to learn python before it's too late. I do a lot of statistical programming, so I import SPSS into python. In my opinion, the best features of Visual...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
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...

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.