[PEAK] Re: dispatch rules based of the current scope.

Phillip J. Eby pje at telecommunity.com
Sun Mar 19 17:16:27 EST 2006


At 10:48 PM 3/19/2006 +0100, Elvelind Grandin wrote:
>On 3/19/06, Phillip J. Eby <pje at telecommunity.com> wrote:
> > At 08:08 PM 3/19/2006 +0100, Elvelind Grandin wrote:
> > >On 3/19/06, Phillip J. Eby <pje at telecommunity.com> wrote:
> > > > At 03:57 PM 3/19/2006 +0100, Elvelind Grandin wrote:
> > > > >Hi.
> > > > >
> > > > >One thing that supriced me in rulesdispatch was this.
> > > > >
> > > > >foo = generic()(foo)
> > > > >a = 1
> > > > >foo.when("a ==1")(print1)
> > > > >foo.when("a ==2")(print2)
> > > > >
> > > > >a = 2
> > > > >foo()
> > > > > >> 1
> > > > >
> > > > >is there any clever way of doing this besides putting a into the
> > > > >signature of the foo function?
> > > >
> > > > Yes; make it a zero argument callable, e.g. "lambda: a".  Zero-argument
> > > > callables are assumed to be dynamic; all other expressions that 
> *can* be
> > > > computed statically, *will* be.
> > >hm.
> > >do you mean make a  lambda as in a = lambda:  or use it in the .when call?
> >
> >
> > f = lambda: a
> >
> > when( f()==1 )
> >
>
>the problem as I understand is that I iether have to do x = lambda: x
>for all vars I would like to be able to use. which wont work. Or I
>have to do something like f = lambda: eval("rule")  which would screw
>up picking the moste specific rule.

I'm not following you here.  Are you trying to do some sort of dynamic rule 
compilation?  In that case you might wish to override the compilation 
machinery and fine-tune its compilation rules to suit your purposes.

Or, you could just do what I do, which is to define any dynamic globals as 
zero-argument callables (such as "get_request()"), and express your rules 
in terms of that API.  This is not only transparent but makes it clear to 
the user that computation is taking place to obtain the current request.  I 
find the "transparent thread-local proxy" pattern to be overly-magical, 
because it disguises the computation that is taking place and deceives the 
reader into thinking they can just assign to the variable to change it, 
when in fact it would screw up every other thread if they did so.




More information about the PEAK mailing list