Assertion for python scripts

Bart. uzi18 at o2.pl
Fri Nov 2 21:29:19 CET 2007


Friday 02 of November 2007 20:53:12 matthias napisał(a):
> On Nov 2, 12:12 pm, "Matt McCredie" <mccre... at gmail.com> wrote:
> > On 11/2/07, matthias <matthiasblankenh... at yahoo.com> wrote:
> > > 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




More information about the Python-list mailing list