Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

conditional running of code portion

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


ngcbmy at gmail

Aug 4, 2012, 9:16 PM

Post #1 of 8 (330 views)
Permalink
conditional running of code portion

Hi,

How can I implement something like C++'s conditional compile.

if VERBOSE_MODE: print debug information
else: do nothing

But I don't want this condition to be checked during runtime as it
will slow down the code.

Thanks in advance.
--
http://mail.python.org/mailman/listinfo/python-list


maniandram01 at gmail

Aug 4, 2012, 9:43 PM

Post #2 of 8 (315 views)
Permalink
Re: conditional running of code portion [In reply to]

Try pypreprocessor <http://code.google.com/p/pypreprocessor/> .
Better idea:
You should be using the
logging<http://docs.python.org/library/logging.html>module if you want
to print debug information quickly.It uses threads and
is optimized to run fast.

On 5 August 2012 09:46, JW Huang <ngcbmy [at] gmail> wrote:

> Hi,
>
> How can I implement something like C++'s conditional compile.
>
> if VERBOSE_MODE: print debug information
> else: do nothing
>
> But I don't want this condition to be checked during runtime as it
> will slow down the code.
>
> Thanks in advance.
> --
> http://mail.python.org/mailman/listinfo/python-list
>


steve+comp.lang.python at pearwood

Aug 4, 2012, 11:30 PM

Post #3 of 8 (311 views)
Permalink
Re: conditional running of code portion [In reply to]

On Sat, 04 Aug 2012 21:16:04 -0700, JW Huang wrote:

> Hi,
>
> How can I implement something like C++'s conditional compile.
>
> if VERBOSE_MODE: print debug information else: do nothing
>
> But I don't want this condition to be checked during runtime as it will
> slow down the code.


You've profiled your code and found that checking a flag is a bottleneck
in your application? I'd hate to think you were wasting your time trying
to avoid "slowing down" your code by an insignificant amount that nobody
will ever notice.

In general, the way to do C-like conditional compiles is to use C, or at
least to use another language that isn't Python. Almost everything
happens at runtime in Python. Possibly PyPy can optimise away unnecessary
checks, but CPython doesn't.

One of the very few exceptions: you can disable code at compile time like
this:

if __debug__:
do_something()


compiles to the byte-code equivalent of:

do_something()


under normal circumstances, and to nothing at all if Python is running
with the -O optimize flag.


If you are working in a tight loop, you can do this:

if VERBOSE_FLAG:
for item in loop:
print(DEBUG_INFORMATION)
do_actual_work(item)
else:
for item in loop:
do_actual_work(item)




--
Steven
--
http://mail.python.org/mailman/listinfo/python-list


storchaka at gmail

Aug 5, 2012, 9:40 AM

Post #4 of 8 (309 views)
Permalink
Re: conditional running of code portion [In reply to]

On 05.08.12 09:30, Steven D'Aprano wrote:
> If you are working in a tight loop, you can do this:
>
> if VERBOSE_FLAG:
> for item in loop:
> print(DEBUG_INFORMATION)
> do_actual_work(item)
> else:
> for item in loop:
> do_actual_work(item)

Or this:

if VERBOSE_FLAG:
def do_work(item):
print(DEBUG_INFORMATION)
do_actual_work(item)
else:
do_work = do_actual_work

for item in loop:
do_work(item)


--
http://mail.python.org/mailman/listinfo/python-list


steveo at syslang

Aug 5, 2012, 7:50 PM

Post #5 of 8 (305 views)
Permalink
Re: conditional running of code portion [In reply to]

On 8/5/2012 12:43 AM, Ramchandra Apte wrote:
> Try pypreprocessor <http://code.google.com/p/pypreprocessor/> .
> Better idea:
> You should be using the logging <http://docs.python.org/library/logging.html>
> module if you want to print debug information quickly.It uses threads and is
> optimized to run fast.

I never saw pypreprocessor. Looks interesting. I have experience with Ned's cog
preprocessor.

http://nedbatchelder.com/code/cog/

I used this in something that was operating at the packet socket level. I had no
time to test if debug was true.



--
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
--
http://mail.python.org/mailman/listinfo/python-list


maniandram01 at gmail

Aug 6, 2012, 5:48 AM

Post #6 of 8 (309 views)
Permalink
Re: conditional running of code portion [In reply to]

I just googled the OP's question and found a StackOverflow question.
That question's solution mentions pypreprocessor.

On 6 August 2012 08:20, Steven W. Orr <steveo [at] syslang> wrote:

> On 8/5/2012 12:43 AM, Ramchandra Apte wrote:
>
>> Try pypreprocessor <http://code.google.com/p/**pypreprocessor/<http://code.google.com/p/pypreprocessor/>>
>> .
>> Better idea:
>> You should be using the logging <http://docs.python.org/**
>> library/logging.html <http://docs.python.org/library/logging.html>>
>>
>> module if you want to print debug information quickly.It uses threads and
>> is
>> optimized to run fast.
>>
>
> I never saw pypreprocessor. Looks interesting. I have experience with
> Ned's cog preprocessor.
>
> http://nedbatchelder.com/code/**cog/ <http://nedbatchelder.com/code/cog/>
>
> I used this in something that was operating at the packet socket level. I
> had no time to test if debug was true.
>
>
>
> --
> 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
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>


dieter at handshake

Aug 6, 2012, 10:02 AM

Post #7 of 8 (307 views)
Permalink
Re: conditional running of code portion [In reply to]

Serhiy Storchaka <storchaka [at] gmail> writes:

> On 05.08.12 09:30, Steven D'Aprano wrote:
>> If you are working in a tight loop, you can do this:
>>
>> if VERBOSE_FLAG:
>> for item in loop:
>> print(DEBUG_INFORMATION)
>> do_actual_work(item)
>> else:
>> for item in loop:
>> do_actual_work(item)
>
> Or this:
>
> if VERBOSE_FLAG:
> def do_work(item):
> print(DEBUG_INFORMATION)
> do_actual_work(item)
> else:
> do_work = do_actual_work
>
> for item in loop:
> do_work(item)

Be warned: a function call is *much* more expensive than an
"if variable:".


--
http://mail.python.org/mailman/listinfo/python-list


storchaka at gmail

Aug 6, 2012, 1:59 PM

Post #8 of 8 (310 views)
Permalink
Re: conditional running of code portion [In reply to]

On 06.08.12 20:02, Dieter Maurer wrote:
> Serhiy Storchaka <storchaka [at] gmail> writes:
>> On 05.08.12 09:30, Steven D'Aprano wrote:
>>> If you are working in a tight loop, you can do this:
>>>
>>> if VERBOSE_FLAG:
>>> for item in loop:
>>> print(DEBUG_INFORMATION)
>>> do_actual_work(item)
>>> else:
>>> for item in loop:
>>> do_actual_work(item)
>>
>> Or this:
>>
>> if VERBOSE_FLAG:
>> def do_work(item):
>> print(DEBUG_INFORMATION)
>> do_actual_work(item)
>> else:
>> do_work = do_actual_work
>>
>> for item in loop:
>> do_work(item)
>
> Be warned: a function call is *much* more expensive than an
> "if variable:".

As any actual work. As iteration.

Yet one way:

def verbose_iter(it):
for i in it:
print(DEBUG_INFORMATION)
yield i
...

if VERBOSE_FLAG:
loop = verbose_iter(loop)
for item in loop:
do_work(item)


--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.