Re: myths about python 3

2010-01-27 Thread David Malcolm
On Wed, 2010-01-27 at 16:25 -0500, Benjamin Kaplan wrote:
> On Wed, Jan 27, 2010 at 3:56 PM, John Nagle  wrote:
> > Daniel Fetchinson wrote:
> >>
> >> Hi folks,
> >>
> >> I was going to write this post for a while because all sorts of myths
> >> periodically come up on this list about python 3. I don't think the
> >> posters mean to spread false information on purpose, they simply are
> >> not aware of the facts.
> >>
> >> My list is surely incomplete, please feel free to post your favorite
> >> misconception about python 3 that people periodically state, claim or
> >> ask about.
> >
> > Myths about Python 3:
> >
> > 1.  Python 3 is supported by major Linux distributions.
> >
> >FALSE - most distros are shipping with Python 2.4, or 2.5 at best.
> >
> The latest versions of Ubuntu Jaunty and Karmic, Fedora 11 and 12, and
> OpenSUSE 11.2 all use Python 2.6. Ubuntu has been shipping python 3
> since Jaunty came out last April. According to Fedora's package index,
> Python 3 is in the devel version which probably means it will be in
> upcoming versions of Fedora as well.

FWIW, more information on Fedora python 3 status here:
https://fedoraproject.org/wiki/Features/Python3F13

[snip]

> Give the package maintainers time to update. There were some pretty
> big changes to the C API. Most of the major 3rd party packages like
> numpy and MySQLdb have already commited to having a Python 3 version.
> They just haven't gotten them out yet.

I'll take this opportunity to make a shameless plug for my 2to3c tool:
http://dmalcolm.livejournal.com/3935.html

which takes some of the grunt work out of taking C code and making it
compilable against both 2 and 3.  (it will still require a human to do
some of the work, alas).


Hope this is helpful
Dave

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


Re: Awful book warning: How to think like a (Python) programmer - non-working examples

2010-02-08 Thread David Malcolm
On Mon, 2010-02-08 at 12:53 -0800, Andrej Mitrovic wrote:
> The book covers Python 2.x syntax.
> 
> You might have downloaded Python 3.1, which has different syntax then
> Python 2.x. From what I can tell, the first example on page 7 is ">>>
> print 1 + 1".
> 
> Try issuing this command:
> print(1 + 1)
> 
> If everything goes well, and you get '2' as the answer, then you're
> probably using Python 3.x. You will have to download the Python 2.x
> binaries from the Python website, install Python 2.x, and try the
> example from the book again.

Sorry to nitpick; the main thrust of the above sounds correct, in that:
print 1 + 1
works in Python 2 but fails in Python 3, but, a minor correction, note
that:
print(1+1)
does work in Python 2 as well as in Python 3; the parentheses are
treated (in the former) as denoting grouping of a subexpression, rather
than function invocation (in the latter):

Python 2.6.2 (r262:71600, Jan 25 2010, 13:22:47) 
[GCC 4.4.2 20100121 (Red Hat 4.4.2-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print(1+1)
2

This can be useful if you're trying to write short fragments of code
that work with both.

Look at the startup message, or run this command, which should work on
both python2 and python3:
  import sys; print(sys.version)

Hope this is helpful
Dave

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


Re: Need debugging knowhow for my creeping Unicodephobia

2010-02-10 Thread David Malcolm
On Wed, 2010-02-10 at 12:17 -0800, Anthony Tolle wrote:
> On Feb 10, 2:09 pm, kj  wrote:
> > Some people have mathphobia.  I'm developing a wicked case of
> > Unicodephobia.
> > [snip]
> 
> Some general advice (Looks like I am reiterating what MRAB said -- I
> type slower :):
> 
> 1. If possible, use unicode strings for everything.  That is, don't
> use both str and unicode within the same project.
> 
> 2. If that isn't possible, convert strings to unicode as early as
> possible, work with them that way, then convert them back as late as
> possible.
> 
> 3. Know what type of string you are working with!  If a function
> returns or accepts a string value, verify whether the expected type is
> unicode or str.
> 
> 4. Consider switching to Python 3.x, since there is only one string
> type (unicode).

Some further nasty gotchas:

5. Be wary of the encoding of sys.stdout (and stderr/stdin), e.g. when
issuing a "print" statement:  they can change on Unix depending on
whether the python process is directly connected to a tty or not.

(a) If they're directly connected to a tty, their encoding is taken from
the locale, UTF-8 on my machine:
[da...@brick ~]$ python -c 'print u"\u03b1\u03b2\u03b3"'
αβγ
(prints alpha, beta, gamma to terminal, though these characters might
not survive being sent in this email)

(b) If they're not (e.g. cronjob, daemon, within a shell pipeline, etc)
their encoding is the default encoding, which is typically ascii;
rerunning the same command, but piping into "cat":
[da...@brick ~]$ python -c 'print u"\u03b1\u03b2\u03b3"'| cat
Traceback (most recent call last):
  File "", line 1, in 
UnicodeEncodeError: 'ascii' codec can't encode characters in position
0-2: ordinal not in range(128)

(c) These problems can lurk in sources and only manifest themselves
during _deployment_ of code.  You can set PYTHONIOENCODING=ascii in the
environment to force (a) to behave like (b), so that your code will fail
whilst you're _developing_ it, rather than on your servers at midnight:
[da...@brick ~]$ PYTHONIOENCODING=ascii python -c 'print u"\u03b1\u03b2
\u03b3"'
Traceback (most recent call last):
  File "", line 1, in 
UnicodeEncodeError: 'ascii' codec can't encode characters in position
0-2: ordinal not in range(128)

(Given the above, it could be argued perhaps that one should never
"print" unicode instances, and instead should write the data to
file-like objects, specifying an encoding.  Not sure).

6. If you're using pygtk (specifically the "pango" module, typically
implicitly imported), be warned that it abuses the C API to set the
default encoding inside python, which probably breaks any unicode
instances in memory at the time, and is likely to cause weird side
effects:
[da...@brick ~]$ python
Python 2.6.2 (r262:71600, Jan 25 2010, 13:22:47) 
[GCC 4.4.2 20100121 (Red Hat 4.4.2-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> import pango
>>> sys.getdefaultencoding()
'utf-8'
(the above is on Fedora 12, though I'd expect to see the same weirdness
on any linux distro running gnome 2)

Python 3 will probably make this all much easier; you'll still have to
care about encodings when dealing with files/sockets/etc, but it should
be much more clear what's going on.  I hope.

Hope this is helpful
Dave

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