Help catching error message

G

Gnarlodious

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
 
J

Jean-Michel Pichavant

Gnarlodious said:
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
 
J

Jean-Michel Pichavant

Jean-Michel Pichavant said:
Gnarlodious said:
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
 
S

Steven D'Aprano

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).
 
G

Gnarlodious

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
 
S

Steven D'Aprano

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top