Skip to main content

Tool for writing simple decorators.

Project description

https://travis-ci.org/zeekay/decorum.png?branch=master

Decorum is a simple tool which aims to make it easier to write flexible and simple decorators. It can also act similarly to functools.wraps.

Typical usage looks like this:

>>> from __future__ import print_function
>>> from decorum import Decorum

>>> class my_decorator(Decorum):
...    def wraps(self, f):
...        print("I'm returning the function! You can keep it!")
...        return super(my_decorator, self).wraps(f)

Decorum lets you write decorators with and without arguments in a unified way. Your decorator can be used with or without arguments, called or not, and it will work the same way:

>>> @my_decorator
... def foo(x):
...     print(x)
I'm returning the function! You can keep it!

>>> foo('bar')
bar

Is identical to:

>>> @my_decorator()
... def foo(x):
...     print(x)
I'm returning the function! You can keep it!

Writing decorators

In order to write your own decorators, just subclass decorum.Decorum.

There are only three methods to be aware of when writing your own decorators:

  • init() setups the decorator itself;

  • wraps() decorates the function;

  • call() runs the wrapped function and returns result.

Here is a slightly fancier example:

>>> from decorum import Decorum

>>> class fancy(Decorum):
...     def init(self, arg=None, *args, **kwargs):
...         super(fancy, self).init(*args, **kwargs)
...         self.arg = arg
...
...     def wraps(self, f):
...         if self.arg:
...             newf = lambda: self.arg
...         else:
...             newf = lambda: 'wut'
...         return super(fancy, self).wraps(newf)
...
...     def call(self, *args, **kwargs):
...         result = super(fancy, self).call(*args, **kwargs)
...         return result.upper()

>>> @fancy
... def foo():
...     pass

>>> foo()
'WUT'

>>> @fancy('woof')
... def foo():
...     pass

>>> foo()
'WOOF'

By default decorum will try to keep assign certain attributes to the wrapped function for you, namely __doc__ and __name__.

>>> import decorum

>>> class identity(Decorum):
...     """Noop decorator: does nothing!"""

>>> @identity
... def my_function():
...     """My function's docstring."""

>>> print(my_function.__name__)
my_function
>>> print(my_function.__doc__)
My function's docstring.

The optional assigned keyword argument can be used to specify which attributes of the original function are assigned directly to the matching attributes on the wrapper function. This defaults to functools.WRAPPER_ASSIGNMENTS. You can specify False or None to disable this.

>>> @identity(assigned=None)
... def my_function():
...     """My function's docstring."""
>>> print(my_function.__name__)
identity
>>> print(my_function.__doc__)
Noop decorator: does nothing!

Testing decorators

Decorum makes it easy to test custom decorators.

Assert a function has been decorated as expected:

>>> assert isinstance(my_function, Decorum)
>>> assert isinstance(my_function, identity)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Decorum-1.0.1.tar.gz (6.0 kB view details)

Uploaded Source

File details

Details for the file Decorum-1.0.1.tar.gz.

File metadata

  • Download URL: Decorum-1.0.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Decorum-1.0.1.tar.gz
Algorithm Hash digest
SHA256 908cfc8a3a55bd6e67060309d41cc46deca18bc2e687f877973269146aef845b
MD5 efb052b1d6673459b09a893aeed012b1
BLAKE2b-256 990b1106b6f19a9aff7d9d8f534ee53cb3299ad45bebfb5fa1465aacb3f07a15

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page