Re: PEAK-rules question

Sébastien de Menten <[email protected]> Sun, 1 Sep 2013 22:07:36 +0200
Newsgroups gmane.comp.python.peak
Message-ID <CAB2pxDvvBPmhdW_oFKn=bfai56KN7g2YAz5XFbd9PbivHjTU1A@mail.gmail.com>
--===============1897937002==
Content-Type: multipart/alternative; boundary=e89a8f6436d2c55daf04e5580149

--e89a8f6436d2c55daf04e5580149
Content-Type: text/plain; charset=ISO-8859-1

Hello,

After some research on the topic, I found a simple way to do what i wanted
with macropy.

I define the following macro in a module

# macro_module.py
from macropy.core.macros import Macros, Str, unparse

macros = Macros()

@macros.expr
def s(tree, **kw):
    return Str(unparse(tree))
#===========

and then use it as:

# test.py
from macro_module import macros, s
from peak.rules import abstract, when

@abstract
def foo(a,b):
  pass
foo.when = lambda condition: when(foo, condition)   #<== this could be
better implemented in the abstract decorator

@foo.when(s[a>4 and b<56])
def foo_cond1(a,b):
  return a+b
#===========

The code is processed automatically with all s[...] transformed in "..."
like:
@foo.when("a>4 and b<56")

which is exactly what i needed (even though there is a dependency on
macropy).

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.
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.
Now the generic functions I want to write are like :
 - "optimal_acceleration(car, weather)"
 - "maintenance_planning(car)"
 - "customer_match(car, customer_profile)"
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 !

I hope this analogy is clearer than my original description.


Sebastien

--e89a8f6436d2c55daf04e5580149
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div><div><div><div><div><div><div><div><div><div>Hello,<b=
r><br></div>After some research on the topic, I found a simple way to do wh=
at i wanted with macropy.<br></div><br>I define the following macro in a mo=
dule<br>
<br># macro_module.py<br>from macropy.core.macros import Macros, Str, unpar=
se<br><br>macros =3D Macros()<br><br>@macros.expr<br>def s(tree, **kw):<br>=
=A0=A0=A0 return Str(unparse(tree))<br>#=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D<b=
r><br></div>and then use it as:<br>
<br></div><div># test.py<br></div>from macro_module import macros, s<br></d=
iv><div>from peak.rules import abstract, when<br></div><div><br>@abstract<b=
r></div>def foo(a,b):<br></div>=A0 pass<br></div>foo.when =3D lambda condit=
ion: when(foo, condition)=A0=A0 #&lt;=3D=3D this could be better implemente=
d in the abstract decorator<br>
<br></div>@foo.when(s[a&gt;4 and b&lt;56])<br></div>def foo_cond1(a,b):<br>=
</div>=A0 return a+b<br><div><div><div><div>#=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D<br><br><div><div><div>The code is processed automatically with all s[..=
.] transformed in &quot;...&quot; like:<br>
@foo.when(&quot;a&gt;4 and b&lt;56&quot;)<br><br></div><div></div><div>whic=
h is exactly what i needed (even though there is a dependency on macropy).<=
br><br></div><div class=3D"gmail_extra">For my second question, explaining =
the real domain space would need pages so I must find a &#39;to-the-point&q=
uot; analogy that captures the gist of the problem. So here is one that doe=
s not use domain-specific terms but maps 100% with the problem.<br>
</div><div class=3D"gmail_extra">I have different kind of cars (pickup, sed=
ans, convertible, etc) which have different transmissions (4x4, traction, p=
ropulsion) and different tires (all season, snow, high performance, etc) an=
d all combinations of Car x Transmission x Tires are possible.<br>
</div><div class=3D"gmail_extra">Now the generic functions I want to write =
are like : <br>=A0- &quot;optimal_acceleration(car, weather)&quot; <br></di=
v><div class=3D"gmail_extra">=A0- &quot;maintenance_planning(car)&quot;<br>=
</div>
<div class=3D"gmail_extra">=A0- &quot;customer_match(car, customer_profile)=
&quot;<br></div><div class=3D"gmail_extra">These functions may depend on th=
e 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 fu=
nctions for these new combinations (if they make sense ... otherwise, an ex=
ception NoApplicableMethods is perfect !<br>
<br></div><div class=3D"gmail_extra">I hope this analogy is clearer than my=
 original description.<br><br><br></div><div class=3D"gmail_extra">Sebastie=
n<br><div class=3D"gmail_quote"><br></div></div></div></div></div></div></d=
iv>
</div></div>

--e89a8f6436d2c55daf04e5580149--

--===============1897937002==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
PEAK mailing list
[email protected]
http://www.eby-sarna.com/mailman/listinfo/peak
--===============1897937002==--