Complex 'compare'

Frank Millman frank at chagford.com
Tue Dec 18 06:38:35 EST 2018


"Chris Angelico"  wrote in message 
news:CAPTjJmpLuyFf04AT+34VraJ5itDvNySVJspEv=DdWDSMMSF88w at mail.gmail.com...
>
> On Tue, Dec 18, 2018 at 9:52 PM Frank Millman <frank at chagford.com> wrote:
> > I need to know if one row is greater than or less than the other. The 
> > sort
> > sequence can be complex - one or more columns, each of which can be 
> > sorted
> > ascending or descending.
> >
> > Below is the function I have come up with. Can anyone see any problem 
> > with
> > it, or suggest a better way to do it?
>
> I'm not sure what the difference is between "compare_type" and "desc".
> You have four options here:
>
> * gt, desc
>     True if source_col is None else source_col < target_col
> * gt, not desc
>     False if source_col is None else source_col > target_col
> * lt, desc
>     False if source_col is None else source_col > target_col
> * lt, not desc
>     True if source_col is None else source_col < target_col
>
> The way this is currently coded, these come in two perfect pairs.
>

Yes, now that you point it out, there is definitely scope for shortening the 
code, as that is effectively duplication.

My main concern was whether my algorithm had any flaws in it. I will keep 
testing to look for corner cases.

I can assume total ordering. The background is that I want to find the 
position of a row in a table according to some sequence. I can't read in the 
entire table, as it could contain millions of rows. So I create a 
server-side cursor in the desired sequence, and then perform a binary search 
on it. So far it is working well.

Thanks

Frank





More information about the Python-list mailing list