472,989 Members | 2,855 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

What's new with Gnosis

At the suggestion of one of my correspondents, I slightly reluctantly
implemented an RSS feed for my website/writing. It is perhaps a bit
crude so far, but maybe I'll spiff it up. The RSS also has an HTML
front to it.

If you want to see the latest news about my _Charming Python_ or _XML
Matters_ columns, or about other articles (later, perhaps stuff about
my book), take a look at:




I'm new to this RSS stuff... so let me know (gently) if I've done
anything terribly wrong.

mertz@ _/_/_/_/ THIS MESSAGE WAS BROUGHT TO YOU BY: \_\_\_\_ n o
gnosis _/_/ Postmodern Enterprises \_\_
..cx _/_/ \_\_ d o
Jul 18 '05 #1
3 2336
"Raymond Hettinger" <vz******@verizon.net> writes:
ACT I ---------------------------------------
s = list('abc')
try: ... result = s['a']
... except IndexError, TypeError:
... print 'Not found'

Traceback (most recent call last):
File "<pyshell#11>", line 2, in -toplevel-
result = s['a']
TypeError: list indices must be integers
The second 'argument' of except is the caught exception object, so
that code works as if you did

result = s['a']
except IndexError, e:
TypeError = e
print 'Not found'
-- which doesn't catch the TypeError.
ACT II -------------------------------------------- class MyMistake(Exception): ... pass
try: ... raise MyMistake, 'try, try again'
... except MyMistake, msg:
... print type(msg)

<type 'instance'>
Again, the second 'argument' of except gets the exception object, not
the string you used in the raise.

These two are equivalent [XXX er, I *think* they're always equivalent]:

raise SomeError, 'my error message'
raise SomeError('my error message')

The second form is more explicit, hence better.

Exception objects do have a __str__ method, though, so you can print
them as if they were strings:

result = s['a']
except IndexError, e:
print e
because print calls the e.__str__ method to do its work.

ACT III -------------------------------------------- class Prohibited(Exception): ... def __init__(self):
... print 'This class of should never get initialized'
... raise Prohibited() This class of should never get initialized

Traceback (most recent call last):
File "<pyshell#40>", line 1, in -toplevel-
raise Prohibited()
Prohibited: <unprintable instance object> raise Prohibited This class of should never get initialized

Traceback (most recent call last):
File "<pyshell#41>", line 1, in -toplevel-
raise Prohibited
Prohibited: <unprintable instance object>
These are equivalent:

raise Exception
raise Exception()

The second is more explicit, hence better.

Since exception classes always end up getting instantiated whichever
way you write the raise statement, you need to write __init__
correctly if you override it. The Exception base class has an
__init__ that it expects to be called, so you should call it:

[At this point, I had to look up the arguments to Exception.__init__.]

class Prohibited(Exception):
def __init__(self, *args):
Exception.__init__(self, *args)
print 'This class will get initialized'
In this case, the lack of that Exception.__init__ call is what's
causing the "<unprintable instance object>" message, but it could
cause other problems too.

[Actually, with your interactive example, I don't get the
"<unprintable..." bit -- maybe that's in 2.3b2...]
ACT IV ----------------------------------------------- module = 'Root'
try: ... raise module + 'Error'
... except 'LeafError':
... print 'Need leaves'
... except 'RootError':
... print 'Need soil'
... except:
... print 'Not sure what is needed'

Not sure what is needed
A raised string exception only matches the string in the except
statment if both strings are the same object. It's not enough for
them to have the same value. Two different string literals that have
the same value aren't necessarily the same object:

myerror = "myerror"
anothererror = "myerror" # not *necessarily* the same object as myerror
raise myerror
except anothererror:
# we only get here if it so happens that:
assert myerror is anothererror
except myerror:
# we always get here, because:
assert myerror is myerror
ACT V ----------------------------------------------- try:

... raise KeyError('Cannot find key')
... except LookupError, msg:
... print 'Lookup:', msg
... except OverflowError, msg:
... print 'Overflow:', msg
... except KeyError, msg:
... print 'Key:', msg
Lookup: 'Cannot find key'

except statements are checked in the order you write them. Class
instance exceptions are matched as if by

isinstance(raised_exception, caught_exception)

so exceptions can be caught by their base classes. LookupError is a
base class of KeyError, so the first except matches.
Jul 18 '05 #2
John J. Lee wrote:
"Raymond Hettinger" <vz******@verizon.net> writes:
ACT I ---------------------------------------


is it my newsreader going mad, or did you post in the wrong thread ?


Jul 18 '05 #3
Bruno Desthuilliers <bd***********@removeme.free.fr> writes:
John J. Lee wrote:
"Raymond Hettinger" <vz******@verizon.net> writes:
ACT I ---------------------------------------


is it my newsreader going mad, or did you post in the wrong thread ?

I posted in the wrong thread.

Something about Gnus... haven't figured out why I occasionally do that yet.
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: thecrow | last post by:
Alright, what the hell is going on here? In the following code, I expect the printed result to be: DEBUG: frank's last name is burns. Instead, what I get is: DEBUG: frank's last name is...
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
by: Mike Henley | last post by:
I first came across rebol a while ago; it seemed interesting but then i was put off by its proprietary nature, although the core of the language is a free download. Recently however, i can't...
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
by: David Mertz, Ph.D. | last post by:
Python/XML users might be interested in: http://www-106.ibm.com/developerworks/xml/library/x-matters39.html Title: Get the most out of gnosis.xml.objectify Subtitle: Use utility functions...
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
by: hacker1017 | last post by:
im just asking out of curiosity.
by: Katie Tam | last post by:
I am new to this filed and begin to learn this langague. Can you tell me the good books to start with ? Katie Tam Network administrator http://www.linkwaves.com/main.asp...
by: Wang, Harry | last post by:
The gnosis xml libs should not be version specific, but when I try to use Python 2.5, I am getting "not well formed (invalid token)" errors. Harry
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.