What's new with Gnosis

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Mertz

    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:



    Or



    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
    _/_/_/ IN A WORLD W/O WALLS, THERE WOULD BE NO GATES \_\_\_ z e


  • John J. Lee

    #2
    Re: What's new with Gnosis

    "Raymond Hettinger" <vze4rx4y@veriz on.net> writes:
    [...][color=blue]
    > ACT I ---------------------------------------[color=green][color=darkred]
    > >>> s = list('abc')
    > >>> try:[/color][/color]
    > ... result = s['a']
    > ... except IndexError, TypeError:
    > ... print 'Not found'
    > ...
    >
    > Traceback (most recent call last):
    > File "<pyshell#1 1>", line 2, in -toplevel-
    > result = s['a']
    > TypeError: list indices must be integers[/color]

    The second 'argument' of except is the caught exception object, so
    that code works as if you did

    try:
    result = s['a']
    except IndexError, e:
    TypeError = e
    print 'Not found'


    -- which doesn't catch the TypeError.


    [color=blue]
    > ACT II --------------------------------------------[color=green][color=darkred]
    > >>> class MyMistake(Excep tion):[/color][/color]
    > ... pass
    >[color=green][color=darkred]
    > >>> try:[/color][/color]
    > ... raise MyMistake, 'try, try again'
    > ... except MyMistake, msg:
    > ... print type(msg)
    > ...
    >
    > <type 'instance'>[/color]

    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:

    try:
    result = s['a']
    except IndexError, e:
    print e


    because print calls the e.__str__ method to do its work.

    [color=blue]
    > ACT III --------------------------------------------[color=green][color=darkred]
    > >>> class Prohibited(Exce ption):[/color][/color]
    > ... def __init__(self):
    > ... print 'This class of should never get initialized'
    > ...[color=green][color=darkred]
    > >>> raise Prohibited()[/color][/color]
    > This class of should never get initialized
    >
    > Traceback (most recent call last):
    > File "<pyshell#4 0>", line 1, in -toplevel-
    > raise Prohibited()
    > Prohibited: <unprintable instance object>[color=green][color=darkred]
    > >>> raise Prohibited[/color][/color]
    > This class of should never get initialized
    >
    > Traceback (most recent call last):
    > File "<pyshell#4 1>", line 1, in -toplevel-
    > raise Prohibited
    > Prohibited: <unprintable instance object>[/color]

    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.__ini t__.]

    class Prohibited(Exce ption):
    def __init__(self, *args):
    Exception.__ini t__(self, *args)
    print 'This class will get initialized'


    In this case, the lack of that Exception.__ini t__ call is what's
    causing the "<unprintab le 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...]


    [color=blue]
    > ACT IV -----------------------------------------------[color=green][color=darkred]
    > >>> module = 'Root'
    > >>> try:[/color][/color]
    > ... 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[/color]

    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
    try:
    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


    [color=blue]
    > ACT V -----------------------------------------------[color=green][color=darkred]
    > >>> try:[/color][/color]
    > ... raise KeyError('Canno t find key')
    > ... except LookupError, msg:
    > ... print 'Lookup:', msg
    > ... except OverflowError, msg:
    > ... print 'Overflow:', msg
    > ... except KeyError, msg:
    > ... print 'Key:', msg
    >
    >
    > Lookup: 'Cannot find key'[/color]

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

    isinstance(rais ed_exception, caught_exceptio n)

    so exceptions can be caught by their base classes. LookupError is a
    base class of KeyError, so the first except matches.


    John

    Comment

    • Bruno Desthuilliers

      #3
      Re: What's new with Gnosis [wrong thread ?]

      John J. Lee wrote:[color=blue]
      > "Raymond Hettinger" <vze4rx4y@veriz on.net> writes:
      > [...]
      >[color=green]
      >>ACT I ---------------------------------------
      >>[/color][/color]


      (snip)

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

      Bruno

      Comment

      • John J. Lee

        #4
        Re: What's new with Gnosis [wrong thread ?]

        Bruno Desthuilliers <bdesth.nospam@ removeme.free.f r> writes:
        [color=blue]
        > John J. Lee wrote:[color=green]
        > > "Raymond Hettinger" <vze4rx4y@veriz on.net> writes:
        > > [...]
        > >[color=darkred]
        > >>ACT I ---------------------------------------
        > >>[/color][/color]
        >
        >
        > (snip)
        >
        > John,
        > is it my newsreader going mad, or did you post in the wrong thread ?[/color]

        I posted in the wrong thread.

        Something about Gnus... haven't figured out why I occasionally do that yet.


        John

        Comment

        Working...