[Zope] dtml-lesson learned...

Andreas Kostyrka andreas@kostyrka.priv.at
30 Aug 2002 21:40:17 +0200


Am Die, 2002-08-20 um 16.28 schrieb Paul Winkler:
> On Tue, Aug 20, 2002 at 07:01:02AM -0700, Paul Winkler wrote:
> > return context.title.split('#')[1]
> 
> 
> ... and to follow up on your latest discovery - 
> always do error checking - I'd do it like this:
What about this (no try/except):
(title_parts.split("#",1)*2)[1]

Another trick to avoid IndexErrors in python is slicing:
Instead of:
try:
  x=a[1]
except IndexError:
  x=None

one can write:
x=(a[1:1]+[None,])[0]

Andreas

> 
> try:
>     title_parts = context.title.split('#', maxsplit=1)[1]
>     return title_parts[1]
> 
> except IndexError:   # there is no '#' in the title
>     return title
> 
> --
> 
> Paul Winkler
> "Welcome to Muppet Labs, where the future is made - today!"