dbf.py API question concerning Index.index_search()

Hans Mulder hansmu at xs4all.nl
Thu Aug 16 06:34:35 EDT 2012


On 16/08/12 01:26:09, Ethan Furman wrote:
> Indexes have a new method (rebirth of an old one, really):
> 
>   .index_search(
>      match,
>      start=None,
>      stop=None,
>      nearest=False,
>      partial=False )
> 
> The defaults are to search the entire index for exact matches and raise
> NotFoundError if it can't find anything.
> 
> match is the search criteria
> start and stop is the range to search in
> nearest returns where the match should be instead of raising an error
> partial will find partial matches
> 
> The question is what should the return value be?
> 
> I don't like the usual pattern of -1 meaning not found (as in
> 'nothere'.find('a')), so I thought a fun and interesting way would be to
> subclass long and override the __nonzero__ method to return True/False
> based on whether the (partial) match was found.  The main problems I see
> here is that the special return value reverts to a normal int/long if
> anything is done to it (adding, subtracting, etc), and the found status
> is lost.
> 
> The other option is returning a (number, bool) tuple -- safer, yet more
> boring... ;)

I think you should go for the safe boring option, because in many use
cases the caller will need to known whether the number you're returning
is the index of a match or just the nearest non-match.  The caller could
redo the match to find out.  But you have already done the match, so you
might as well tell them the result.


Hope this helps,

-- HansM




More information about the Python-list mailing list