RE: Python #ifdef

2013-06-12 Thread Carlos Nepomuceno

 Date: Mon, 3 Jun 2013 12:05:49 +1000
 Subject: Re: Python #ifdef
 From: ros...@gmail.com
 To: python-list@python.org
[...]
 Ah. I actually wasn't aware of m4's use with sendmail. I first met it
 as the aforementioned PHP preprocessor, simply by Googling for
 something along the lines of generic preprocessor. First hit solved
 my problems.

 ChrisA

Why didn't you use something like EZT[1]?

[1] https://code.google.com/p/ezt/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python #ifdef

2013-06-12 Thread Chris Angelico
On Tue, Jun 4, 2013 at 8:18 AM, Carlos Nepomuceno
carlosnepomuc...@outlook.com wrote:
 
 Date: Mon, 3 Jun 2013 12:05:49 +1000
 Subject: Re: Python #ifdef
 From: ros...@gmail.com
 To: python-list@python.org
 [...]
 Ah. I actually wasn't aware of m4's use with sendmail. I first met it
 as the aforementioned PHP preprocessor, simply by Googling for
 something along the lines of generic preprocessor. First hit solved
 my problems.

 ChrisA

 Why didn't you use something like EZT[1]?

 [1] https://code.google.com/p/ezt/

I'm a bit behind getting to this; I think some python-list posts got
stuck somewhere (maybe in the news-list gateway?). Anyway: The reason
I didn't was simply that m4 came up first. I was familiar with the C
preprocessor and PPWizard (which is designed for HTML), and simply
went looking for a generic one. And yes, I had seriously contemplated
using the C preprocessor (from gcc), but there was a reason not to
(don't remember now, but I'm guessing it'll be the conflict between
hash comments in PHP and hash directives to cpp).

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


Re: Python #ifdef

2013-06-02 Thread Erik Max Francis

On 05/29/2013 08:05 AM, Chris Angelico wrote:

It's not a bad tool. I used it as a sort of PHP preprocessor, because
requirements at work had me wanting to have a source file defining a
PHP class and having an autogenerated section in the middle of that
class. PHP's 'include' directive doesn't work for that. Of course, had
we been using a better language, that wouldn't have been an issue (and
it stopped being an issue when we improved the design and stopped
using that class system, too, though I retained the makefile
directives about building .php.m4 - .php files). But still, GNU M4 is
a decent piece of technology.


Agreed.  The terror that most people feel when hearing m4 is because 
m4 was associated with sendmail, not because m4 was inherently awful. 
It has problems, but you'd only encounter them when doing something 
_very_ abstract.


--
Erik Max Francis  m...@alcyone.com  http://www.alcyone.com/max/
 San Jose, CA, USA  37 18 N 121 57 W  AIM/Y!M/Jabber erikmaxfrancis
  Substance is one of the greatest of our illusions.
   -- Sir Arthur Eddington
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python #ifdef

2013-06-02 Thread Roy Smith
In article n4mdnzkiorrtrjbmnz2dnuvz5godn...@giganews.com,
 Erik Max Francis m...@alcyone.com wrote:

 On 05/29/2013 08:05 AM, Chris Angelico wrote:
  It's not a bad tool. I used it as a sort of PHP preprocessor, because
  requirements at work had me wanting to have a source file defining a
  PHP class and having an autogenerated section in the middle of that
  class. PHP's 'include' directive doesn't work for that. Of course, had
  we been using a better language, that wouldn't have been an issue (and
  it stopped being an issue when we improved the design and stopped
  using that class system, too, though I retained the makefile
  directives about building .php.m4 - .php files). But still, GNU M4 is
  a decent piece of technology.
 
 Agreed.  The terror that most people feel when hearing m4 is because 
 m4 was associated with sendmail

Aggghhh.  You had to go and dredge up those bad memories?  I hadn't 
thought about sendmail config files in years.  I'll probably never be 
able to get to sleep tonight.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python #ifdef

2013-06-02 Thread Skip Montanaro
 The terror that most people feel when hearing m4 is because m4 was
associated with sendmail, not because m4 was inherently awful.

In fact, m4 made sendmail configs easier to maintain.

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


Re: Python #ifdef

2013-06-02 Thread Chris Angelico
On Mon, Jun 3, 2013 at 9:57 AM, Erik Max Francis m...@alcyone.com wrote:
 On 05/29/2013 08:05 AM, Chris Angelico wrote:

 It's not a bad tool. I used it as a sort of PHP preprocessor, because
 requirements at work had me wanting to have a source file defining a
 PHP class and having an autogenerated section in the middle of that
 class. PHP's 'include' directive doesn't work for that. Of course, had
 we been using a better language, that wouldn't have been an issue (and
 it stopped being an issue when we improved the design and stopped
 using that class system, too, though I retained the makefile
 directives about building .php.m4 - .php files). But still, GNU M4 is

 a decent piece of technology.


 Agreed.  The terror that most people feel when hearing m4 is because m4
 was associated with sendmail, not because m4 was inherently awful. It has
 problems, but you'd only encounter them when doing something _very_
 abstract.

Ah. I actually wasn't aware of m4's use with sendmail. I first met it
as the aforementioned PHP preprocessor, simply by Googling for
something along the lines of generic preprocessor. First hit solved
my problems.

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


Re: Python #ifdef

2013-05-29 Thread Grant Edwards
On 2013-05-28, Carlos Nepomuceno carlosnepomuc...@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.edwardsYow! Gee, I feel kind of
  at   LIGHT in the head now,
  gmail.comknowing I can't make my
   satellite dish PAYMENTS!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python #ifdef

2013-05-29 Thread Grant Edwards
On 2013-05-29, Dan Stromberg drsali...@gmail.com wrote:

 And in case you still want a preprocessor for Python (you likely don't need
 one this time), here's an example of doing this using the venerable m4:
 https://pypi.python.org/pypi/red-black-tree-mod .  Note the many comments
 added to keep line numbers consistent.

I was wondering whether or not to mention m4.  Since m4 is (in my
mind) inextricably linked to RATFOR and sendmail config files I try
to avoid thinking about it lest the flashbacks start again...

-- 
Grant Edwards   grant.b.edwardsYow! Hmmm ... a CRIPPLED
  at   ACCOUNTANT with a FALAFEL
  gmail.comsandwich is HIT by a
   TROLLEY-CAR ...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python #ifdef

2013-05-29 Thread Chris Angelico
On Thu, May 30, 2013 at 12:55 AM, Grant Edwards invalid@invalid.invalid wrote:
 On 2013-05-29, Dan Stromberg drsali...@gmail.com wrote:

 And in case you still want a preprocessor for Python (you likely don't need
 one this time), here's an example of doing this using the venerable m4:
 https://pypi.python.org/pypi/red-black-tree-mod .  Note the many comments
 added to keep line numbers consistent.

 I was wondering whether or not to mention m4.  Since m4 is (in my
 mind) inextricably linked to RATFOR and sendmail config files I try
 to avoid thinking about it lest the flashbacks start again...

It's not a bad tool. I used it as a sort of PHP preprocessor, because
requirements at work had me wanting to have a source file defining a
PHP class and having an autogenerated section in the middle of that
class. PHP's 'include' directive doesn't work for that. Of course, had
we been using a better language, that wouldn't have been an issue (and
it stopped being an issue when we improved the design and stopped
using that class system, too, though I retained the makefile
directives about building .php.m4 - .php files). But still, GNU M4 is
a decent piece of technology.

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


Re: Python #ifdef

2013-05-29 Thread Grant Edwards
On 2013-05-29, Chris Angelico ros...@gmail.com wrote:
 On Thu, May 30, 2013 at 12:55 AM, Grant Edwards invalid@invalid.invalid 
 wrote:
 On 2013-05-29, Dan Stromberg drsali...@gmail.com wrote:

 And in case you still want a preprocessor for Python (you likely don't need
 one this time), here's an example of doing this using the venerable m4:
 https://pypi.python.org/pypi/red-black-tree-mod .  Note the many comments
 added to keep line numbers consistent.

 I was wondering whether or not to mention m4.  Since m4 is (in my
 mind) inextricably linked to RATFOR and sendmail config files I try
 to avoid thinking about it lest the flashbacks start again...

 It's not a bad tool. I used it as a sort of PHP preprocessor, 
 [...]
 But still, GNU M4 is a decent piece of technology.

I didn't mean to disparage m4 -- it always seemed well thought out and
useful.  It just happens to be associated with unpleasant things for
me.  [And it didn't really seem like a good option for the OP's
problem.]

Still, if one really did want a preprocessor for Python programs, m4
would be the first thing I'd look at.

-- 
Grant Edwards   grant.b.edwardsYow! I wonder if I ought
  at   to tell them about my
  gmail.comPREVIOUS LIFE as a COMPLETE
   STRANGER?
-- 
http://mail.python.org/mailman/listinfo/python-list


Python #ifdef

2013-05-28 Thread Carlos Nepomuceno
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


Re: Python #ifdef

2013-05-28 Thread Joel Goldstick
No
On May 28, 2013 3:48 PM, Carlos Nepomuceno carlosnepomuc...@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

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


Re: Python #ifdef

2013-05-28 Thread Dave Angel

On 05/28/2013 03:46 PM, Carlos Nepomuceno wrote:

Are there Python 'preprocessor directives'?


Python doesn't define a preprocessor, and CPYthon doesn't implement one. 
 Nothing to stop you from doing so, however.


I'd like to have something like '#ifdef' to mix code from Python 2 and 3 in a 
single file.

Is that possible? How?  



It's quite possible that you don't need a preprocessor do what you want, 
since Python does much less compile-time checking than C.  Also, it's 
possible to store function objects and module objects in variables, and 
thus to hide many things from the body of the code.


One biggie is print, since that's a reserved word (and a statement) in 
Python 2.x.  But if you're using 2.7 you can use from __future__ import 
print, or something like that, and just use 3.x function semantics.



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


Re: Python #ifdef

2013-05-28 Thread Neil Cerutti
On 2013-05-28, Joel Goldstick joel.goldst...@gmail.com wrote:

 No

Yes. More below.

 On May 28, 2013 3:48 PM, Carlos Nepomuceno carlosnepomuc...@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
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Python #ifdef

2013-05-28 Thread Carlos Nepomuceno
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) 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python #ifdef

2013-05-28 Thread Grant Edwards
On 2013-05-28, Carlos Nepomuceno carlosnepomuc...@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.edwardsYow! Am I having fun yet?
  at   
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Python #ifdef

2013-05-28 Thread Benjamin Kaplan
On May 28, 2013 1:10 PM, Carlos Nepomuceno carlosnepomuc...@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) :
   ...
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Python #ifdef

2013-05-28 Thread Carlos Nepomuceno

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


RE: Python #ifdef

2013-05-28 Thread Fábio Santos
On 28 May 2013 21:26, Carlos Nepomuceno carlosnepomuc...@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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python #ifdef

2013-05-28 Thread Grant Edwards
On 2013-05-28, Carlos Nepomuceno carlosnepomuc...@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.edwardsYow! I'm ANN LANDERS!!
  at   I can SHOPLIFT!!
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Python #ifdef

2013-05-28 Thread Carlos Nepomuceno

 Date: Tue, 28 May 2013 21:34:36 +0100 
 Subject: RE: Python #ifdef 
 From: fabiosantos...@gmail.com 
 To: carlosnepomuc...@outlook.com 
 CC: python-list@python.org 
  
  
 On 28 May 2013 21:26, Carlos Nepomuceno  
 carlosnepomuc...@outlook.commailto:carlosnepomuc...@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/ 
  
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Python #ifdef

2013-05-28 Thread Carlos Nepomuceno

 From: invalid@invalid.invalid
 Subject: Re: Python #ifdef
 Date: Tue, 28 May 2013 20:42:34 +
 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?
  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python #ifdef

2013-05-28 Thread Mark Lawrence

On 28/05/2013 20:46, Carlos Nepomuceno 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?  



https://pypi.python.org/pypi/six/1.3.0

--
If you're using GoogleCrap™ please read this 
http://wiki.python.org/moin/GoogleGroupsPython.


Mark Lawrence

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


Re: Python #ifdef

2013-05-28 Thread Joel Goldstick
On Tue, May 28, 2013 at 6:18 PM, Mark Lawrence breamore...@yahoo.co.ukwrote:

 On 28/05/2013 20:46, Carlos Nepomuceno 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?


 https://pypi.python.org/pypi/**six/1.3.0https://pypi.python.org/pypi/six/1.3.0

 --
 If you're using GoogleCrap™ please read this http://wiki.python.org/moin/*
 *GoogleGroupsPython http://wiki.python.org/moin/GoogleGroupsPython.

 Mark Lawrence

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


my original response was from cell phone.  I just answered that you can't
do ifdefs, implying that there is no preprocessor in python.  I learned a
lot of things I didn't know reading the thread, but I wonder if it is a
good idea in general to try to write code like this.  -- combined 2.x/3.x
codebase can be a bear to maintain.  I wouldn't do it unless there was some
imposing reason that I must.  Its not just print() -- that isn't bad, but
changes in module names (urllib), arithmetic, and unicode especially make
this idea in general, very tricky.  Pity the next developer who needs to
try to maintain it.

So, maybe you CAN do it, but SHOULD you want to do it?

-- 
Joel Goldstick
http://joelgoldstick.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Python #ifdef

2013-05-28 Thread Carlos Nepomuceno

 Date: Tue, 28 May 2013 18:25:59 -0400 
 Subject: Re: Python #ifdef 
 From: joel.goldst...@gmail.com 
 To: breamore...@yahoo.co.uk 
 CC: python-list@python.org 
[...]
  
 my original response was from cell phone.  I just answered that you  
 can't do ifdefs, implying that there is no preprocessor in python.  I  
 learned a lot of things I didn't know reading the thread, but I wonder  
 if it is a good idea in general to try to write code like this.  --  
 combined 2.x/3.x codebase can be a bear to maintain.  I wouldn't do it  
 unless there was some imposing reason that I must.  Its not just  
 print() -- that isn't bad, but changes in module names (urllib),  
 arithmetic, and unicode especially make this idea in general, very  
 tricky.  Pity the next developer who needs to try to maintain it. 
  
 So, maybe you CAN do it, but SHOULD you want to do it? 
  
 --  
 Joel Goldstick 
 http://joelgoldstick.com 


Thanks Joel! In this case I think it does because I would like to have the same 
short benchmarking script to be runnable by Python 2 and Python 3.

The only piece of code that doesn't run on Python 2 is a to_bytes() single 
call. So it's not a huge maintenance load. ;)

I didn't try to write 'portable' code to Python 3 yet. What's the catch?
  
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Python #ifdef

2013-05-28 Thread Fábio Santos
On 28 May 2013 21:53, Carlos Nepomuceno carlosnepomuc...@outlook.com
wrote:

 
  Date: Tue, 28 May 2013 21:34:36 +0100
  Subject: RE: Python #ifdef
  From: fabiosantos...@gmail.com
  To: carlosnepomuc...@outlook.com
  CC: python-list@python.org
 
 
  On 28 May 2013 21:26, Carlos Nepomuceno
  carlosnepomuc...@outlook.commailto:carlosnepomuc...@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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python #ifdef

2013-05-28 Thread 88888 Dihedral
Carlos Nepomuceno於 2013年5月29日星期三UTC+8上午3時46分37秒寫道:
 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?

Use execfile(filename) at the beginning to get what you want.
The .pyc version is preferred.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python #ifdef

2013-05-28 Thread Terry Jan Reedy

On 5/28/2013 6:25 PM, Joel Goldstick wrote:

On Tue, May 28, 2013 at 6:18 PM, Mark Lawrence breamore...@yahoo.co.uk
mailto:breamore...@yahoo.co.uk wrote:



On 28/05/2013 20:46, Carlos Nepomuceno wrote:
I'd like to have something like '#ifdef' to mix code from Python
2 and 3 in a single file.



https://pypi.python.org/pypi/__six/1.3.0
https://pypi.python.org/pypi/six/1.3.0



my original response was from cell phone.  I just answered that you
can't do ifdefs, implying that there is no preprocessor in python.  I
learned a lot of things I didn't know reading the thread, but I wonder
if it is a good idea in general to try to write code like this.  --
combined 2.x/3.x codebase can be a bear to maintain.


Many people have come to prefer a) a single 23 codebase over b) 
separate 2 and 3 codebases or c) a single 2 codebase repeatedly 
converted to a 3 codebase with 2to3.


They use 2to3 once (well, a few times) to discover differences that need 
to be considered.


For 2.7 and 3.x, the future imports are enough for some code. The six 
module handles harder cases.



I wouldn't do it
unless there was some imposing reason that I must.  Its not just print()
-- that isn't bad, but changes in module names (urllib),


I believe six handles that

 arithmetic, and

from __future__ import integer_division  # spelling?
handles the only change


unicode


Use unicode consistently and
from __future__ import unicode_literals  # spelling?
or the re-addition u'' prefix do quite well.

Otherwise, do not use things that evaporated, like apply() and classic 
classes. (Inherit from object if nothing else.)


This is all hearsay coming from me ;-).

Terry


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


Supporting both 2.x and 3.x in one code base [was Re: Python #ifdef]

2013-05-28 Thread Steven D'Aprano
On Tue, 28 May 2013 18:25:59 -0400, Joel Goldstick wrote:

 I wonder
 if it is a good idea in general to try to write code like this.  --
 combined 2.x/3.x codebase can be a bear to maintain.

Not so much a bear as a tiny little kitten.


 I wouldn't do it
 unless there was some imposing reason that I must.  Its not just print()
 -- that isn't bad, but changes in module names (urllib), arithmetic, and
 unicode especially make this idea in general, very tricky.  Pity the
 next developer who needs to try to maintain it.

It's not that hard really. Well, like all things, it depends on the 
circumstances. If you're reliant on external modules, you *may* have a 
bad time if those modules are 2.x only or 3.x only, but the standard 
library and built-ins don't provide too much of a challenge.

There's no doubt that supporting 2.x and 3.x in one code base is more 
difficult that just supporting one or the other, but the difficulty is 
much less than often supposed. Python 2.7, and to a lesser extent, 2.6, 
are designed to be as easy to port to 3.x as possible, which has the 
happy side-effect that they are also relatively easy to write code for 
them that will also run under 3.x.

Many of the differences can be eliminated with a few __future__ imports:

from __future__ import division, print_function


Differences in behaviour of the built-ins can be eliminated:

from future_builtins import *


Built-ins such as reduce and cmp that have been moved, or eliminated, can 
easily be restored:

if sys.version = '3':
from functools import reduce


Or if you prefer a Better To Ask Forgiveness Than Permission approach:

try:
reduce
except NameError:
from functools import reduce


Name changes of modules are easy to deal with:

try:
import configparser
except ImportError:
import ConfigParser as configparser


The most difficult difference is the difference between strings in 2.x 
and 3.x, but if you drop support for Python 3.1 and 3.2, you can write 
code that works in both 2.7 and 3.3 by using the u syntax. Or just use 
ASCII literals, which work perfectly in both.

There are really only a very few things that cannot be shared between 2.x 
and 3.x: syntactical features that are only supported by 3.x. So if 
you're planning on writing code that runs in both 2.x and 3.x, you need 
to eschew the 3-only features like keyword-only function arguments and 
function annotations.

But that's no different than writing code to support *any* two versions 
that don't have identical syntax. E.g. 2.4 and 2.5: 2.5 supports ternary 
if, `a if condition else b`, while 2.4 does not, so if you need to 
support both, you can't use ternary if. Nearly all the code I write is 
for 2.4 or better, and I can assure you that the hardest part is 
supporting 2.4. Adding 3.x doesn't make it much harder.

(My hat goes off to those supporting 2.3 through 3.3 in one code base. 
That is, frankly, astonishing.)

Nobody *likes* to have to support really old versions missing the cool 
syntax that you want to use, but nobody says that you should even try. 
3.x doesn't change that.



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


Re: Python #ifdef

2013-05-28 Thread Dan Stromberg
Here're slides from a presentation about writing code that runs on 2.x and
3.x: http://stromberg.dnsalias.org/~dstromberg/Intro-to-Python/

And in case you still want a preprocessor for Python (you likely don't need
one this time), here's an example of doing this using the venerable m4:
https://pypi.python.org/pypi/red-black-tree-mod .  Note the many comments
added to keep line numbers consistent.

Sent from my android phone.
On May 28, 2013 12:47 PM, Carlos Nepomuceno carlosnepomuc...@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

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