Skip to main content

Tool for writing simple decorators

Project description

=======
Decorum
=======

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 decorum import decorator

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

Decorum makes lets you write decorators 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

Is identical to:

@my_decorator()
def foo(x): print x

Writing decorators
==================

Decorum provides two easy ways to write your own decorators. You can use `decorum.decorator` to
decorate decorator classes, or you can directly subclass decorum.Decorum. There are only
two methods to be aware of when writing your own decorators. Define a `wraps` method to handle
the actual decoration and return the decorated function, and optionally define an `init` method
to handle any arguments you want to accept, and handle basic setup (it's called before decoration
by `__init__`, you can use it in a similar fashion to a real `__init__` method). By default
decorum will try to keep assign certain attributes to the wrapped function for you, namely `__doc__`
and `__name__`. You can set `keep_attrs` to None to turn this off, or provide it with a list of
attributes you want applied to the returned decorated function.

Here is a slightly fancier example:

from decorum import decorator

@decorator
class fancy:
def init(self, arg=None):
self.arg = arg

def wraps(self, f):
if self.arg:
def newf():
print self.arg
else:
def newf():
print 'wut'
return newf

@fancy
def foo():
pass
foo()

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

# prints
wut
woof

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-0.4.1.tar.gz (2.1 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for Decorum-0.4.1.tar.gz
Algorithm Hash digest
SHA256 4c33892acde6d2f0cb24c47f0738a8d03361a458ddf7a1209b1ee0c05045d5cf
MD5 cb61fc898b982ce9040afda1b2b37be7
BLAKE2b-256 1c61ee9bf4bc89cd21805c447edccfffab598b6e9f66d45fbb5bbe0ff44648be

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