<div dir="ltr"><div><div><div><div><div><div><div><div><div><div>Hello,<br><br></div>After some research on the topic, I found a simple way to do what i wanted with macropy.<br></div><br>I define the following macro in a module<br>
<br># macro_module.py<br>from macropy.core.macros import Macros, Str, unparse<br><br>macros = Macros()<br><br>@macros.expr<br>def s(tree, **kw):<br> return Str(unparse(tree))<br>#===========<br><br></div>and then use it as:<br>
<br></div><div># test.py<br></div>from macro_module import macros, s<br></div><div>from peak.rules import abstract, when<br></div><div><br>@abstract<br></div>def foo(a,b):<br></div> pass<br></div>foo.when = lambda condition: when(foo, condition) #<== this could be better implemented in the abstract decorator<br>
<br></div>@foo.when(s[a>4 and b<56])<br></div>def foo_cond1(a,b):<br></div> return a+b<br><div><div><div><div>#===========<br><br><div><div><div>The code is processed automatically with all s[...] transformed in "..." like:<br>
@foo.when("a>4 and b<56")<br><br></div><div></div><div>which is exactly what i needed (even though there is a dependency on macropy).<br><br></div><div class="gmail_extra">For my second question, explaining the real domain space would need pages so I must find a 'to-the-point" analogy that captures the gist of the problem. So here is one that does not use domain-specific terms but maps 100% with the problem.<br>
</div><div class="gmail_extra">I have different kind of cars (pickup, sedans, convertible, etc) which have different transmissions (4x4, traction, propulsion) and different tires (all season, snow, high performance, etc) and all combinations of Car x Transmission x Tires are possible.<br>
</div><div class="gmail_extra">Now the generic functions I want to write are like : <br> - "optimal_acceleration(car, weather)" <br></div><div class="gmail_extra"> - "maintenance_planning(car)"<br></div>
<div class="gmail_extra"> - "customer_match(car, customer_profile)"<br></div><div class="gmail_extra">These functions may depend on the Car(Transmission, Tire) combination and while all may not be supported, I want to be able to add support for any of them. Moreover, if a user adds a new kind of transmission, or car, or tire, I want to be able to add the functions for these new combinations (if they make sense ... otherwise, an exception NoApplicableMethods is perfect !<br>
<br></div><div class="gmail_extra">I hope this analogy is clearer than my original description.<br><br><br></div><div class="gmail_extra">Sebastien<br><div class="gmail_quote"><br></div></div></div></div></div></div></div>
</div></div>