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


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

Re: Python #ifdef

Started byJoel Goldstick <joel.goldstick@gmail.com>
First post2013-05-28 15:49 -0400
Last post2013-05-28 13:14 -0700
Articles 12 — 6 participants

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Python #ifdef Joel Goldstick <joel.goldstick@gmail.com> - 2013-05-28 15:49 -0400
    Re: Python #ifdef Neil Cerutti <neilc@norwich.edu> - 2013-05-28 20:00 +0000
      RE: Python #ifdef Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-28 23:07 +0300
        Re: Python #ifdef Grant Edwards <invalid@invalid.invalid> - 2013-05-28 20:12 +0000
          RE: Python #ifdef Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-28 23:22 +0300
            Re: Python #ifdef Grant Edwards <invalid@invalid.invalid> - 2013-05-28 20:42 +0000
              RE: Python #ifdef Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-28 23:54 +0300
                Re: Python #ifdef Grant Edwards <invalid@invalid.invalid> - 2013-05-29 14:45 +0000
          RE: Python #ifdef Fábio Santos <fabiosantosart@gmail.com> - 2013-05-28 21:34 +0100
          RE: Python #ifdef Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-28 23:48 +0300
          RE: Python #ifdef Fábio Santos <fabiosantosart@gmail.com> - 2013-05-28 23:51 +0100
      RE: Python #ifdef Benjamin Kaplan <benjamin.kaplan@case.edu> - 2013-05-28 13:14 -0700

#46330 — Re: Python #ifdef

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-05-28 15:49 -0400
SubjectRe: Python #ifdef
Message-ID<mailman.2318.1369770916.3114.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

No
On May 28, 2013 3:48 PM, "Carlos Nepomuceno" <carlosnepomuceno@outlook.com>
wrote:

> Are there Python 'preprocessor directives'?
>
> I'd like to have something like '#ifdef' to mix code from Python 2 and 3
> in a single file.
>
> Is that possible? How?
> --
> http://mail.python.org/mailman/listinfo/python-list
>

[toc] | [next] | [standalone]


#46332

FromNeil Cerutti <neilc@norwich.edu>
Date2013-05-28 20:00 +0000
Message-ID<b0kgm4F3gs4U1@mid.individual.net>
In reply to#46330
On 2013-05-28, Joel Goldstick <joel.goldstick@gmail.com> wrote:
>
> No

Yes. More below.

> On May 28, 2013 3:48 PM, "Carlos Nepomuceno" <carlosnepomuceno@outlook.com>
> wrote:
>> Are there Python 'preprocessor directives'?
>>
>> I'd like to have something like '#ifdef' to mix code from Python 2 and 3
>> in a single file.
>>
>> Is that possible? How?

You need sys.version_info.

For more, see http://python3porting.com/noconv.html

-- 
Neil Cerutti

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


#46333

FromCarlos Nepomuceno <carlosnepomuceno@outlook.com>
Date2013-05-28 23:07 +0300
Message-ID<mailman.2320.1369771667.3114.python-list@python.org>
In reply to#46332
Thank you! I made it run like the following. What do you think about that? IS there a better way?



#The following runs on Python 2.7
sc3='''
# Python 3
def original(n):
    m = 0
    for b in n.to_bytes(6, 'big'):
        m = 256*m + b
    return m
'''
if sys.version_info[0] == 3:
    exec(sc3) 		 	   		  

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


#46334

FromGrant Edwards <invalid@invalid.invalid>
Date2013-05-28 20:12 +0000
Message-ID<ko334b$83c$1@reader1.panix.com>
In reply to#46333
On 2013-05-28, Carlos Nepomuceno <carlosnepomuceno@outlook.com> wrote:

> Thank you! I made it run like the following. What do you think about
> that? IS there a better way?
>
> #The following runs on Python 2.7
> sc3='''
> # Python 3
> def original(n):
> ??? m = 0
> ??? for b in n.to_bytes(6, 'big'):
> ??????? m = 256*m + b
> ??? return m
> '''
> if sys.version_info[0] == 3:
> ??? exec(sc3) 		 	   		  

You're trying to make this a lot harder than it really is:

if sys.version_info[0] == 3:
    def original(n):
        m = 0
        for b in n.to_bytes(6, 'big'):
            m = 256*m + b
        return m
else:
    def original(n):
        <something else?>
        

-- 
Grant Edwards               grant.b.edwards        Yow! Am I having fun yet?
                                  at               
                              gmail.com            

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


#46336

FromCarlos Nepomuceno <carlosnepomuceno@outlook.com>
Date2013-05-28 23:22 +0300
Message-ID<mailman.2322.1369772569.3114.python-list@python.org>
In reply to#46334
----------------------------------------
> From: invalid@invalid.invalid
> Subject: Re: Python #ifdef
[...]
> You're trying to make this a lot harder than it really is:
>
> if sys.version_info[0] == 3:
> def original(n):
> m = 0
> for b in n.to_bytes(6, 'big'):
> m = 256*m + b
> return m
> else:
> def original(n):
> <something else?>
>
>
> --
> Grant Edwards grant.b.edwards Yow! Am I having fun yet?
> at
> gmail.com

Haha! That's it!!!

Just realized how funny this can be: ;)

### never to be opened ###
def pandoras_box(v):
    return v/0.0

if customer_didnt_pay():
    pandoras_box()

#lol 		 	   		  

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


#46339

FromGrant Edwards <invalid@invalid.invalid>
Date2013-05-28 20:42 +0000
Message-ID<ko34rq$m2s$1@reader1.panix.com>
In reply to#46336
On 2013-05-28, Carlos Nepomuceno <carlosnepomuceno@outlook.com> wrote:

> [...]
>> You're trying to make this a lot harder than it really is:
>>
>> if sys.version_info[0] == 3:
>> def original(n):
>> m = 0
>> for b in n.to_bytes(6, 'big'):
>> m = 256*m + b
>> return m
>> else:
>> def original(n):
>> <something else?>
>
> Haha! That's it!!!
>
> Just realized how funny this can be: ;)

Here's the important lesson from this thread:

Instead of asking "how do I write X in Python" where yoy've assumed X
is the solution to your problem, you're usually better off asking how
to solve the underlying problem in a Pythonic way.

IOW, instead of asking about a Python preprocessor (which you have
assumed is the solution to the problem because that's how you would do
it in C), ask about the actual problem (how to define a function
differently depending on Python version).

People on this list are very inventive and will expend a surprising
amount of time to figure out often too-clever ways to do X because you
asked how to do X -- even if doing X is a lousy way to solve your
actual problem...

When asking how do I solve a problem, it's OK to illustrate that
question with an example X of how you would solve it in C or Java or
Ruby or Perl or whatever, but remember

  1) Not everybody here knows C or Java or Ruby or Perl or whatever,
     and the person who _does_ know everyting there is to know about
     solving some particular underlying problem isn't going to go
     learn a new language so that they can understand your example and
     figure out what you're trying to accomplish.

  2) Programming languages differ. X may be the best way to solve the
     problem in one language, but it might be an awful way to do it in
     another language.

-- 
Grant Edwards               grant.b.edwards        Yow! I'm ANN LANDERS!!
                                  at               I can SHOPLIFT!!
                              gmail.com            

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


#46341

FromCarlos Nepomuceno <carlosnepomuceno@outlook.com>
Date2013-05-28 23:54 +0300
Message-ID<mailman.2326.1369774488.3114.python-list@python.org>
In reply to#46339
----------------------------------------
> From: invalid@invalid.invalid
> Subject: Re: Python #ifdef
> Date: Tue, 28 May 2013 20:42:34 +0000
> To: python-list@python.org
[...]
> Here's the important lesson from this thread:
>
> Instead of asking "how do I write X in Python" where yoy've assumed X
> is the solution to your problem, you're usually better off asking how
> to solve the underlying problem in a Pythonic way.
>
> IOW, instead of asking about a Python preprocessor (which you have
> assumed is the solution to the problem because that's how you would do
> it in C), ask about the actual problem (how to define a function
> differently depending on Python version).
>
> People on this list are very inventive and will expend a surprising
> amount of time to figure out often too-clever ways to do X because you
> asked how to do X -- even if doing X is a lousy way to solve your
> actual problem...
>
> When asking how do I solve a problem, it's OK to illustrate that
> question with an example X of how you would solve it in C or Java or
> Ruby or Perl or whatever, but remember
>
> 1) Not everybody here knows C or Java or Ruby or Perl or whatever,
> and the person who _does_ know everyting there is to know about
> solving some particular underlying problem isn't going to go
> learn a new language so that they can understand your example and
> figure out what you're trying to accomplish.
>
> 2) Programming languages differ. X may be the best way to solve the
> problem in one language, but it might be an awful way to do it in
> another language.
>
> --
> Grant Edwards grant.b.edwards Yow! I'm ANN LANDERS!!
> at I can SHOPLIFT!!
> gmail.com


You're right! Sometimes I hate myself for doing exactly the opposite of what I would like to do!

Unfortunately I can't change the thread subject.

How do you have "invalid@invalid.invalid" instead of your email address? 		 	   		  

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


#46386

FromGrant Edwards <invalid@invalid.invalid>
Date2013-05-29 14:45 +0000
Message-ID<ko54ah$c5b$1@reader1.panix.com>
In reply to#46341
On 2013-05-28, Carlos Nepomuceno <carlosnepomuceno@outlook.com> wrote:

> How do you have "invalid@invalid.invalid" instead of your email address?

I have this in my .slrnrc:

 set hostname "invalid.invalid"
 set username "grant"
 set realname "Grant Edwards"

I'm not sure why it doesn't show up as grant@invalid.invalid -- I
think it used to.

Since I'm this as a Usenet news group using the slrn newsreader
program, there's no need to make my email address visible in the
message headers in a usable form.  I originally did this long ago
(probably 20 years) to avoid spam.  I'm pretty sure that it's all in
vain these days, and the spam filters at Google are what's keeping me
from drowning.

-- 
Grant Edwards               grant.b.edwards        Yow! Gee, I feel kind of
                                  at               LIGHT in the head now,
                              gmail.com            knowing I can't make my
                                                   satellite dish PAYMENTS!

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


#46338

FromFábio Santos <fabiosantosart@gmail.com>
Date2013-05-28 21:34 +0100
Message-ID<mailman.2324.1369773285.3114.python-list@python.org>
In reply to#46334

[Multipart message — attachments visible in raw view] — view raw

On 28 May 2013 21:26, "Carlos Nepomuceno" <carlosnepomuceno@outlook.com>
wrote:
> Haha! That's it!!!
>
> Just realized how funny this can be: ;)
>
> ### never to be opened ###
> def pandoras_box(v):
>     return v/0.0
>
> if customer_didnt_pay():
>     pandoras_box()
>
> #lol

1/0 is, after print, my most common debug statement.

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


#46340

FromCarlos Nepomuceno <carlosnepomuceno@outlook.com>
Date2013-05-28 23:48 +0300
Message-ID<mailman.2325.1369774122.3114.python-list@python.org>
In reply to#46334
________________________________
> Date: Tue, 28 May 2013 21:34:36 +0100 
> Subject: RE: Python #ifdef 
> From: fabiosantosart@gmail.com 
> To: carlosnepomuceno@outlook.com 
> CC: python-list@python.org 
>  
>  
> On 28 May 2013 21:26, "Carlos Nepomuceno"  
> <carlosnepomuceno@outlook.com<mailto:carlosnepomuceno@outlook.com>>  
> wrote: 
> > Haha! That's it!!! 
> > 
> > Just realized how funny this can be: ;) 
> > 
> > ### never to be opened ### 
> > def pandoras_box(v): 
> >     return v/0.0 
> > 
> > if customer_didnt_pay(): 
> >     pandoras_box() 
> > 
> > #lol 
>  
> 1/0 is, after print, my most common debug statement.

What's the best debugger for Python? Have you tried HAP[1]?

[1] http://hapdebugger.sourceforge.net/ 		 	   		  

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


#46347

FromFábio Santos <fabiosantosart@gmail.com>
Date2013-05-28 23:51 +0100
Message-ID<mailman.2332.1369781472.3114.python-list@python.org>
In reply to#46334

[Multipart message — attachments visible in raw view] — view raw

On 28 May 2013 21:53, "Carlos Nepomuceno" <carlosnepomuceno@outlook.com>
wrote:
>
> ________________________________
> > Date: Tue, 28 May 2013 21:34:36 +0100
> > Subject: RE: Python #ifdef
> > From: fabiosantosart@gmail.com
> > To: carlosnepomuceno@outlook.com
> > CC: python-list@python.org
> >
> >
> > On 28 May 2013 21:26, "Carlos Nepomuceno"
> > <carlosnepomuceno@outlook.com<mailto:carlosnepomuceno@outlook.com>>
> > wrote:
> > > Haha! That's it!!!
> > >
> > > Just realized how funny this can be: ;)
> > >
> > > ### never to be opened ###
> > > def pandoras_box(v):
> > >     return v/0.0
> > >
> > > if customer_didnt_pay():
> > >     pandoras_box()
> > >
> > > #lol
> >
> > 1/0 is, after print, my most common debug statement.
>
> What's the best debugger for Python? Have you tried HAP[1]?
>
> [1] http://hapdebugger.sourceforge.net/

Never saw that, but the remote debugging looks like it adds some
flexibility. That said, I don't often use a debugger. When I do, it's pdb.
Pdb is not bad at all, and it comes in the stdlib, which makes it readily
available in a virtualenv. It's impractical to set more than a breakpoint,
but then again I only use a breakpoint at a time.

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


#46335

FromBenjamin Kaplan <benjamin.kaplan@case.edu>
Date2013-05-28 13:14 -0700
Message-ID<mailman.2321.1369772484.3114.python-list@python.org>
In reply to#46332

[Multipart message — attachments visible in raw view] — view raw

On May 28, 2013 1:10 PM, "Carlos Nepomuceno" <carlosnepomuceno@outlook.com>
wrote:
>
> Thank you! I made it run like the following. What do you think about
that? IS there a better way?
>
>
>
> #The following runs on Python 2.7
> sc3='''
> # Python 3
> def original(n):
>     m = 0
>     for b in n.to_bytes(6, 'big'):
>         m = 256*m + b
>     return m
> '''
> if sys.version_info[0] == 3:
>     exec(sc3)
> --

No need for exec.

if sys.version_info[0] >= 3:
    def original(n) :
       ...

[toc] | [prev] | [standalone]


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


csiph-web