<span style="font-family:arial,helvetica,sans-serif">Hello,</span><br>
<div><font face="arial,helvetica,sans-serif"> </font></div>
<div><font face="arial,helvetica,sans-serif">I have two questions on PEAK-rules where I would more than happy to have your opinion/advice.</font></div>
<p><font face="arial,helvetica,sans-serif">First the simple one: when using the @when(multimethod_function, &quot;condition in a string&quot;) decorator, i find the &quot;condition in string&quot; syntax slightly annoying as it is not recognised by the IDE or other code inspector (flake, etc). For the IDE point, it is not very readable as there is not syntax coloring and the autocompletion feature (and other refactoring features) does not work ideally. For the code inspector, it detects falsely that some imports are unused as they are in fact only used in the string. </font></p>


<p><font face="arial,helvetica,sans-serif"> Could there be an alternative? I was thinking along in the lines of a when_inside decorator working in the following spirit:</font></p>
<blockquote style="MARGIN-RIGHT:0px" dir="ltr">
<blockquote style="MARGIN-RIGHT:0px" dir="ltr">
<p><font face="arial,helvetica,sans-serif">from peak.rules import when, abstract</font></p>
<p><font face="arial,helvetica,sans-serif">@abstract </font>
</p><div><font face="arial,helvetica,sans-serif">def foo(a, b):</font></div>
<div><font face="arial,helvetica,sans-serif">    pass</font></div>
<p><font face="arial,helvetica,sans-serif">@when(foo, &quot;a&gt;4 and b&lt;56&quot;) </font>
</p><div><font face="arial,helvetica,sans-serif">def foo(a, b):</font></div>
<div><font face="arial,helvetica,sans-serif">    &quot;This function does blabla&quot;</font></div>
<div><font face="arial,helvetica,sans-serif">     </font></div>
<div><font face="arial,helvetica,sans-serif">    for i in range(5):</font></div>
<div><font face="arial,helvetica,sans-serif">        print i</font></div>
<div><font face="arial,helvetica,sans-serif">         </font></div>
<p><font face="arial,helvetica,sans-serif">@<strong>when_inside</strong>(foo)  # this would be equivalent to the previous definition </font>
</p><div><font face="arial,helvetica,sans-serif">def foo(a, b):</font></div>
<div><font face="arial,helvetica,sans-serif">    &quot;This function does blabla&quot;</font></div>
<div><font face="arial,helvetica,sans-serif">    <strong>a&gt;5 and b&lt;56</strong>     # the condition that normally is the 2nd argument of when is the first line of code (maybe with an &quot;assert a&gt;5 and b&lt;56&quot; if it helps)</font></div>


<div><font face="arial,helvetica,sans-serif">     </font></div>
<div><font face="arial,helvetica,sans-serif">    for i in range(5):</font></div>
<div><font face="arial,helvetica,sans-serif">        print i </font>
<div><font face="arial,helvetica,sans-serif"> </font></div>
<div><font face="arial,helvetica,sans-serif"><strong>@foo.when</strong>     # this would be even simpler and still equivalent to the previous definition</font></div>
<div><font face="arial,helvetica,sans-serif">def foo(a, b):</font></div>
<div><font face="arial,helvetica,sans-serif">    &quot;This function does blabla&quot;</font></div>
<div><font face="arial,helvetica,sans-serif">    <strong>a&gt;5 and b&lt;56</strong>     # the condition that normally is the 2nd argument of when is the first line of code (maybe with an &quot;assert a&gt;5 and b&lt;56&quot; if it helps)</font></div>

</div>
<div><font face="arial,helvetica,sans-serif">    for i in range(5): </font>
<div><font face="arial,helvetica,sans-serif">        print i</font></div></div><p></p><p></p><p></p></blockquote></blockquote>
<div dir="ltr"><font face="arial,helvetica,sans-serif">The decorator would thus extract and analyse the first code line of the function (maybe using the meta package (</font><a href="https://pypi.python.org/pypi/meta" target="_blank"><font face="arial,helvetica,sans-serif">https://pypi.python.org/pypi/meta</font></a><font face="arial,helvetica,sans-serif">) with meta.dump_python_source(meta.decompile(foo.func_code)).split(&quot;\n&quot;)[1] to get it)) to get the condition.</font></div>


<div dir="ltr"><font face="arial,helvetica,sans-serif">In your opinion, is this meaningful/useful ? but also doable ?</font></div>
<div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><span style="FONT-FAMILY:&#39;Times New Roman&#39;,&#39;serif&#39;"><font face="arial,helvetica,sans-serif"></font></span> </div>
<div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><span style="FONT-FAMILY:&#39;Times New Roman&#39;,&#39;serif&#39;"><font face="arial,helvetica,sans-serif">My second question is related to the use case for peak rules.</font></span></div>


<div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><span style="FONT-FAMILY:&#39;Times New Roman&#39;,&#39;serif&#39;"><font face="arial,helvetica,sans-serif">I am using peak-rules in a project essentially to replace standard object methods in a system with a lot of composition of objects with inheritance. So I have a class Operation which instances have two attributes which are instances of two other classes Source and Destination. So the &quot;abstract&quot; class looks like:</font></span></div>


<div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><span style="FONT-FAMILY:&#39;Times New Roman&#39;,&#39;serif&#39;"><font face="arial,helvetica,sans-serif">Class Operation:</font></span></div>
<div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><span style="FONT-FAMILY:&#39;Times New Roman&#39;,&#39;serif&#39;"><font face="arial,helvetica,sans-serif"><span>            </span>source = None (but should be an instance of Source)</font></span></div>


<div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><span style="FONT-FAMILY:&#39;Times New Roman&#39;,&#39;serif&#39;"><font face="arial,helvetica,sans-serif"><span>            </span>destination = None (but should be an instance of Destination)</font></span></div>


<div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><span style="FONT-FAMILY:&#39;Times New Roman&#39;,&#39;serif&#39;"><font face="arial,helvetica,sans-serif">then I can subclass Operation to add new attributes/meaning for the class and the same for Source and Destination.</font></span></div>


<div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><span style="FONT-FAMILY:&#39;Times New Roman&#39;,&#39;serif&#39;"><font face="arial,helvetica,sans-serif">After that, I have an engine that must interpret (=do calculation on) the Operation with its Source and Destination and the interpretation is dependent on the 3 objects (operation, source and destination).</font></span></div>


<div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><span style="FONT-FAMILY:&#39;Times New Roman&#39;,&#39;serif&#39;"><font face="arial,helvetica,sans-serif">I use peak.rules to dispatch to the correct algorithm in function of the conditions like:</font></span></div>


<div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><span style="FONT-FAMILY:&#39;Courier New&#39;"><font face="arial,helvetica,sans-serif">@abstract</font></span></div>
<div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><span style="FONT-FAMILY:&#39;Courier New&#39;"><font face="arial,helvetica,sans-serif">def interpret(operation):</font></span></div>
<div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><span style="FONT-FAMILY:&#39;Courier New&#39;"><font face="arial,helvetica,sans-serif">    pass</font></span></div>
<div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><span style="FONT-FAMILY:&#39;Courier New&#39;"><font face="arial,helvetica,sans-serif"></font></span> </div>
<div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><span style="FONT-FAMILY:&#39;Courier New&#39;"><font face="arial,helvetica,sans-serif">@when(interpret, “isinstance(operation, SubOperationOfTypeZ) and isinstance(operation.source, SubTypeWOfSource) and operation.source.date_of_creation.month &lt; 5”)</font></span></div>


<div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><span style="FONT-FAMILY:&#39;Courier New&#39;"><font face="arial,helvetica,sans-serif">def interpret(operation):</font></span></div>
<div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><span style="FONT-FAMILY:&#39;Courier New&#39;"><font face="arial,helvetica,sans-serif">    return “blabla”</font></span></div>
<div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><span style="FONT-FAMILY:&#39;Courier New&#39;"><font face="Arial"></font></span> </div>
<div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><span style="FONT-FAMILY:&#39;Courier New&#39;"><font face="Arial">Is this usage of peak.rules OK ? would you use a different pattern ? My feeling it is that it is one of the use of generic functions methods ... but i have no practical experience with them. </font></span></div>
<div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><br></div><div style="LINE-HEIGHT:normal;MARGIN:0cm 0cm 10pt" class="MsoNormal"><span style="FONT-FAMILY:&#39;Courier New&#39;"><font face="Arial">Sebastien</font></span></div>

<br>