Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #15467 > unrolled thread

Help catching error message

Started byGnarlodious <gnarlodious@gmail.com>
First post2011-11-08 05:20 -0800
Last post2011-11-09 02:57 +0000
Articles 6 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Help catching error message Gnarlodious <gnarlodious@gmail.com> - 2011-11-08 05:20 -0800
    Re: Help catching error message Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-11-08 18:15 +0100
    Re: Help catching error message Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-11-08 18:24 +0100
    Re: Help catching error message Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-11-08 22:16 +0000
      Re: Help catching error message Gnarlodious <gnarlodious@gmail.com> - 2011-11-08 17:48 -0800
        Re: Help catching error message Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-11-09 02:57 +0000

#15467 — Help catching error message

FromGnarlodious <gnarlodious@gmail.com>
Date2011-11-08 05:20 -0800
SubjectHelp catching error message
Message-ID<8629ed5b-a657-4bda-9930-a0571279f4d6@p20g2000prm.googlegroups.com>
What I say is this:

def SaveEvents(self,events):
   try:
      plistlib.writePlist(events, self.path+'/Data/Events.plist') #
None if OK
   except IOError:
      return "IOError: [Errno 13] Apache can't write Events.plist
file"

Note that success returns"None" while failure returns a string.

I catch the error like this:

errorStatus=Data.Dict.SaveEvents(Data.Plist.Events)
if errorStatus: content=errorStatus

It works, but isn there a more elegant way to do it? As in, one line?
I can imagine if success returned nothing then content would remain
unchanged. Isn't there a built-in way to send an error string back and
then catch it as a variable name?

This is Py3 inside WSGI.

-- Gnarlie

[toc] | [next] | [standalone]


#15471

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2011-11-08 18:15 +0100
Message-ID<mailman.2545.1320772544.27778.python-list@python.org>
In reply to#15467
Gnarlodious wrote:
> What I say is this:
>
> def SaveEvents(self,events):
>    try:
>       plistlib.writePlist(events, self.path+'/Data/Events.plist') #
> None if OK
>    except IOError:
>       return "IOError: [Errno 13] Apache can't write Events.plist
> file"
>
> Note that success returns"None" while failure returns a string.
>
> I catch the error like this:
>
> errorStatus=Data.Dict.SaveEvents(Data.Plist.Events)
> if errorStatus: content=errorStatus
>
> It works, but isn there a more elegant way to do it? As in, one line?
> I can imagine if success returned nothing then content would remain
> unchanged. Isn't there a built-in way to send an error string back and
> then catch it as a variable name?
>
> This is Py3 inside WSGI.
>
> -- Gnarlie
>   
Hi,

There's no need to rephrase an exception unless you want to *add* 
information.

def saveEvents(self,events):
    plistlib.writePlist(events, self.path+'/Data/Events.plist') 

try:   
    Data.Dict.SaveEvents(Data.Plist.Events)
except IOError, exc: # i'm using python 2.5, this except clause may have 
changed in py3
    content = str(exc)

JM

[toc] | [prev] | [next] | [standalone]


#15472

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2011-11-08 18:24 +0100
Message-ID<mailman.2546.1320773067.27778.python-list@python.org>
In reply to#15467
Jean-Michel Pichavant wrote:
> Gnarlodious wrote:
>> What I say is this:
>>
>> def SaveEvents(self,events):
>>    try:
>>       plistlib.writePlist(events, self.path+'/Data/Events.plist') #
>> None if OK
>>    except IOError:
>>       return "IOError: [Errno 13] Apache can't write Events.plist
>> file"
>>
>> Note that success returns"None" while failure returns a string.
>>
>> I catch the error like this:
>>
>> errorStatus=Data.Dict.SaveEvents(Data.Plist.Events)
>> if errorStatus: content=errorStatus
>>
>> It works, but isn there a more elegant way to do it? As in, one line?
>> I can imagine if success returned nothing then content would remain
>> unchanged. Isn't there a built-in way to send an error string back and
>> then catch it as a variable name?
>>
>> This is Py3 inside WSGI.
>>
>> -- Gnarlie
>>   
> Hi,
>
> There's no need to rephrase an exception unless you want to *add* 
> information.
>
> def saveEvents(self,events):
>    plistlib.writePlist(events, self.path+'/Data/Events.plist')
> try:      Data.Dict.SaveEvents(Data.Plist.Events)
> except IOError, exc: # i'm using python 2.5, this except clause may 
> have changed in py3
>    content = str(exc)
>
> JM
>
looks like for whatever reason the formating has gone crazy.
http://www.copypastecode.com/100088/

jm

[toc] | [prev] | [next] | [standalone]


#15478

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-11-08 22:16 +0000
Message-ID<4eb9aa41$0$29970$c3e8da3$5496439d@news.astraweb.com>
In reply to#15467
On Tue, 08 Nov 2011 05:20:51 -0800, Gnarlodious wrote:

> What I say is this:
> 
> def SaveEvents(self,events):
>    try:
>       plistlib.writePlist(events, self.path+'/Data/Events.plist') #
> None if OK
>    except IOError:
>       return "IOError: [Errno 13] Apache can't write Events.plist
> file"
> 
> Note that success returns"None" while failure returns a string.

I started off writing a sarcastic response about people who refuse to 
learn idiomatic Python and instead insist on writing (e.g.) Java or PHPP 
in Python. But then I eventually got to the *very last line* of your post 
where you noted that you were doing this in WSGI.

For future reference, when writing unidiomatic code *deliberately*, 
please say so up front, at the start of your message rather than at the 
end, and save your readers (or at least *me*) from jumping to the wrong 
conclusion.


Change the function to this:

def SaveEvents(self,events):
    try:
        plistlib.writePlist(events, self.path+'/Data/Events.plist')
        return ''
    except IOError as e:
        return str(e)
        # or if you prefer, your own custom error string
        # "IOError: [Errno 13] Apache can't write Events.plist file"


I return a string in both cases so that you can, if you choose, use the 
output of SaveEvents anywhere where a string is expected without worrying 
about whether it returns None or a string:

content = Data.Dict.SaveEvents(Data.Plist.Events)
# content will be the empty string if no error, otherwise error message.

If you don't care about that, just delete the return '' line and the 
function will fall back on returning None by default.

Either way, you can also use it like this:

content = Data.Dict.SaveEvents(Data.Plist.Events) or content

This will leave content unchanged if no error is returned, otherwise it 
will replace the value. Note that content must have an initial value to 
start with (presumably the empty string).


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#15492

FromGnarlodious <gnarlodious@gmail.com>
Date2011-11-08 17:48 -0800
Message-ID<d71afa52-8fe8-40cf-a80f-ae9e76984ee2@q35g2000prh.googlegroups.com>
In reply to#15478
On Nov 8, 3:16 pm, Steven D'Aprano wrote:

> content = Data.Dict.SaveEvents(Data.Plist.Events) or content
>
> This will leave content unchanged if no error is returned, otherwise it
> will replace the value.
Ah, the 'or' operator does it. Thank you, that is exactly what I was
looking for.

I should confess, I am not a programmer and don't even know what
"idiomatic" means in this context. I don't know Java or PHP, only a
little Forth from the HP calculator era. Advanced topics I just skip
over because I don't understand them. But I am learning slowly and
appreciate the help.

-- Rachel
http://Sectrum.com

[toc] | [prev] | [next] | [standalone]


#15494

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-11-09 02:57 +0000
Message-ID<4eb9ec18$0$29988$c3e8da3$5496439d@news.astraweb.com>
In reply to#15492
On Tue, 08 Nov 2011 17:48:57 -0800, Gnarlodious wrote:

> On Nov 8, 3:16 pm, Steven D'Aprano wrote:
> 
>> content = Data.Dict.SaveEvents(Data.Plist.Events) or content
>>
>> This will leave content unchanged if no error is returned, otherwise it
>> will replace the value.
> Ah, the 'or' operator does it. Thank you, that is exactly what I was
> looking for.
> 
> I should confess, I am not a programmer and don't even know what
> "idiomatic" means in this context. I don't know Java or PHP, only a
> little Forth from the HP calculator era. Advanced topics I just skip
> over because I don't understand them. But I am learning slowly and
> appreciate the help.

Idiomatic in this context means that the code follows standard styles, 
patterns or techniques commonly accepted by most good programmers of the 
language. This implies that the code works with the language, playing to 
its strengths, rather than against it.

Non-idiomatic code tends to be slower than it could be, or harder to 
read, or harder to maintain, or all three. Idiomatic is relative to the 
language: what is idiomatic for one language may not be for another.

For instance, if I were to ask "How do I do something with each item of a 
list?", the idiomatic way in Python would be:


for item in some_list:
    do_something_with(item)


rather than:

for i in range(len(some_list)):
    item = some_list[index]
    do_something_with(item)

and especially not this:

index = 0
while index < len(some_list):
    item = some_list[index]
    do_something_with(item)
    index += 1



Regards,



-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web