<div dir="ltr"><div class="markdown-here-wrapper" style=""><p style="margin:0px 0px 1.2em!important">The reason the original code snippet doesn’t seem to be working as expected is that the <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">cmp1</code> function is assigned to the <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">__lt__</code> attribute of the <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">SeqFeature</code> module, not the <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">SeqFeature</code> class, which is located at <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">SeqFeature.SeqFeature</code>. When assigning to the class itself, not the module, the new comparator function is called.</p>
<p style="margin:0px 0px 1.2em!important">This sort of patching works differently for old-style and new-style classes, having to do with how special methods are looked up. Old style classes look up special methods on the instance, new style classes look them up on the instance’s class.</p>
<div title="MDH:VGhlIHJlYXNvbiB0aGUgb3JpZ2luYWwgY29kZSBzbmlwcGV0IGRvZXNuJ3Qgc2VlbSB0byBiZSB3
b3JraW5nIGFzIGV4cGVjdGVkIGlzIHRoYXQgdGhlIGBjbXAxYCBmdW5jdGlvbiBpcyBhc3NpZ25l
ZCB0byB0aGUgYF9fbHRfX2AgYXR0cmlidXRlIG9mIHRoZSBgU2VxRmVhdHVyZWAgbW9kdWxlLCBu
b3QgdGhlIGBTZXFGZWF0dXJlYCBjbGFzcywgd2hpY2ggaXMgbG9jYXRlZCBhdCBgU2VxRmVhdHVy
ZS5TZXFGZWF0dXJlYC4gV2hlbiBhc3NpZ25pbmcgdG8gdGhlIGNsYXNzIGl0c2VsZiwgbm90IHRo
ZSBtb2R1bGUsIHRoZSBuZXcgY29tcGFyYXRvciBmdW5jdGlvbiBpcyBjYWxsZWQuPGRpdj48YnI+
PC9kaXY+PGRpdj5UaGlzIHNvcnQgb2YgcGF0Y2hpbmcgd29ya3MgZGlmZmVyZW50bHkgZm9yIG9s
ZC1zdHlsZSBhbmQgbmV3LXN0eWxlIGNsYXNzZXMsIGhhdmluZyB0byBkbyB3aXRoIGhvdyBzcGVj
aWFsIG1ldGhvZHMgYXJlIGxvb2tlZCB1cC4gT2xkIHN0eWxlIGNsYXNzZXMgbG9vayB1cCBzcGVj
aWFsIG1ldGhvZHMgb24gdGhlIGluc3RhbmNlLCBuZXcgc3R5bGUgY2xhc3NlcyBsb29rIHRoZW0g
dXAgb24gdGhlIGluc3RhbmNlJ3MgY2xhc3MuPGRpdj48YnI+PC9kaXY+PGRpdj48YnI+PC9kaXY+
PC9kaXY+" style="height:0;width:0;max-height:0;max-width:0;overflow:hidden;font-size:0em;padding:0;margin:0">​</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jan 31, 2017 at 4:07 AM, Peter Cock <span dir="ltr"><<a href="mailto:p.j.a.cock@googlemail.com" target="_blank">p.j.a.cock@googlemail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Bastien,<br>
<br>
I'm not immediately sure if "monkey patching" the class<br>
methods at run time like that would work in principle.<br>
If you insert a print into it, it does not seem to be invoked.<br>
<br>
It might be worth trying a modified Biopython, or an<br>
explicit subclass to narrow down where this breaks.<br>
<br>
Or more simply, can you just do the start position<br>
comparison explicitly if that's what you want to use?<br>
<br>
f1.location.start < f2.location.start<br>
<br>
Peter<br>
<div><div class="h5"><br>
<br>
On Mon, Jan 30, 2017 at 11:05 PM, Chevreux, Bastien<br>
<<a href="mailto:bastien.chevreux@dsm.com">bastien.chevreux@dsm.com</a>> wrote:<br>
> Hi there,<br>
><br>
><br>
><br>
> I have a problem making the SeqFeature() class comparable by providing a<br>
> __lt__ function. Consider the following:<br>
><br>
><br>
><br>
> ------------------------------<wbr>------------------------------<wbr>------<br>
><br>
> #!/usr/bin/env python3<br>
><br>
><br>
><br>
> from Bio import SeqFeature<br>
><br>
><br>
><br>
> def cmp1(this,other):<br>
><br>
>     return int(this.location.start) < int(other.location.start);<br>
><br>
><br>
><br>
> SeqFeature.__lt__=cmp1;<br>
><br>
> f1 = SeqFeature.SeqFeature(<wbr>SeqFeature.FeatureLocation(10, 200));<br>
><br>
> f2 = SeqFeature.SeqFeature(<wbr>SeqFeature.FeatureLocation(<wbr>1000, 1200));<br>
><br>
><br>
><br>
> if f1<f2:<br>
><br>
>     print("f1<f2");<br>
><br>
> else:<br>
><br>
>     print("nope, f1>=f2");<br>
><br>
> ------------------------------<wbr>------------------------------<wbr>------<br>
><br>
><br>
><br>
> The code above runs with an error message:<br>
><br>
>     if f1<f2:<br>
><br>
> TypeError: unorderable types: SeqFeature() < SeqFeature()<br>
><br>
><br>
><br>
> What I do not understand is that this should be the canonical recipe for<br>
> making any class comparable via LT operator. Compare to the following code<br>
> which runs without problems:<br>
><br>
><br>
><br>
> ------------------------------<wbr>------------------------------<wbr>------<br>
><br>
> #!/usr/bin/env python3<br>
><br>
><br>
><br>
> class myclass():<br>
><br>
>     def __init__(self, value):<br>
><br>
>         self.bla=value;<br>
><br>
><br>
><br>
> def cmp2(this,other):<br>
><br>
>     return this.bla < other.bla;<br>
><br>
><br>
><br>
> myclass.__lt__=cmp2;<br>
><br>
> m1=myclass(1);<br>
><br>
> m2=myclass(2);<br>
><br>
><br>
><br>
> if m1<m2:<br>
><br>
>     print("m1<m2");<br>
><br>
> else:<br>
><br>
>     print("nope, m1>=m2");<br>
><br>
> ------------------------------<wbr>------------------------------<wbr>------<br>
><br>
><br>
><br>
> What am I missing?<br>
><br>
><br>
><br>
> Best,<br>
><br>
>   Bastien<br>
><br>
><br>
><br>
> --<br>
> DSM Nutritional Products Microbia Inc | Bioinformatics<br>
> 60 Westview Street | Lexington, MA 02421 | United States<br>
> Phone +1 781 259 7613 | Fax +1 781 259 0615<br>
><br>
><br>
><br>
><br>
> ______________________________<wbr>__<br>
><br>
> DISCLAIMER:<br>
> This e-mail is for the intended recipient only.<br>
> If you have received it by mistake please let us know by reply and then<br>
> delete it from your system; access, disclosure, copying, distribution or<br>
> reliance on any of it by anyone else is prohibited.<br>
> If you as intended recipient have received this e-mail incorrectly, please<br>
> notify the sender (via e-mail) immediately.<br>
><br>
</div></div>> ______________________________<wbr>_________________<br>
> Biopython mailing list  -  <a href="mailto:Biopython@mailman.open-bio.org">Biopython@mailman.open-bio.org</a><br>
> <a href="http://mailman.open-bio.org/mailman/listinfo/biopython" rel="noreferrer" target="_blank">http://mailman.open-bio.org/<wbr>mailman/listinfo/biopython</a><br>
______________________________<wbr>_________________<br>
Biopython mailing list  -  <a href="mailto:Biopython@mailman.open-bio.org">Biopython@mailman.open-bio.org</a><br>
<a href="http://mailman.open-bio.org/mailman/listinfo/biopython" rel="noreferrer" target="_blank">http://mailman.open-bio.org/<wbr>mailman/listinfo/biopython</a><br>
</blockquote></div><br></div>