[PEAK] Re: PEAK-rules question

Sébastien de Menten sdementen at gmail.com
Sun Sep 1 16:07:36 EDT 2013


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.eby-sarna.com/pipermail/peak/attachments/20130901/5fa006d5/attachment.html


More information about the PEAK mailing list