Skip to main content

This module provides four decorators:

prioritized_when

prioritized_around

prioritized_before

prioritized_after

These behave like their peak.rules counterparts except that they accept an optional prio argument which can be used to provide a comparable object (usually an integer) 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 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 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,), prio=1)(jsonify_Person3) # doctest: +ELLIPSIS
<function jsonify_Person3 at ...>
>>> jsonify(Person())
Don't be so smart, I am, my prio is higher!

For convenience, a generic decorator is provided too which behaves like peak.rules.dispatch.generic except that the when,…,``after`` decorators that will be bound as attributes of the decorated function will be prioritized:

>>> @generic
... def f(n): pass

>>> f(5)
Traceback (most recent call last):
    ...
NoApplicableMethods: ((5,), {})

Add a default rule:

>>> @f.when()
... def default_f(n):
...     return n
>>> f(5)
5

Add a default rule that overrides the former:

>>> @f.when(prio=1)
... def new_default_f(n):
...     return n+1
>>> f(5)
6

Release files for prioritized_methods 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for prioritized_methods 0.2.1
File Size Uploaded
prioritized_methods-0.2.1.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for prioritized_methods 0.2.1
File Interpreter ABI Platform
prioritized_methods-0.2.1-py2.5.egg Legacy Egg format - - Details

Total release size: 14.5 kB

Release files / prioritized_methods-0.2.1.tar.gz

Download URL prioritized_methods-0.2.1.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
931da40e1ba53c6d35312371b3c84573f1a5297a0ee6a78584a2337d0180b63c
BLAKE2b-256 checksum
How to use checksums
e497ffc61ba866fc7b3d2da41793bb98dc4e59665e060948d4d67dc944983a0c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / prioritized_methods-0.2.1-py2.5.egg

Download URL prioritized_methods-0.2.1-py2.5.egg
Size 9.8 kB
Tags Egg
SHA-256 checksum
How to use checksums
d94f1545c4896f01ccf2b5004deb1beed721fa4afa64e52b8ae548b79bc6f1d5
BLAKE2b-256 checksum
How to use checksums
79e5a7322c099953a7d0d35ec761f1d71d6c79b5c29f41c39dee8cc2cf51ac6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2

2 release files

0.1.3

2 release files

0.1.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page