Re: Comparison problem

2005-11-28 Thread Peter Hansen
Tim Henderson wrote: > peter (Thanks for clarifying to whom you were responding... I saw the other post but wouldn't have responded since it didn't seem to be in response to one of mine. :-) ) > would not the more correct way to do this be short circuit > evaluation. somthing along lines of >

Re: Comparison problem

2005-11-27 Thread Tim Henderson
peter would not the more correct way to do this be short circuit evaluation. somthing along lines of if (len(item) > 0) and (item[0] == '-'): pass seems to be the more correct approach. rather than relying on slicing to return an empty string. For instance suppose you wanted to test to see if th

Re: Comparison problem

2005-11-27 Thread Tim Henderson
of course the more correct way is most likely the use of short circuit evaluation. so somthing along lines of if (len(item) > 0) and (item[0] == '-'): pass would probably be the correct approach. -- http://mail.python.org/mailman/listinfo/python-list

Re: Comparison problem

2005-11-27 Thread Peter Hansen
Fredrik Lundh wrote: > Peter Hansen wrote: >>Actually, it's not so much baroque as it is safe... item[0] will fail if >>the string is empty, while item[0:1] will return '' in that case. >> >>Of course, as you point out, .startswith() is the better approach anyway. > > $ timeit -s "s = 'abc'" "s[:1

Re: Comparison problem

2005-11-27 Thread [EMAIL PROTECTED]
I think no matter what language you programs it, it is hard to understand. Can you break it up into sub-problems first ? Like first parsing the inventory file into a python dict, then also the fields from web to another dict ? Chris wrote: > Hi, > > I'm new to python, and I'm trying to write a sma

Re: Comparison problem

2005-11-27 Thread Fredrik Lundh
Peter Hansen wrote: > Actually, it's not so much baroque as it is safe... item[0] will fail if > the string is empty, while item[0:1] will return '' in that case. > > Of course, as you point out, .startswith() is the better approach anyway. $ timeit -s "s = 'abc'" "s[:1] == 'a'" 100 loops, be

Re: Comparison problem

2005-11-26 Thread Alex Martelli
Tom Anderson <[EMAIL PROTECTED]> wrote: ... > But, more importantly, egad! What's the thinking behind having slicing > behave like that? Anyone got any ideas? What's the use case, as seems to > be the fashionable way of putting it these days? :) Slicing has always been "soft" (it's OK to specif

Re: Comparison problem

2005-11-26 Thread Peter Hansen
Tom Anderson wrote: > On Sat, 26 Nov 2005, Peter Hansen wrote: >>Tom Anderson wrote: >>>On Sat, 26 Nov 2005, Chris wrote: if item[0:1]=="-": >>> >>>item[0:1] seems a rather baroque way of writing item[0]! I'd actually >>>suggest writing this line like this: >> >>Actually, it's not so much ba

Re: Comparison problem

2005-11-26 Thread Tom Anderson
On Sat, 26 Nov 2005, Peter Hansen wrote: > Tom Anderson wrote: >> On Sat, 26 Nov 2005, Chris wrote: >> >>> if item[0:1]=="-": >> >> item[0:1] seems a rather baroque way of writing item[0]! I'd actually >> suggest writing this line like this: > > Actually, it's not so much baroque as it is saf

Re: Comparison problem

2005-11-26 Thread Peter Hansen
Tom Anderson wrote: > On Sat, 26 Nov 2005, Chris wrote: > >> if item[0:1]=="-": > > item[0:1] seems a rather baroque way of writing item[0]! I'd actually > suggest writing this line like this: Actually, it's not so much baroque as it is safe... item[0] will fail if the string is empty, while

Re: Comparison problem

2005-11-26 Thread Tom Anderson
Chris, as well as addressing what i think is causing your problem, i'm going to point out some bits of your code that i think could be polished a little. It's intended in a spirit of constructive criticism, so i hope you don't mind! On Sat, 26 Nov 2005, Chris wrote: >if item[0:1]=="-": it