I'd like to have something like '#ifdef' to mix code from Python 2 and 3 in a single file.
Is that possible? How?
#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)
On May 28, 2013 1:10 PM, "Carlos Nepomuceno" <carlosne...@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) :
...
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
On 28 May 2013 21:26, "Carlos Nepomuceno" <carlosne...@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]?
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 "inv...@invalid.invalid" instead of your email address?
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?
On 28 May 2013 21:53, "Carlos Nepomuceno" <carlosne...@outlook.com> wrote:
>
> ________________________________
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.
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.
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.
> 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