enabling universal newline

2012-11-02 Thread Peter Kleiweg

In Python 3.1 and 3.2

At start-up, the value of sys.stdin.newlines is None, which 
means, universal newline should be enabled. But it isn't.

So I do this:

sys.stdin = io.TextIOWrapper(sys.stdin.detach(), newline=None)

Now, sys.stdin.newlines is still None, but universal newline is 
enabled.

Why is this?



-- 
Peter Kleiweg
http://pkleiweg.home.xs4all.nl/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: enabling universal newline

2012-11-03 Thread Peter Kleiweg
Steven D'Aprano schreef op de 2e dag van de slachtmaand van het jaar 2012:

> On Fri, 02 Nov 2012 23:22:53 +0100, Peter Kleiweg wrote:
> 
> > In Python 3.1 and 3.2
> > 
> > At start-up, the value of sys.stdin.newlines is None, which means,
> > universal newline should be enabled. But it isn't.
> 
> What makes you think it is not enabled?

Script 1:

#!/usr/bin/env python3.1
import sys
print(sys.stdin.readlines())

Output:

~ test.py < text
['a\rbc\rdef\r']


Script 2:

#!/usr/bin/env python3.1
import io, sys
sys.stdin = io.TextIOWrapper(sys.stdin.detach(), newline=None)
print(sys.stdin.readlines())

Output:

    ~ test.py < text
['a\n', 'bc\n', 'def\n']


-- 
Peter Kleiweg
http://pkleiweg.home.xs4all.nl/
-- 
http://mail.python.org/mailman/listinfo/python-list


sys.stdout.detach() results in ValueError

2012-03-07 Thread Peter Kleiweg

I want to write out some binary data to stdout in Python3. I 
thought the way to do this was to call detach on sys.stdout. But 
apparently, you can't. Here is a minimal script:

#!/usr/bin/env python3.1
import sys
fp = sys.stdout.detach()

Not yet using fp in any way, this script gives the following error:

Exception ValueError: 'underlying buffer has been detached' in

Same in Python 3.1.4 and Python 3.2.2

So, what do I do if I want to send binary data to stdout? 



-- 
Peter Kleiweg
http://pkleiweg.home.xs4all.nl/
-- 
http://mail.python.org/mailman/listinfo/python-list


what is best method to set sys.stdout to utf-8?

2012-03-07 Thread Peter Kleiweg

In Python 3, there seem to be two ways to set sys.stdout to 
utf-8 after the script has started:

sys.stdout = codecs.getwriter('utf-8')(sys.stdout.detach())

sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8')

I guess the second is better. At start-up, type(sys.stdout) is 
, and it's also after using the 
second method.

After using the first method, type(sys.stdout) is changed to 
. 

Should I always use the second method?

-- 
Peter Kleiweg
http://pkleiweg.home.xs4all.nl/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sys.stdout.detach() results in ValueError

2012-03-07 Thread Peter Kleiweg
Dave Angel schreef op de 7e dag van de lentemaand van het jaar 2012:

> On 03/07/2012 02:41 PM, Peter Kleiweg wrote:
> > I want to write out some binary data to stdout in Python3. I
> > thought the way to do this was to call detach on sys.stdout. But
> > apparently, you can't. Here is a minimal script:
> > 
> >  #!/usr/bin/env python3.1
> >  import sys
> >  fp = sys.stdout.detach()
> > 
> > Not yet using fp in any way, this script gives the following error:
> > 
> >  Exception ValueError: 'underlying buffer has been detached' in
> > 
> > Same in Python 3.1.4 and Python 3.2.2
> > 
> > So, what do I do if I want to send binary data to stdout?
> > 
> > 
> > 
> 
> sys.stdout.write(  some_binary_data )

TypeError: must be str, not bytes




-- 
Peter Kleiweg
http://pkleiweg.home.xs4all.nl/
-- 
http://mail.python.org/mailman/listinfo/python-list


initialising a list of lists

2005-11-16 Thread Peter Kleiweg

This does not what I want it to do:

>>> a = [[]] * 6
>>> a[3].append('X')
>>> a
[['X'], ['X'], ['X'], ['X'], ['X'], ['X']]

This does what I want:

>>> b = [[] for _ in range(6)]
>>> b[3].append('X')
>>> b
[[], [], [], ['X'], [], []]

The first is clear and wrong. The second is hairy and right.
Is there a way to do it clear and right?

-- 
Peter Kleiweg  L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)  S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/~kleiweg/ls.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: initialising a list of lists

2005-11-16 Thread Peter Kleiweg
Fredrik Lundh schreef op de 16e dag van de slachtmaand van het jaar 2005:

> Peter Kleiweg wrote:
> 
> > This does not what I want it to do:
> >
> >>>> a = [[]] * 6
> >>>> a[3].append('X')
> >>>> a
> >[['X'], ['X'], ['X'], ['X'], ['X'], ['X']]
> >
> > This does what I want:
> >
> >>>> b = [[] for _ in range(6)]
> >>>> b[3].append('X')
> >>>> b
> >[[], [], [], ['X'], [], []]
> >
> > The first is clear and wrong. The second is hairy and right.
> > Is there a way to do it clear and right?
> 
> http://www.python.org/doc/faq/programming.html#how-do-i-create-a-multidimensional-list

In other words: no there isn't.

-- 
Peter Kleiweg  L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)  S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/~kleiweg/ls.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Quitting a Tkinter application with confirmation

2005-11-16 Thread Peter Kleiweg

I have an application written in Tkinter. There is a menu item 
'quit' that calls the function 'quit'. If 'quit' is called, it 
first checks if there is unsaved data. If there is, it won't let 
the application exit unless you confirm to disregard the 
changes.

So far, so good.

I want the program to behave identical if the 'close' button of 
the application window is clicked. I tried the code below, 
using a class derived from Tk that redefines the destroy 
method. That seems to work. At least on Linux. 

My questions: 

Is this the correct and save way to do this? Will it work on any 
operating system? Shouldn't I do some event capturing instead? 
(Capture the SIGTERM or SIGQUIT, sent by the Window manager to 
the application.)

from Tkinter import *
import tkMessageBox

def quit():
if not changes:
root.quit()
else:
if tkMessageBox.askyesno(_('Quit'), _('Project is not saved. Ignore 
changes and quit?')):
root.quit()

class myTk(Tk):
    def destroy(self):
quit()

root = myTk()

-- 
Peter Kleiweg  L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)  S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/~kleiweg/ls.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Quitting a Tkinter application with confirmation

2005-11-16 Thread Peter Kleiweg
Fredrik Lundh schreef op de 16e dag van de slachtmaand van het jaar 2005:

> Peter Kleiweg wrote:
> 
> > I want the program to behave identical if the 'close' button of
> > the application window is clicked. I tried the code below,
> > using a class derived from Tk that redefines the destroy
> > method. That seems to work. At least on Linux.
> >
> > My questions:
> >
> > Is this the correct and save way to do this? Will it work on any
> > operating system? Shouldn't I do some event capturing instead?
> 
> the right way to do this is to implement a WM_DELETE_WINDOW
> protocol handler:
> 
> http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm#protocols
> 
> in your case, adding
> 
> root.protocol("WM_DELETE_WINDOW", root.destroy)
> 
> to the right place should be enough.

Yes, that works.

By the way, this example ends the program with root.destroy().
I used root.quit(). Is there a reason for using one or the other, 
or does it not matter? 


-- 
Peter Kleiweg  L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)  S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/~kleiweg/ls.html
-- 
http://mail.python.org/mailman/listinfo/python-list


from date/time string to seconds since epoch

2005-07-06 Thread Peter Kleiweg

Is there a safe and clean way to parse a date/time string into
seconds since epoch?

I have a string with date and time in GMT. I can get the correct
value using this:


#!/usr/bin/env python
import os
os.environ['TZ'] = 'UTC'
import time
s = 'Wed, 06 Jul 2005 16:49:38 GMT'
seconds = time.mktime(time.strptime(s, '%a, %d %b %Y %H:%M:%S %Z'))


However, I also need conversions to localtime. Setting TZ to UTC
before importing the time module won't let me do this. Changing
TZ after importing time has no effect.


-- 
Peter Kleiweg  L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)  S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/~kleiweg/ls.html

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


Re: from date/time string to seconds since epoch

2005-07-06 Thread Peter Kleiweg
[EMAIL PROTECTED] schreef op de 6e dag van de hooimaand van het jaar 2005:

> You can calculate the offset of where you are like this:
>
> [EMAIL PROTECTED]:~ $ python
> Python 2.4.1 (#2, Mar 30 2005, 21:51:10)
> [GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> py> import time
> py> offset = 0
> py> if time.daylight:
> py. offset = time.altzone
> py. else:
> py. offset = time.timezone
> py.
> py> print offset
> -7200
> py>
>
> You can use time.time() to get the UTC time, then add the offset.

The offset is for this moment, not for the date and time of the
string parsed.

-- 
Peter Kleiweg  L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)  S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/~kleiweg/ls.html

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


split on NO-BREAK SPACE

2007-07-22 Thread Peter Kleiweg

Is this a bug or a feature?


Python 2.4.4 (#1, Oct 19 2006, 11:55:22) 
[GCC 2.95.3 20010315 (SuSE)] on linux2

>>> a = 'a b c\240d e'
>>> a
'a b c\xa0d e'
>>> a.split()
['a', 'b', 'c\xa0d', 'e']
>>> a = a.decode('latin-1')
>>> a
u'a b c\xa0d e'
>>> a.split()
[u'a', u'b', u'c', u'd', u'e']



-- 
Peter Kleiweg  L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)  S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/kleiweg/ls.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: split on NO-BREAK SPACE

2007-07-22 Thread Peter Kleiweg
Carsten Haese schreef op de 22e dag van de hooimaand van het jaar 2007:

> On Sun, 2007-07-22 at 17:15 +0200, Peter Kleiweg wrote:
> > Is this a bug or a feature?
> > 
> > 
> > Python 2.4.4 (#1, Oct 19 2006, 11:55:22) 
> > [GCC 2.95.3 20010315 (SuSE)] on linux2
> > 
> > >>> a = 'a b c\240d e'
> > >>> a
> > 'a b c\xa0d e'
> > >>> a.split()
> > ['a', 'b', 'c\xa0d', 'e']
> > >>> a = a.decode('latin-1')
> > >>> a
> > u'a b c\xa0d e'
> > >>> a.split()
> > [u'a', u'b', u'c', u'd', u'e']
> 
> It's a feature. See help(str.split): "If sep is not specified or is
> None, any whitespace string is a separator."

Define "any whitespace".
Why is it different in  and ?
Why does split() split when it says NO-BREAK?

-- 
Peter Kleiweg  L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)  S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/kleiweg/ls.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: split on NO-BREAK SPACE

2007-07-22 Thread Peter Kleiweg
Carsten Haese schreef op de 22e dag van de hooimaand van het jaar 2007:

> On Sun, 2007-07-22 at 17:44 +0200, Peter Kleiweg wrote: 
> > > It's a feature. See help(str.split): "If sep is not specified or is
> > > None, any whitespace string is a separator."
> > 
> > Define "any whitespace".
> 
> Any string for which isspace returns True.

Define white space to isspace()
 
> > Why is it different in  and ?
> 
> >>> '\xa0'.isspace()
> False
> >>> u'\xa0'.isspace()
> True

Here is another "space":

  >>> u'\uFEFF'.isspace()
  False

isspace() is inconsistent
 
> For byte strings, Python doesn't know whether 0xA0 is a whitespace
> because it depends on the encoding whether the number 160 corresponds to
> a whitespace character. For unicode strings, code point 160 is
> unquestionably a whitespace, because it is a no-break SPACE.

I question it. And so does the sre module:

  \s Matches any whitespace character; equivalent to [ \t\n\r\f\v]

Where is the NO-BREAK SPACE in there?

 
> > Why does split() split when it says NO-BREAK?
> 
> Precisely. It says NO-BREAK. It doesn't say NO-SPLIT.

That is a stupid answer.


-- 
Peter Kleiweg  L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)  S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/kleiweg/ls.html
-- 
http://mail.python.org/mailman/listinfo/python-list


space / nonspace

2007-07-22 Thread Peter Kleiweg

>>> import re
>>> s = u'a b\u00A0c d'
>>> s.split()
[u'a', u'b', u'c', u'd']
>>> re.findall(r'\S+', s)
[u'a', u'b\xa0c', u'd']  


This isn't documented either:

>>> s = ' b c '
>>> s.split()
['b', 'c']
>>> s.split(' ')
['', 'b', 'c', '']

   

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


How to send utf-8 mail in Python 3?

2010-03-05 Thread Peter Kleiweg

I try to send e-mail from Python 3.1.1

Encoding as iso-8859-1 goes fine.
But encoding as utf-8 doesn't work.
What am I doing wrong?


Python 3.1.1 (r311:74480, Oct  2 2009, 11:50:52) 
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from email.mime.text import MIMEText
>>> text = 'H\u00e9'
>>> msg = MIMEText(text, 'plain', 'iso-8859-1')
>>> print(msg.as_string())
Content-Type: text/plain; charset="iso-8859-1"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable

H=E9
>>> msg = MIMEText(text, 'plain', 'utf-8')
Traceback (most recent call last):
  File "/my/opt/Python-3/lib/python3.1/email/message.py", line 269, in 
set_charset
cte(self)
TypeError: 'str' object is not callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "", line 1, in 
  File "/my/opt/Python-3/lib/python3.1/email/mime/text.py", line 30, in __init__
self.set_payload(_text, _charset)
  File "/my/opt/Python-3/lib/python3.1/email/message.py", line 234, in 
set_payload
self.set_charset(charset)
  File "/my/opt/Python-3/lib/python3.1/email/message.py", line 271, in 
set_charset
self._payload = charset.body_encode(self._payload)
  File "/my/opt/Python-3/lib/python3.1/email/charset.py", line 380, in 
body_encode
return email.base64mime.body_encode(string)
  File "/my/opt/Python-3/lib/python3.1/email/base64mime.py", line 94, in 
body_encode
enc = b2a_base64(s[i:i + max_unencoded]).decode("ascii")
TypeError: must be bytes or buffer, not str
>>> 




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


os.system: string encoding

2010-06-25 Thread Peter Kleiweg

How do I set the string encoding for os.system to anything other then UTF-8?


(peter) ~ echo $LANG
nl...@euro
(peter) ~ python3
Python 3.1.1 (r311:74480, Oct  2 2009, 11:50:52) 
>>> '\N{EURO SIGN}'
'€'
>>> import os
>>> os.system('echo \N{EURO SIGN}')
â?¬
0
>>> 


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


Re: I strongly dislike Python 3

2010-06-26 Thread Peter Kleiweg
Stephen Hansen schreef op de 26e dag van de zomermaand van het jaar 2010:

> There were various serious problems with Python 2 which could not be fixed in
> a backwards compatible way; we've been living with them for years and years
> now, and it was decided that a single break to go back and correct them would
> be preferable to keeping them forever.
> 
> From the fact that "strings" in Python 2 ended up doing double-duty as both
> octet sequences and arrays of character text, to yes the syntactical

I have been using Python 3 for quite some time now, and this 
string thing in Python 3 has caused me more headaches than it 
ever did in Python 2. Though it seems nice in theory to know 
exactly what type of bytes or string you have, you're working 
with a standard library that often doesn't know how to handle 
this distinction. The e-mail module is broken, as well as the 
cgi module. When I read a file from a zip archive, I can't open 
it as a binary stream, but I get binary anyway. I can't call 
os.system with a binary string, only with a text string, and 
that's encoded into utf-8, no matter what encoding I need.

Some basic text string functions seem to be working on byte 
string functions as well, but sometimes they don't, and there's 
no rhyme in why it does or doesn't.

>>> 'abcd'[0] == 'abcd'[:1]
True
>>> b'abcd'[0] == b'abcd'[:1]
False

Why

In the end it may be all better, but at the moment, still a lot 
of work has to be done to fix the standard library. And then 
there's all those packages out there, that have to be ported... 
I still can't use numpy in Python 3.


-- 
Peter Kleiweg  L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)  S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/kleiweg/ls.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I strongly dislike Python 3

2010-06-26 Thread Peter Kleiweg
Stephen Hansen schreef op de 26e dag van de zomermaand van het jaar 2010:

> On 6/26/10 11:55 AM, Peter Kleiweg wrote:
> 
> > I have been using Python 3 for quite some time now, and this
> [snip]
> 
> I'm not advocating using Python 3 or that it doesn't have plenty of work to do
> still. I don't use it yet, so can't really comment on any of your issues
> except to ask: Have you opened bug reports?

Yes.
 




-- 
Peter Kleiweg  L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)  S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/kleiweg/ls.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I strongly dislike Python 3

2010-06-27 Thread Peter Kleiweg
David Cournapeau schreef op de 27e dag van de zomermaand van het jaar 2010:

> I doubt "porting is easier than you think" will convince many people
> if they don't know what the gain will be. For example, porting numpy
> and scipy to py3k has been easier than I thought, but besides making
> it easier for other people to switch, I can't see *any* benefit.
> That's bound to frustrate people.

The latest NumPy is 1.4.1, and it does not install with Python 3.1.1

-- 
Peter Kleiweg  L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)  S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/kleiweg/ls.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: os.system: string encoding

2010-06-27 Thread Peter Kleiweg
Martin v. Loewis schreef op de 27e dag van de zomermaand van het jaar 2010:

> Am 25.06.2010 17:13, schrieb Peter Kleiweg:
> > How do I set the string encoding for os.system to anything other then UTF-8?
> 
> You shouldn't have to set it, as it should use your locale's encoding.
> In 3.1.2, it will.
> 
> For the moment, you can encode the string explicitly, and pass a byte
> string.

That doesn't work

Python 3.1.1 (r311:74480, Oct  2 2009, 11:50:52) 
>>> import os
>>> os.system('echo \N{EURO SIGN}'.encode('iso-8859-15'))
Traceback (most recent call last):
  File "", line 1, in 
TypeError: must be string, not bytes


-- 
Peter Kleiweg  L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)  S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/kleiweg/ls.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Announcing: python-ghostscript 0.3

2010-08-12 Thread Peter Kleiweg
Hartmut Goebel schreef op de 12e dag van de oogstmaand van het jaar 2010:

> Here is an example for how to use the high-level interface of
> `python-ghostscript`. This implements a very basic ps2pdf-tool::
> 
>   import sys
>   import ghostscript
> 
>   args = [
>   "ps2pdf",   # actual value doesn't matter
>   "-dNOPAUSE", "-dBATCH", "-dSAFER",
>   "-sDEVICE=pdfwrite",
>   "-sOutputFile=" + sys.argv[1],
>   "-c", ".setpdfwrite",
>   "-f",  sys.argv[2]
>   ]
> 
>   ghostscript.Ghostscript(*args)

How is this different from os.system(' '.join(args)) ?



-- 
Peter Kleiweg  L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)  S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/kleiweg/ls.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Announcing: python-ghostscript 0.3

2010-08-12 Thread Peter Kleiweg
Aahz schreef op de 12e dag van de oogstmaand van het jaar 2010:

> In article ,
> Peter Kleiweg   wrote:
> >Hartmut Goebel schreef op de 12e dag van de oogstmaand van het jaar 2010:
> >
> >> Here is an example for how to use the high-level interface of
> >> `python-ghostscript`. This implements a very basic ps2pdf-tool::
> >> 
> >>   import sys
> >>   import ghostscript
> >> 
> >>   args = [
> >>   "ps2pdf",# actual value doesn't matter
> >>   "-dNOPAUSE", "-dBATCH", "-dSAFER",
> >>   "-sDEVICE=pdfwrite",
> >>   "-sOutputFile=" + sys.argv[1],
> >>   "-c", ".setpdfwrite",
> >>   "-f",  sys.argv[2]
> >>   ]
> >> 
> >>   ghostscript.Ghostscript(*args)
> >
> >How is this different from os.system(' '.join(args)) ?
> 
> You don't have problems with shell metacharacters.

Then use os.spawnv(os.P_WAIT, args[0], args) 



-- 
Peter Kleiweg  L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)  S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/kleiweg/ls.html
-- 
http://mail.python.org/mailman/listinfo/python-list