An extension to PEAK-Rules to prioritize methods in order to to avoid AmbiguousMethods situations
Project description
A :class:`PrioritizedMethod` and :class:`PrioritizedAround`
:class:`peak.rules.Method` subclasses which can disambiguate ambiguous methods
with a ``prio`` attribute in the Method's body (ie: the function being
decorated with ``when``/``around``).
This module provides two decorators, ``prioritized_around`` and
``prioritized_when`` which behave like their ``peak.rules``
counterparts except that they accept an optional ``prio`` argument which can
be used to provide a comparable object that will be used to disambiguate
situations in which more than rule applies to the given arguments and no rule
is more specific than another. That is, situations in which an
:exc:`peak.rules.AmbiguousMethods` would have been raised.
This is useful for libraries which want to be extensible via generic functions
but want their users to easily override a method without figuring out how to
write a more specific rule or when it is not feasible.
For example, TurboJson provides a ``jsonify`` function that looks like this::
>>> def jsonify(obj):
... "jsonify an object"
And extends it so it can handle SqlAlchemy mapped classes in a way
similar to this one::
>>> from peak.rules import when
>>> def jsonify_sa(obj):
... print "You're a SA object and I'm going to jsonify you!"
>>> when(jsonify, "hasattr(obj, 'c')")(jsonify_sa) # doctest: +ELLIPSIS
<function jsonify_sa at ...>
>>> class Person(object):
... def __init__(self):
... self.c = "im a stub"
>>> jsonify(Person())
You're a SA object and I'm going to jsonify you!
So far so good, however, when a user of the library wants to override the built
in implementation it can become quite hard since they have to write a more
specific rule which can be tedious, for example:
hasattr(self, 'c') and isinstance(obj, Person)
Notice the ``hasattr`` test, even though ``isinstance(obj, Person)`` implies it,
just to make it more specific than the built in, this gets more cumbersome the
more complicated the expression becomes.
Else this is what happens::
>>> def jsonify_Person(obj):
... print "No way, I'm going to jsonify you!"
>>> when(jsonify, (Person,))(jsonify_Person) # doctest: +ELLIPSIS
<function jsonify_Person at ...>
>>> try:
... jsonify(Person())
... except AmbiguousMethods:
... print "I told you, gfs can sometimes be a pain"
I told you, gfs can sometimes be a pain
To remedy this situation ``prioritized_when`` can be used to provide an
implementation that will override the one declared with ``when``::
>>> def jsonify_Person2(obj):
... print "No way, I'm going to jsonify you!"
>>> prioritized_when(jsonify, (Person,))(jsonify_Person2) # doctest: +ELLIPSIS
<function jsonify_Person2 at ...>
>>> jsonify(Person())
No way, I'm going to jsonify you!
Notice that we didn't need a ``prio`` argument. This is because methods
decorated with ``prioritized_when`` always override those that have been
decorated with ``peak.rules.when``.
Methods decorated with ``prioritized_when`` can also override other methods
that have been decorated by the same decorator using the ``prio`` parameter,
the one which compares greater wins, if both are equal
:exc:`AmbiguousMethods` will be raised as usual.
>>> def jsonify_Person3(obj):
... print "Don't be so smart, I am, my prio is higher!"
>>> prioritized_when(jsonify, (Person,), 1)(jsonify_Person3) # doctest: +ELLIPSIS
<function jsonify_Person3 at ...>
>>> jsonify(Person())
Don't be so smart, I am, my prio is higher!
:class:`peak.rules.Method` subclasses which can disambiguate ambiguous methods
with a ``prio`` attribute in the Method's body (ie: the function being
decorated with ``when``/``around``).
This module provides two decorators, ``prioritized_around`` and
``prioritized_when`` which behave like their ``peak.rules``
counterparts except that they accept an optional ``prio`` argument which can
be used to provide a comparable object that will be used to disambiguate
situations in which more than rule applies to the given arguments and no rule
is more specific than another. That is, situations in which an
:exc:`peak.rules.AmbiguousMethods` would have been raised.
This is useful for libraries which want to be extensible via generic functions
but want their users to easily override a method without figuring out how to
write a more specific rule or when it is not feasible.
For example, TurboJson provides a ``jsonify`` function that looks like this::
>>> def jsonify(obj):
... "jsonify an object"
And extends it so it can handle SqlAlchemy mapped classes in a way
similar to this one::
>>> from peak.rules import when
>>> def jsonify_sa(obj):
... print "You're a SA object and I'm going to jsonify you!"
>>> when(jsonify, "hasattr(obj, 'c')")(jsonify_sa) # doctest: +ELLIPSIS
<function jsonify_sa at ...>
>>> class Person(object):
... def __init__(self):
... self.c = "im a stub"
>>> jsonify(Person())
You're a SA object and I'm going to jsonify you!
So far so good, however, when a user of the library wants to override the built
in implementation it can become quite hard since they have to write a more
specific rule which can be tedious, for example:
hasattr(self, 'c') and isinstance(obj, Person)
Notice the ``hasattr`` test, even though ``isinstance(obj, Person)`` implies it,
just to make it more specific than the built in, this gets more cumbersome the
more complicated the expression becomes.
Else this is what happens::
>>> def jsonify_Person(obj):
... print "No way, I'm going to jsonify you!"
>>> when(jsonify, (Person,))(jsonify_Person) # doctest: +ELLIPSIS
<function jsonify_Person at ...>
>>> try:
... jsonify(Person())
... except AmbiguousMethods:
... print "I told you, gfs can sometimes be a pain"
I told you, gfs can sometimes be a pain
To remedy this situation ``prioritized_when`` can be used to provide an
implementation that will override the one declared with ``when``::
>>> def jsonify_Person2(obj):
... print "No way, I'm going to jsonify you!"
>>> prioritized_when(jsonify, (Person,))(jsonify_Person2) # doctest: +ELLIPSIS
<function jsonify_Person2 at ...>
>>> jsonify(Person())
No way, I'm going to jsonify you!
Notice that we didn't need a ``prio`` argument. This is because methods
decorated with ``prioritized_when`` always override those that have been
decorated with ``peak.rules.when``.
Methods decorated with ``prioritized_when`` can also override other methods
that have been decorated by the same decorator using the ``prio`` parameter,
the one which compares greater wins, if both are equal
:exc:`AmbiguousMethods` will be raised as usual.
>>> def jsonify_Person3(obj):
... print "Don't be so smart, I am, my prio is higher!"
>>> prioritized_when(jsonify, (Person,), 1)(jsonify_Person3) # doctest: +ELLIPSIS
<function jsonify_Person3 at ...>
>>> jsonify(Person())
Don't be so smart, I am, my prio is higher!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for prioritized_methods-0.1.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40ce0661214a6bfce49178c908cc4c30a93cb29adf7dd32bd822ae152780c2fb |
|
MD5 | 1ed6f94e588a1e4f7a52e02dfaea05fb |
|
BLAKE2b-256 | 9a8ffa1a60b2adf9fc0bd63cfbd2f6114486e36c8ab22a39c19e7fad7bdad0b6 |
Close
Hashes for prioritized_methods-0.1.2-py2.5.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11a0c47796d05e3636510f97fe9d28efe627a7ef05c64ea431049b415332ba8c |
|
MD5 | 28166e365450194b95407aac81791fbd |
|
BLAKE2b-256 | bcb2c48d92b93665d15359a8c6c968a23dd5e4b7e804ddf25d25c728f1bd0d79 |