Re: Getting errors from Imaplib

2014-11-30 Thread Beatrix Willius from Moth Software
On 30.11.2014, at 21:11, Chris Angelico  wrote:
> 
> 2) Network failures and auth problems should be dealt with the same
> way. Change one line of code:
> except (imaplib.IMAP4.error, OSError):
> 
> Now it'll cope with OSError the same way it copes with IMAP errors.
> 
> Does that answer your question?

Thanks! That solves my problem.

Mit freundlichen Grüßen/Regards

Trixi Willius

http://www.mothsoftware.com
Mail Archiver X: The email archiving solution for professionals

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


Re: [Python-Dev] Unicode decode exception

2014-11-30 Thread Michael Torrie
On 11/30/2014 09:19 PM, balaji marisetti wrote:
> The default encoding is "UTF-8". It works if I do:
> 
> with open("filename", errors="ignore") as f:
> 
> 
> So I think Python2, by default, ignores all errors whereas Python3 doesn't

Do you mean that the file is supposed to be utf-8 but isn't?  Because if
it is, you need to tell open about it in Python3 to make sure that's the
decoding scheme it uses. Otherwise you will see this decode error.

Also I'm not sure you posted the full traceback. Usually a
UnicodeDecodeError says something about an invalid byte while using a
specific unicode decoding scheme (say, ASCII, which can't handle utf-8
bytes).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] Unicode decode exception

2014-11-30 Thread balaji marisetti
The default encoding is "UTF-8". It works if I do:

with open("filename", errors="ignore") as f:


So I think Python2, by default, ignores all errors whereas Python3 doesn't

On 1 December 2014 at 01:49, Chris Angelico  wrote:
> On Sun, Nov 30, 2014 at 7:07 PM, balaji marisetti
>  wrote:
>> Hi,
>
> Hi. This list is for the development *of* Python, not development
> *with* Python, so I'm sending this reply also to
> python-list@python.org where it can be better handled. You'll probably
> want to subscribe here:
>
> https://mail.python.org/mailman/listinfo/python-list
>
> or alternatively, point a news reader at comp.lang.python. Let's
> continue this conversation on python-list rather than python-dev.
>
>> When I try to iterate through the lines of a
>> file("openssl-1.0.1j/crypto/bn/asm/x86_64-gcc.c"), I get a
>> UnicodeDecodeError (in python 3.4.0 on Ubuntu 14.04). But there is no
>> such error with python 2.7.6. What could be the problem?
>
> The difference between the two Python versions is that 2.7 lets you be
> a bit sloppy about Unicode vs bytes, but 3.4 requires that you keep
> them properly separate.
>
>> In [39]: with open("openssl-1.0.1j/crypto/bn/asm/x86_64-gcc.c") as f:
>>  for line in f:
>>  print (line)
>>
>> ---
>> UnicodeDecodeErrorTraceback (most recent call last)
>>  in ()
>>   1 with open("../openssl-1.0.1j/crypto/bn/asm/x86_64-gcc.c") as f:
>> > 2 for line in f:
>>   3 print (line)
>>   4
>>
>> /usr/lib/python3.4/codecs.py in decode(self, input, final)
>> 311 # decode input (taking the buffer into account)
>> 312 data = self.buffer + input
>> --> 313 (result, consumed) = self._buffer_decode(data,
>> self.errors, final)
>> 314 # keep undecoded input until the next call
>> 315 self.buffer = data[consumed:]
>>
>>
>> --
>> :-)balaji
>
> Most likely, the line of input that you just reached has a non-ASCII
> character, and the default encoding is ASCII. (Though without the
> actual exception message, I can't be sure of that.) The best fix would
> be to know what the file's encoding is, and simply add that as a
> parameter to your open() call - perhaps this:
>
> with open("filename", encoding="utf-8") as f:
>
> If you use the right encoding, and the file is correctly encoded, you
> should have no errors. If you still have errors... welcome to data
> problems, life can be hard. :|
>
> ChrisA



-- 
:-)balaji
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Suds Python 2.4.3 Proxy

2014-11-30 Thread Chris Angelico
On Mon, Dec 1, 2014 at 8:14 AM, Ned Deily  wrote:
> In article
> ,
>  Chris Angelico  wrote:
>> It might be worth looking at some actual 2.4 documentation.
>> Unfortunately that doesn't seem to be hosted on python.org any more
>> (though I might be wrong),
>
> https://python.org/ -> Documentation
>https://www.python.org/doc/ -> Documentation Releases by Version
>   https://www.python.org/doc/versions/ -> Python 2.4.4
>  https://docs.python.org/release/2.4.4/

Ahh, I see why I didn't find it. Since the 2.4 days, there seems to
have been a reorganization that changed URLs. Compare:

https://docs.python.org/release/2.4/lib/module-urllib2.html
https://docs.python.org/2.7/library/urllib2.html

Glad I was wrong on that, and the first of those links is what the OP needs.

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


Re: Suds Python 2.4.3 Proxy

2014-11-30 Thread Ned Deily
In article 
,
 Chris Angelico  wrote:
> It might be worth looking at some actual 2.4 documentation.
> Unfortunately that doesn't seem to be hosted on python.org any more
> (though I might be wrong),

https://python.org/ -> Documentation
   https://www.python.org/doc/ -> Documentation Releases by Version
  https://www.python.org/doc/versions/ -> Python 2.4.4
 https://docs.python.org/release/2.4.4/

-- 
 Ned Deily,
 n...@acm.org

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


Re: [Python-Dev] Unicode decode exception

2014-11-30 Thread Chris Angelico
On Sun, Nov 30, 2014 at 7:07 PM, balaji marisetti
 wrote:
> Hi,

Hi. This list is for the development *of* Python, not development
*with* Python, so I'm sending this reply also to
python-list@python.org where it can be better handled. You'll probably
want to subscribe here:

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

or alternatively, point a news reader at comp.lang.python. Let's
continue this conversation on python-list rather than python-dev.

> When I try to iterate through the lines of a
> file("openssl-1.0.1j/crypto/bn/asm/x86_64-gcc.c"), I get a
> UnicodeDecodeError (in python 3.4.0 on Ubuntu 14.04). But there is no
> such error with python 2.7.6. What could be the problem?

The difference between the two Python versions is that 2.7 lets you be
a bit sloppy about Unicode vs bytes, but 3.4 requires that you keep
them properly separate.

> In [39]: with open("openssl-1.0.1j/crypto/bn/asm/x86_64-gcc.c") as f:
>  for line in f:
>  print (line)
>
> ---
> UnicodeDecodeErrorTraceback (most recent call last)
>  in ()
>   1 with open("../openssl-1.0.1j/crypto/bn/asm/x86_64-gcc.c") as f:
> > 2 for line in f:
>   3 print (line)
>   4
>
> /usr/lib/python3.4/codecs.py in decode(self, input, final)
> 311 # decode input (taking the buffer into account)
> 312 data = self.buffer + input
> --> 313 (result, consumed) = self._buffer_decode(data,
> self.errors, final)
> 314 # keep undecoded input until the next call
> 315 self.buffer = data[consumed:]
>
>
> --
> :-)balaji

Most likely, the line of input that you just reached has a non-ASCII
character, and the default encoding is ASCII. (Though without the
actual exception message, I can't be sure of that.) The best fix would
be to know what the file's encoding is, and simply add that as a
parameter to your open() call - perhaps this:

with open("filename", encoding="utf-8") as f:

If you use the right encoding, and the file is correctly encoded, you
should have no errors. If you still have errors... welcome to data
problems, life can be hard. :|

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


Re: Getting errors from Imaplib

2014-11-30 Thread Chris Angelico
On Mon, Dec 1, 2014 at 1:42 AM, Beatrix Willius from Moth Software
 wrote:
> no, pinging Yahoo doesn't work. But this is an additional problem - perhaps I 
> tested too often. This is one of my accounts for Imap testing and I only 
> copied the value from Mail.
>
> For getting the error back this doesn't really matter.

I'm not sure what you mean here. There are two ways I could see this
being handled:

1) A network connection failure is fundamentally different from an
auth problem. That's what you're currently seeing; only auth problems
are caught.

2) Network failures and auth problems should be dealt with the same
way. Change one line of code:
except (imaplib.IMAP4.error, OSError):

Now it'll cope with OSError the same way it copes with IMAP errors.

Does that answer your question?

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


Re: I love assert

2014-11-30 Thread Ethan Furman
On 11/30/2014 12:18 AM, Steven D'Aprano wrote:
> 
> Ah, wait, I see the reference. You're talking about the quote from James O
> Coplien:
> 
> "An even more professional approach is to leave the assertions 
> in the code when you ship, and to automatically file a bug report
> on behalf of the end user and perhaps to try to re-start the 
> application every time an assertion fails."
> 
> 
> Sounds reasonable to me (modulo privacy considerations). Many modern
> software already does this: Firefox, Windows, KDE all have forms of crash
> reporting that will automatically submit bug reports to the developers for
> you. And Firefox at least will try (and generally succeed) to automatically
> restart after a crash.

The error-catching/reporting/restarting framework is fine.  My assertion (pun 
intended) is that building that framework
on assert, which can be unknowingly disabled by the end-user, is foolish.

--
~Ethan~



signature.asc
Description: OpenPGP digital signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Getting errors from Imaplib

2014-11-30 Thread Beatrix Willius from Moth Software
Hi Chris,

no, pinging Yahoo doesn't work. But this is an additional problem - perhaps I 
tested too often. This is one of my accounts for Imap testing and I only copied 
the value from Mail.

For getting the error back this doesn't really matter.

> import imaplib
> 
> host = 'imap.mail.yahoo.com'
> try:
>   imap_connection = imaplib.IMAP4(host)
>   print('success')
> except imaplib.IMAP4.error:
>   print('authentication failed')

> On 30.11.2014, at 13:02, Chris Angelico  wrote:
> 
> You're getting an error before it even gets as far as authenticating -
> there's a network-level issue here. Are you able to ping that host?
> Something's not connected.

Mit freundlichen Grüßen/Regards

Trixi Willius

http://www.mothsoftware.com
Mail Archiver X: The email archiving solution for professionals

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


Re: Suds Python 2.4.3 Proxy

2014-11-30 Thread Mark Lawrence

On 30/11/2014 09:10, Chris Angelico wrote:

On Sun, Nov 30, 2014 at 2:01 AM, Jerry Rocteur  wrote:

This works GREAT on 2.7 but when I run it on 2.4 I get:

   File "/usr/lib64/python2.4/urllib2.py", line 580, in proxy_open
 if '@' in host:
TypeError: iterable argument required


It might be worth looking at some actual 2.4 documentation.
Unfortunately that doesn't seem to be hosted on python.org any more
(though I might be wrong), so you'll want to look on your system
itself and find some docs.



This 
https://hg.python.org/cpython/file/ceec209b26d4/Doc/lib/liburllib2.tex 
should help.



Otherwise, why not simply install Python 2.7 on those systems? It can
happily coexist with the system 2.4.

ChrisA



--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: Help needed

2014-11-30 Thread Denis McMahon
On Sat, 29 Nov 2014 11:35:18 -0800, Gautam R Bharadwaj wrote:

> Here is the code in python, this code arranges the alphabets in
> descending order and now I want to encode each alphabet with 0 and next
> alphabet with 1, then 00,01,10,11,000,001 and so on. Please help me with
> that.
> 
> //
> //CODE
> 
> from collections import defaultdict import string text ='intalks is an
> organization comprised of passionate
> students'.lower().translate(None,string.punctuation+' ')
> c = defaultdict(int)
> c.update({letter:0 for letter in string.lowercase[:26]})
> for letter in text:
> c[letter] += 1
> 
> for letter,freq in sorted(c.iteritems(),key=lambda (l,f): (-f,l)):
> print freq, letter

If you can confirm that the following:

000111101100010110010011001101001111010101011001100100011110

is the output you want in terms of the original string encoded according 
to your encoding scheme, then your problem domain currently is two fold:

a) To write a function that generates the encoding
b) To apply the encoding to the original string

To achieve the former, I used a combination of determining a string 
length, converting a string to an integer using base 2, converting a 
number to a string in binary form, and generating arbitrary length 
strings of zeroes.

Not necessarily all done in the most pythonic of manners I must admit. 
Have you attempted anything to generate you string of encodings yet? 
Because until you show a good faith effort, you don't my solution to your 
homework problem for free.

To achieve the latter, I took the encodings and strung them together 
according to the letters they represented in the original string.

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Getting errors from Imaplib

2014-11-30 Thread Chris Angelico
On Sun, Nov 30, 2014 at 7:36 PM, Beatrix Willius from Moth Software
 wrote:
>
> Let's say I have the following very simple Python code:
>
> import imaplib
>
> host = 'imap.gmail.com'
> try:
> imap_connection = imaplib.IMAP4(host)
> print('success')
> except imaplib.IMAP4.error:
> print('authentication failed')
>
> OSError: [Errno 65] No route to host

You're getting an error before it even gets as far as authenticating -
there's a network-level issue here. Are you able to ping that host?
Something's not connected.

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


Getting errors from Imaplib

2014-11-30 Thread Beatrix Willius from Moth Software
Hi,

first poster here. I still consider myself pretty much a Python newbie.

Let's say I have the following very simple Python code:

import imaplib

host = 'imap.gmail.com'
try:
imap_connection = imaplib.IMAP4(host)
print('success')
except imaplib.IMAP4.error:
print('authentication failed')

Now, Gmail doesn't like non-ssl connections. In this case I only want to return 
an error. This worked for Pyhton 2.7 but in Python 3.4 I only get a traceback 
(see below). The line print('authentication failed') is not executed at all. 
How do I capture the error for using the print function?

Mac OS 10.10. Python 3.4.1. The script is part of a larger one that is talking 
to my Xojo application.

Connected to pydev debugger (build 135.1057)
Traceback (most recent call last):
  File "/Applications/Develop/PyCharm CE.app/helpers/pydev/pydevd.py", line 
1733, in 
debugger.run(setup['file'], None, None)
  File "/Applications/Develop/PyCharm CE.app/helpers/pydev/pydevd.py", line 
1226, in run
pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/Develop/PyCharm CE.app/helpers/pydev/_pydev_execfile.py", 
line 38, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc) #execute the script
  File "/Users/beatrixwillius/Documents/ Datei/Development/Mail 
Archiver/Projects/MainClasses Projects/imap/test/test.py", line 6, in 
imap_connection = imaplib.IMAP4(host)
  File 
"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/imaplib.py", 
line 181, in __init__
self.open(host, port)
  File 
"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/imaplib.py", 
line 257, in open
self.sock = self._create_socket()
  File 
"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/imaplib.py", 
line 247, in _create_socket
return socket.create_connection((self.host, self.port))
  File 
"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socket.py", 
line 509, in create_connection
raise err
  File 
"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socket.py", 
line 500, in create_connection
sock.connect(sa)
OSError: [Errno 65] No route to host

Mit freundlichen Grüßen/Regards

Trixi Willius

http://www.mothsoftware.com
Mail Archiver X: The email archiving solution for professionals

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


Re: Suds Python 2.4.3 Proxy

2014-11-30 Thread Chris Angelico
On Sun, Nov 30, 2014 at 2:01 AM, Jerry Rocteur  wrote:
> This works GREAT on 2.7 but when I run it on 2.4 I get:
>
>   File "/usr/lib64/python2.4/urllib2.py", line 580, in proxy_open
> if '@' in host:
> TypeError: iterable argument required

It might be worth looking at some actual 2.4 documentation.
Unfortunately that doesn't seem to be hosted on python.org any more
(though I might be wrong), so you'll want to look on your system
itself and find some docs.

Otherwise, why not simply install Python 2.7 on those systems? It can
happily coexist with the system 2.4.

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


Re: I love assert

2014-11-30 Thread Steven D'Aprano
Ethan Furman wrote:

> On 11/28/2014 05:30 PM, Steven D'Aprano wrote:
>> alister wrote:
>> 
>>> And as may wiser people than me have already highlighted Assertions can
>>> be switched off in python which means they cannot be relied upon in
>>> production code invalidating the authors suggestion that they can
>>> automate bug reports & "Extend testing into the lifetime of the product"
>> 
>> I'm afraid that you appear to have missed the point of assertions as
>> tests. Since they should never be used to *enforce* correct behaviour,
>> but only to *verify* behaviour is correct (i.e. as a form of testing), it
>> doesn't matter if you disable them. If I turn them off, all that happens
>> is that the tests won't run *for me*. The software will still be just as
>> correct, or just as buggy, regardless of the assertions.
> 
> And you have missed the point that building an intricate error-reporting,
> program restarting framework on top of 'assert' is ridiculous.

You are absolutely correct, I did miss that point. I didn't even notice
anyone suggest building an intricate error-reporting, program restarting
framework, let alone that it should be built on top of assert. Who made
that suggestion?

To be frank, I don't even know what that would mean -- how do you build such
a framework on top of assert? That's like saying "on top of print", only
even less useful. print() at least can redirect output to a file. assert
can only do to things: nothing, or raise AssertionError.


Ah, wait, I see the reference. You're talking about the quote from James O
Coplien:

"An even more professional approach is to leave the assertions 
in the code when you ship, and to automatically file a bug report
on behalf of the end user and perhaps to try to re-start the 
application every time an assertion fails."


Sounds reasonable to me (modulo privacy considerations). Many modern
software already does this: Firefox, Windows, KDE all have forms of crash
reporting that will automatically submit bug reports to the developers for
you. And Firefox at least will try (and generally succeed) to automatically
restart after a crash.

I'm sure that this crash reporting software isn't built on top of assert.
That would be absurd. But whatever it is built from, once it exists, then
it ought to be entirely agnostic whether exceptions are raised by assert or
raise. Python already supports half of this: the logging module can
intercept unexpected exceptions (including AssertionError), log them, email
them, display them in a GUI window, or any other output method you should
like. Having an application catch AssertionError and then phone home to
automatically submit diagnostic information to the developer shouldn't be
terribly hard, although the privacy issues need to be considered.

The other part, restarting the application, may be appropriate for some apps
but not others. How you would technically do this, I'm not sure. You
probably need some sort of daemon process which respawns your app if it
dies.

Which reminds me of this:

http://www.csd.uwo.ca/~magi/personal/humour/Computer_Folklore/Robin%20Hood%20And%20Friar%20Tuck.html



-- 
Steven

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