Python 2/3 compatibility code that reads like English!

2014-12-10 Thread Chris Angelico
So you have a string of text, either a Unicode string in Python 3, or a byte string that's meant to be UTF-8. Most of the way through, you're working with the native string type, for compatibility with other sections of code. But then you want to be certain you're working with a Unicode string...

Re: Python 2-3 compatibility

2013-06-02 Thread Terry Jan Reedy
On 6/2/2013 10:13 AM, Jason Swails wrote: Because Python 2.4 and 2.5 don't support the except Exception as err: syntax, I've used except Exception, err: Is there any way of getting this effect in a way compatible with Py2.4 and 3.x? Don't do either. Just catch the exception with 'except Ex

Re: Python 2-3 compatibility

2013-06-02 Thread Dan Stromberg
From http://stromberg.dnsalias.org/~dstromberg/Intro-to-Python/Python%202%20and%203.pdf: Try/Except: both 2.x and 3.x try: print(1/0) except ZeroDivisionError: extra = sys.exc_info()[1] print('oops') HTH On Sun, Jun 2, 2013 at 7:13 AM, Jason Swails wrote: > Hello Everyone, > > I ha

Python 2-3 compatibility

2013-06-02 Thread Jason Swails
Hello Everyone, I have a Python script that I wrote to support a project that I work on (that runs primarily on Unix OSes). Given its support role in this package, this script should not introduce any other dependencies. As a result, I wrote the script in Python 2, since every Linux currently sh

Re: API design for Python 2 / 3 compatibility

2013-04-14 Thread Stefan Schwarzer
Terry, Ethan: Thanks a lot for your excellent advice. :-) On 2013-04-13 19:32, Terry Jan Reedy wrote: > Approach 2 matches (or should match) io.open, which became > builtin open in Python 3. I would simply document that > ftp_host.open mimics io.open in the same way that > ftp_host.chdir, etceter

Re: API design for Python 2 / 3 compatibility

2013-04-13 Thread Ethan Furman
On 04/13/2013 09:36 AM, Stefan Schwarzer wrote: * Approach 2 When opening remote text files for reading, ftputil will always return unicode strings from `read(line/s)`, regardless of whether it runs under Python 2 or Python 3. Pro: Uniform API, independent on underlying Python

Re: API design for Python 2 / 3 compatibility

2013-04-13 Thread Terry Jan Reedy
On 4/13/2013 12:36 PM, Stefan Schwarzer wrote: Hello, I'm currently changing the FTP client library ftputil [1] so that the same code of the library works with Python 2 (2.6 and up) and Python 3. (At the moment the code is for Python 2 only.) I've run into a API design issue where I don't know w

API design for Python 2 / 3 compatibility

2013-04-13 Thread Stefan Schwarzer
Hello, I'm currently changing the FTP client library ftputil [1] so that the same code of the library works with Python 2 (2.6 and up) and Python 3. (At the moment the code is for Python 2 only.) I've run into a API design issue where I don't know which API I should offer ftputil users under Pytho