<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:SimSun;
        panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
@font-face
        {font-family:"\@SimSun";
        panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p
        {mso-style-priority:99;
        mso-margin-top-alt:auto;
        margin-right:0in;
        mso-margin-bottom-alt:auto;
        margin-left:0in;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
code
        {mso-style-priority:99;
        font-family:"Courier New";}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0in;
        margin-right:0in;
        margin-bottom:0in;
        margin-left:.5in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
span.EmailStyle19
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-GB" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">> From: Joshua Klein [mailto:mobiusklein@gmail.com]
<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">> […] When assigning to the class itself, not the module, the new<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">> comparator function is called<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">Yay, that worked, learning something new every day. Thanks a million.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">Peter: the ultimate goal of that request was to be able to call sort() on features, with sometimes different and very custom sort criteria. Nothing which would
 fit BioPython really.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">Best,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">  Bastien<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Calibri",sans-serif;color:#008FD5">--
<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</span><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p></o:p></span></p>
</div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif"> Joshua Klein [mailto:mobiusklein@gmail.com]
<br>
<b>Sent:</b> Tuesday, January 31, 2017 7:48 AM<br>
<b>To:</b> Peter Cock <p.j.a.cock@googlemail.com><br>
<b>Cc:</b> Chevreux, Bastien <bastien.chevreux@dsm.com>; biopython@biopython.org<br>
<b>Subject:</b> Re: [Biopython] Cannot make SeqFeature() comparable?<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-bottom:12.0pt">--- This mail has been sent from an external source ---
<o:p></o:p></p>
<div>
<div>
<div>
<p style="margin:1.2em!important">The reason the original code snippet doesn’t seem to be working as expected is that the
<code><span style="font-size:10.0pt;font-family:Consolas;border:solid #EAEAEA 1.0pt;padding:0in;background:#F8F8F8">cmp1</span></code> function is assigned to the
<code><span style="font-size:10.0pt;font-family:Consolas;border:solid #EAEAEA 1.0pt;padding:0in;background:#F8F8F8">__lt__</span></code> attribute of the
<code><span style="font-size:10.0pt;font-family:Consolas;border:solid #EAEAEA 1.0pt;padding:0in;background:#F8F8F8">SeqFeature</span></code> module, not the
<code><span style="font-size:10.0pt;font-family:Consolas;border:solid #EAEAEA 1.0pt;padding:0in;background:#F8F8F8">SeqFeature</span></code> class, which is located at
<code><span style="font-size:10.0pt;font-family:Consolas;border:solid #EAEAEA 1.0pt;padding:0in;background:#F8F8F8">SeqFeature.SeqFeature</span></code>. When assigning to the class itself, not the module, the new comparator function is called.<o:p></o:p></p>
<p style="margin: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.<o:p></o:p></p>
<div>
<p class="MsoNormal"><span style="font-size:1.0pt">​<o:p></o:p></span></p>
</div>
</div>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">On Tue, Jan 31, 2017 at 4:07 AM, Peter Cock <<a href="mailto:p.j.a.cock@googlemail.com" target="_blank">p.j.a.cock@googlemail.com</a>> wrote:<o:p></o:p></p>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<p class="MsoNormal">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<o:p></o:p></p>
<div>
<div>
<p class="MsoNormal"><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>
> ------------------------------------------------------------------<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(SeqFeature.FeatureLocation(10, 200));<br>
><br>
> f2 = SeqFeature.SeqFeature(SeqFeature.FeatureLocation(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>
> ------------------------------------------------------------------<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>
> ------------------------------------------------------------------<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>
> ------------------------------------------------------------------<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>
> ________________________________<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>
><o:p></o:p></p>
</div>
</div>
<p class="MsoNormal">> _______________________________________________<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" target="_blank">
http://mailman.open-bio.org/mailman/listinfo/biopython</a><br>
_______________________________________________<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" target="_blank">http://mailman.open-bio.org/mailman/listinfo/biopython</a><o:p></o:p></p>
</blockquote>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</div>
</body>
</html>