Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #59493 > unrolled thread

Implementing #define macros similar to C on python

Started byJL <lightaiyee@gmail.com>
First post2013-11-14 18:29 -0800
Last post2013-11-16 00:20 +0100
Articles 13 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  Implementing #define macros similar to C on python JL <lightaiyee@gmail.com> - 2013-11-14 18:29 -0800
    Re: Implementing #define macros similar to C on python Dave Angel <davea@davea.name> - 2013-11-14 21:30 -0600
    Re: Implementing #define macros similar to C on python Chris Angelico <rosuav@gmail.com> - 2013-11-15 14:49 +1100
      Re: Implementing #define macros similar to C on python JL <lightaiyee@gmail.com> - 2013-11-15 15:36 -0800
        Re: Implementing #define macros similar to C on python Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2013-11-16 01:20 +0100
        Re: Implementing #define macros similar to C on python Terry Reedy <tjreedy@udel.edu> - 2013-11-15 19:22 -0500
        Re: Implementing #define macros similar to C on python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-16 00:22 +0000
          Re: Implementing #define macros similar to C on python JL <lightaiyee@gmail.com> - 2013-11-15 21:38 -0800
            Re: Implementing #define macros similar to C on python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-16 11:48 +0000
    Re: Implementing #define macros similar to C on python Roy Smith <roy@panix.com> - 2013-11-14 23:10 -0500
      Re: Implementing #define macros similar to C on python Chris Angelico <rosuav@gmail.com> - 2013-11-15 15:57 +1100
      Re: Implementing #define macros similar to C on python Serhiy Storchaka <storchaka@gmail.com> - 2013-11-16 12:02 +0200
    Re: Implementing #define macros similar to C on python Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2013-11-16 00:20 +0100

#59493 — Implementing #define macros similar to C on python

FromJL <lightaiyee@gmail.com>
Date2013-11-14 18:29 -0800
SubjectImplementing #define macros similar to C on python
Message-ID<fae7479b-ecec-4114-9750-6595fa8c78fa@googlegroups.com>
One of my favorite tools in C/C++ language is the preprocessor macros.

One example is switching certain print messages for debugging use only

#ifdef DEBUG_ENABLE
DEBUG_PRINT   print
#else
DEBUG_PRINT

Is it possible to implement something similar in python? Thank you.

[toc] | [next] | [standalone]


#59496

FromDave Angel <davea@davea.name>
Date2013-11-14 21:30 -0600
Message-ID<mailman.2635.1384486506.18130.python-list@python.org>
In reply to#59493
On Thu, 14 Nov 2013 18:29:48 -0800 (PST), JL <lightaiyee@gmail.com> 
wrote:
> One of my favorite tools in C/C++ language is the preprocessor 
macros.


> One example is switching certain print messages for debugging use 
only


> #ifdef DEBUG_ENABLE
> DEBUG_PRINT   print
> #else
> DEBUG_PRINT


> Is it possible to implement something similar in python? Thank you.

Sure. A preprocessor can be written for nearly every language. Are 
you offering?

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#59497

FromChris Angelico <rosuav@gmail.com>
Date2013-11-15 14:49 +1100
Message-ID<mailman.2636.1384487396.18130.python-list@python.org>
In reply to#59493
On Fri, Nov 15, 2013 at 1:29 PM, JL <lightaiyee@gmail.com> wrote:
> One of my favorite tools in C/C++ language is the preprocessor macros.
>
> One example is switching certain print messages for debugging use only
>
> #ifdef DEBUG_ENABLE
> DEBUG_PRINT   print
> #else
> DEBUG_PRINT
>
> Is it possible to implement something similar in python? Thank you.

There are usually other ways to do things. For instance, you can
define a function to either do something or do nothing:

if debug_mode:
    debug_print = print
else:
    debug_print = lambda: None

debug_print("This won't be shown unless we're in debug mode!")

But as Dave says, you could write a preprocessor if you need one.

ChrisA

[toc] | [prev] | [next] | [standalone]


#59572

FromJL <lightaiyee@gmail.com>
Date2013-11-15 15:36 -0800
Message-ID<69e7aee9-ad78-412e-9e1c-84b5a855acc9@googlegroups.com>
In reply to#59497
Thanks! This is the answer which I am seeking. However, I am not able to get the following line to work. I am using python 2.7.5

debug_print = print

Can we assign a function into a variable in this manner?

On Friday, November 15, 2013 11:49:52 AM UTC+8, Chris Angelico wrote:
> On Fri, Nov 15, 2013 at 1:29 PM, JL <lightaiyee@gmail.com> wrote:
> 
> > One of my favorite tools in C/C++ language is the preprocessor macros.
> 
> >
> 
> > One example is switching certain print messages for debugging use only
> 
> >
> 
> > #ifdef DEBUG_ENABLE
> 
> > DEBUG_PRINT   print
> 
> > #else
> 
> > DEBUG_PRINT
> 
> >
> 
> > Is it possible to implement something similar in python? Thank you.
> 
> 
> 
> There are usually other ways to do things. For instance, you can
> 
> define a function to either do something or do nothing:
> 
> 
> 
> if debug_mode:
> 
>     debug_print = print
> 
> else:
> 
>     debug_print = lambda: None
> 
> 
> 
> debug_print("This won't be shown unless we're in debug mode!")
> 
> 
> 
> But as Dave says, you could write a preprocessor if you need one.
> 
> 
> 
> ChrisA

[toc] | [prev] | [next] | [standalone]


#59576

FromIrmen de Jong <irmen.NOSPAM@xs4all.nl>
Date2013-11-16 01:20 +0100
Message-ID<5286ba48$0$16001$e4fe514c@news.xs4all.nl>
In reply to#59572
On 16-11-2013 0:36, JL wrote:
> Thanks! This is the answer which I am seeking. However, I am not able to get the following line to work. I am using python 2.7.5
> 
> debug_print = print
> 
> Can we assign a function into a variable in this manner?

Yes, functions are just another object. But 'print' is only a function as of Python 3.
For your version, try adding this as the first line:
from __future__ import print_function

Irmen

[toc] | [prev] | [next] | [standalone]


#59577

FromTerry Reedy <tjreedy@udel.edu>
Date2013-11-15 19:22 -0500
Message-ID<mailman.2692.1384561344.18130.python-list@python.org>
In reply to#59572
On 11/15/2013 6:36 PM, JL wrote:
> Thanks! This is the answer which I am seeking. However, I am not able to get the following line to work. I am using python 2.7.5
>
> debug_print = print

Start your file with
from __future__ import print_function
and the above should work.

Oh, and please snip stuff not relevant to your post.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#59578

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-11-16 00:22 +0000
Message-ID<mailman.2693.1384561357.18130.python-list@python.org>
In reply to#59572
On 15/11/2013 23:36, JL wrote:
> Thanks! This is the answer which I am seeking. However, I am not able to get the following line to work. I am using python 2.7.5
>
> debug_print = print
>
> Can we assign a function into a variable in this manner?
>
> On Friday, November 15, 2013 11:49:52 AM UTC+8, Chris Angelico wrote:
>> On Fri, Nov 15, 2013 at 1:29 PM, JL <lightaiyee@gmail.com> wrote:
>>
>>> One of my favorite tools in C/C++ language is the preprocessor macros.
>>
>>>
>>
>>> One example is switching certain print messages for debugging use only
>>
>>>
>>
>>> #ifdef DEBUG_ENABLE
>>
>>> DEBUG_PRINT   print
>>
>>> #else
>>
>>> DEBUG_PRINT
>>
>>>
>>
>>> Is it possible to implement something similar in python? Thank you.
>>
>>
>>
>> There are usually other ways to do things. For instance, you can
>>
>> define a function to either do something or do nothing:
>>
>>
>>
>> if debug_mode:
>>
>>      debug_print = print
>>
>> else:
>>
>>      debug_print = lambda: None
>>
>>
>>
>> debug_print("This won't be shown unless we're in debug mode!")
>>
>>
>>
>> But as Dave says, you could write a preprocessor if you need one.
>>
>>
>>
>> ChrisA

Yes but please don't top post.  Actually print is a statement in Python 
2 so your code should work if you use

from __future__ import print_function

at the top of your code.

Would you also be kind enough to read and action this 
https://wiki.python.org/moin/GoogleGroupsPython to prevent the double 
line spacing shown above, thanks.

-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#59592

FromJL <lightaiyee@gmail.com>
Date2013-11-15 21:38 -0800
Message-ID<38b1fadd-bd6a-4722-8a0c-5c10586a718f@googlegroups.com>
In reply to#59578
On Saturday, November 16, 2013 8:22:25 AM UTC+8, Mark Lawrence wrote:

> Yes but please don't top post.  Actually print is a statement in Python 
> 2 so your code should work if you use
> from __future__ import print_function
> at the top of your code.
> Would you also be kind enough to read and action this 
> https://wiki.python.org/moin/GoogleGroupsPython to prevent the double 
> line spacing shown above, thanks.

Thank you for the tip. Will try that out. Hope I get the posting etiquette right this time.

[toc] | [prev] | [next] | [standalone]


#59601

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-11-16 11:48 +0000
Message-ID<mailman.2711.1384602533.18130.python-list@python.org>
In reply to#59592
On 16/11/2013 05:38, JL wrote:
> On Saturday, November 16, 2013 8:22:25 AM UTC+8, Mark Lawrence wrote:
>
>> Yes but please don't top post.  Actually print is a statement in Python
>> 2 so your code should work if you use
>> from __future__ import print_function
>> at the top of your code.
>> Would you also be kind enough to read and action this
>> https://wiki.python.org/moin/GoogleGroupsPython to prevent the double
>> line spacing shown above, thanks.
>
> Thank you for the tip. Will try that out. Hope I get the posting etiquette right this time.
>

No problem.  It's not a matter of etiquette, it's using a tool that's 
not flawed :)

-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#59498

FromRoy Smith <roy@panix.com>
Date2013-11-14 23:10 -0500
Message-ID<roy-2E4CE0.23100314112013@news.panix.com>
In reply to#59493
In article <fae7479b-ecec-4114-9750-6595fa8c78fa@googlegroups.com>,
 JL <lightaiyee@gmail.com> wrote:

> One of my favorite tools in C/C++ language is the preprocessor macros.
> 
> One example is switching certain print messages for debugging use only
> 
> #ifdef DEBUG_ENABLE
> DEBUG_PRINT   print
> #else
> DEBUG_PRINT
> 
> Is it possible to implement something similar in python? Thank you.

Why would you want to?  One of the most horrible things about C/C++ is 
the preprocessor.  Python has much better mechanisms to implement just 
about anything you would do with the preprocessor.

For the example you gave, you would log things as info() or debug(), and 
then adjust the filter level in the logger.

[toc] | [prev] | [next] | [standalone]


#59500

FromChris Angelico <rosuav@gmail.com>
Date2013-11-15 15:57 +1100
Message-ID<mailman.2638.1384491435.18130.python-list@python.org>
In reply to#59498
On Fri, Nov 15, 2013 at 3:10 PM, Roy Smith <roy@panix.com> wrote:
> Why would you want to?  One of the most horrible things about C/C++ is
> the preprocessor.

Hey, that's not fair! Without the preprocessor, how would you be able
to do this:

//Hide this part away in a header file somewhere
struct b0rkb0rk
{
    float value;
    b0rkb0rk(float v):value(v) {}
    operator float() {return value;}
    float operator +(float other) {return value+other-0.1;}
};
//Behold the power of the preprocessor!
#define float b0rkb0rk

//Okay, now here's your application
#include <iostream>

int main()
{
    std::cout << "Look how stupidly inaccurate float is!\n";
    float x = 123.0f;
    std::cout << "123.0 + 2.0 = " << x + 2.0f << "\n";
    std::cout << "See? You should totally use double instead.\n";
}

(Anybody got a cheek de-tonguer handy? I think it's stuck.)

ChrisA

[toc] | [prev] | [next] | [standalone]


#59596

FromSerhiy Storchaka <storchaka@gmail.com>
Date2013-11-16 12:02 +0200
Message-ID<mailman.2705.1384596164.18130.python-list@python.org>
In reply to#59498
15.11.13 06:57, Chris Angelico написав(ла):
> On Fri, Nov 15, 2013 at 3:10 PM, Roy Smith <roy@panix.com> wrote:
>> Why would you want to?  One of the most horrible things about C/C++ is
>> the preprocessor.
>
> Hey, that's not fair! Without the preprocessor, how would you be able
> to do this:
>
> //Hide this part away in a header file somewhere
> struct b0rkb0rk
> {
>      float value;
>      b0rkb0rk(float v):value(v) {}
>      operator float() {return value;}
>      float operator +(float other) {return value+other-0.1;}
> };
> //Behold the power of the preprocessor!
> #define float b0rkb0rk
>
> //Okay, now here's your application
> #include <iostream>
>
> int main()
> {
>      std::cout << "Look how stupidly inaccurate float is!\n";
>      float x = 123.0f;
>      std::cout << "123.0 + 2.0 = " << x + 2.0f << "\n";
>      std::cout << "See? You should totally use double instead.\n";
> }
>
> (Anybody got a cheek de-tonguer handy? I think it's stuck.)

 >>> class b0rkb0rk(float):
...     def __add__(self, other):
...         return super().__add__(other) - 0.1
...
 >>> import builtins
 >>> builtins.float = b0rkb0rk
 >>> float(123) + 2
124.9

[toc] | [prev] | [next] | [standalone]


#59570

FromIrmen de Jong <irmen.NOSPAM@xs4all.nl>
Date2013-11-16 00:20 +0100
Message-ID<5286ac50$0$15880$e4fe514c@news.xs4all.nl>
In reply to#59493
On 15-11-2013 3:29, JL wrote:
> One of my favorite tools in C/C++ language is the preprocessor macros.
> 
> One example is switching certain print messages for debugging use only
> 
> #ifdef DEBUG_ENABLE
> DEBUG_PRINT   print
> #else
> DEBUG_PRINT
> 
> Is it possible to implement something similar in python? Thank you.
> 

You could just run cpp (or gcc -E) on your python-with-macros-file to generate the final
.py file. But: yuck, eww, gross.

Irmen

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web