[PEAK] Rules clarification

Sergey Schetinin maluke at gmail.com
Thu Feb 28 02:02:08 EST 2008


It also seems that there's some bug triggered by declarations that
mean nothing, for example:

@abstract
def x(a):
    return a

@when(x, None) # <-- comment this line out and there will be no exception
def x_nonsense(a):
    foo

@when(x, (int,))
def x_int(a):
    return a+2

o = object()

@when(x, "a is o") # <-- this trows an exception
def xo(a):
    print '!'

-----

Traceback (most recent call last):
  File "test_peak_rules.py", line 25, in <module>
    @when(x, "a is o")
  File "c:\python25\lib\site-packages\DecoratorTools-1.7-py2.5.egg\peak\util\decorators.py",
line 601, in do_decorate
    frame, getattr(f,'__name__',None), f, frame.f_locals
  File "c:\python25\lib\site-packages\peak_rules-0.3-py2.5.egg\peak\rules\core.py",
line 196, in callback
    register_for_class(None)
  File "c:\python25\lib\site-packages\peak_rules-0.3-py2.5.egg\peak\rules\core.py",
line 189, in register_for_class
    rules.add(parse_rule(engine, pred, context, cls))
  File "c:\python25\lib\site-packages\peak_rules-0.3-py2.5.egg\peak\rules\core.py",
line 65, in parse_rule
    def parse_rule(engine, predicate, context, cls):
  File "c:\python25\lib\site-packages\peak_rules-0.3-py2.5.egg\peak\rules\core.py",
line 547, in callback
    return f(*args)
  File "c:\python25\lib\site-packages\peak_rules-0.3-py2.5.egg\peak\rules\core.py",
line 153, in __call__
    return self.body(*args, **kw)
  File "c:\python25\lib\site-packages\peak_rules-0.3-py2.5.egg\peak\rules\core.py",
line 804, in parse_string_rule_by_upgrade
    Dispatching(engine.function).create_engine(IndexedEngine),
  File "c:\python25\lib\site-packages\peak_rules-0.3-py2.5.egg\peak\rules\core.py",
line 347, in create_engine
    self.engine = engine_type(self)
  File "c:\python25\lib\site-packages\peak_rules-0.3-py2.5.egg\peak\rules\predicates.py",
line 212, in __init__
    super(IndexedEngine, self).__init__(disp)
  File "c:\python25\lib\site-packages\peak_rules-0.3-py2.5.egg\peak\rules\core.py",
line 424, in __init__
    self.rules.subscribe(self)
  File "<peak.util.decorators.wrap wrapping peak.rules.core.subscribe
at 0x00B07CF0>", line 14, in subscribe
  File "c:\python25\lib\site-packages\peak_rules-0.3-py2.5.egg\peak\rules\core.py",
line 303, in subscribe
    listener.actions_changed(frozenset(self), empty)
  File "<peak.util.decorators.wrap wrapping
peak.rules.core.actions_changed at 0x00B09DB0>", line 14, in
actions_changed
  File "c:\python25\lib\site-packages\peak_rules-0.3-py2.5.egg\peak\rules\core.py",
line 433, in actions_changed
    self._add_method(rule.predicate, rule)
  File "c:\python25\lib\site-packages\peak_rules-0.3-py2.5.egg\peak\rules\predicates.py",
line 216, in _add_method
    signature = Signature(tests_for(signature, self))
  File "c:\python25\lib\site-packages\peak_rules-0.3-py2.5.egg\peak\rules\criteria.py",
line 321, in tests_for
    def tests_for(ob, engine=None):
  File "c:\python25\lib\site-packages\peak_rules-0.3-py2.5.egg\peak\rules\core.py",
line 547, in callback
    return f(*args)
  File "c:\python25\lib\site-packages\peak_rules-0.3-py2.5.egg\peak\rules\core.py",
line 214, in __call__
    raise self.__class__(*self.args+(args,kw))  # XXX
peak.rules.core.NoApplicableMethods: ((None,
<peak.rules.predicates.IndexedEngine object at 0x00B1AE70>), {})


On Thu, Feb 28, 2008 at 7:36 AM, Sergey Schetinin <maluke at gmail.com> wrote:
> After poking around the code a bit I see that an appropriate
>  implementation of that seems to be:
>
>  @when(implies, (object, type))
>  def type_implies(s1,s2):
>     return implies(s1, (s2,))
>
>
>
>
>  On Thu, Feb 28, 2008 at 7:21 AM, Sergey Schetinin <maluke at gmail.com> wrote:
>  >
>  > Thanks for your reply.
>  >   I think it would be reasonable to assume that if the second parameter
>  >   to <when> is a type it should be treated the same as a tuple with that
>  >   single value. This should illustrate what I'm talking about:
>  >
>  >   from peak.rules import *
>  >
>  >   @around(when, (object, type)) # this shouldn't be necessary IMO
>  >   def when_type(func, type, *args):
>  >     return when(func, (type,), *args)
>  >
>  >   @abstract
>  >   def x(a):
>  >     return a
>  >
>  >   @when(x, int)
>  >   def xint(a):
>  >     return a+1
>  >
>  >   assert x(1) == 2
>  >
>  >
>  >
>  >
>  >   On Thu, Feb 28, 2008 at 6:56 AM, Phillip J. Eby <pje at telecommunity.com> wrote:
>  >   > Sorry I didn't reply to this sooner; I meant to think about it before
>  >   >  replying and then it scrolled out of my immediate view and I
>  >  forgot about it.
>  >   >
>  >   >
>  >   >
>  >   >  At 06:47 PM 2/15/2008 +0200, Sergey Schetinin wrote:
>  >   >  >I've noticed that "when" function accepts anything as it's second
>  >   >  >parameter (not just tuples),
>  >   >
>  >   >  Yes; all that is required is that it be something which can "imply" a
>  >   >  type tuple (i.e. using the peak.rules.implies() function).
>  >   >
>  >   >
>  >   >
>  >   >  >  what does this declaration would mean
>  >   >  >then (maybe nothing and it should throw an exception?)
>  >   >  >
>  >   >  > >>> from peak.rules import *
>  >   >  > >>> @abstract
>  >   >  >... def test(foo):
>  >   >  >...     pass
>  >   >  >...
>  >   >  > >>> @when(test, int) # <--------- this is what I'm talking about
>  >   >  >... def test_int(foo):
>  >   >  >...     print foo
>  >   >  >...
>  >   >
>  >   >  It indeed means nothing.  Whether it should throw an exception is a
>  >   >  more complex problem.  Right now, the default for testing implication
>  >   >  between two different types is to say that neither implies the
>  >   >  other.  This is a sensible default, except for where it's misleading
>  >   >  in this case.  So I could add a specific rule to catch it and throw
>  >   >  an exception, but I'm wondering how many such special cases would
>  >  need adding.
>  >   >
>  >   >
>  >
>  >
>  >
>  >
>  >
>  >  --
>  >   Best Regards,
>  >   Sergey Schetinin
>  >
>  >   http://s3bk.com/ -- S3 Backup
>  >   http://word-to-html.com/ -- Word to HTML Converter
>  >
>  >
>  >
>  >  --
>  >  Best Regards,
>  >  Sergey Schetinin
>  >
>  >  http://s3bk.com/ -- S3 Backup
>  >  http://word-to-html.com/ -- Word to HTML Converter
>  >
>
>
>
>  --
>
>
> Best Regards,
>  Sergey Schetinin
>
>  http://s3bk.com/ -- S3 Backup
>  http://word-to-html.com/ -- Word to HTML Converter
>



-- 
Best Regards,
Sergey Schetinin

http://s3bk.com/ -- S3 Backup
http://word-to-html.com/ -- Word to HTML Converter



More information about the PEAK mailing list