This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

The problem

Python has a few forms for decorators, you can have a plain simple decorator, with no arguments:

@mydecorator
def foo(): pass

Or a decorator with some arguments:

@mydecorator(1, 2)
def foo(): pass

You can even decorate a class:

@mydecorator
class Foo(object): pass

and each form is a little different to implement. This was frustrating if you wanted to create easy to use decorators where the developer didn’t need to worry about @mydecorator() working differently than @mydecorator.

decorators module

The decorators module allows you to easily create broad decorators that encompass all forms and all types (functions, methods, classes) using the same interface:

import decorators

class mydecorator(decorators.Decorator):
    def decorate_func(self, func, *dec_args, **dec_kwargs):
        def decorator(*args, *kwargs):
            print "You passed into the decorator these arguments", dec_args, dec_kwargs
            print "You passed into your function these arguments", args, kwargs
            print "Your function is", func
            return func(*args, **kwargs)

        return decorator

    def decorate_class(self, klass, *dec_args, **dec_kwargs):
        print "You passed into the decorator these arguments", dec_args, dec_kwargs
        print "Your class is", klass
        return klass

You can then use this decorator:

@mydecorator
def foo(): print "foo()"

@mydecorator(1, 2, boom="blam")
def bar(*args, **kwargs): print "bar()"

@mydecorator
class Baz(object): pass

@mydecorator(1, 2, boom="blam")
class Che(object): pass

Now, your decorator can decorate functions or classes, pass in arguments, or not, and you never have to worry about the subtle differences between the decorators, and best of all, you don’t have to duplicate code.

Other decorators

The Decorator class is good if you want to create a decorator that is totally flexible, if you want to enforce your decorator only being used for a function/method, you can use FuncDecorator. If you want to only decorate a class, use ClassDecorator, and if you want to decorate every instance of a class, use InstanceDecorator.

import decorators

class only_func(FuncDecorator):
    def decorate(self, func, *dec_a, **dec_kw):
        def decorator(*args, **kwargs):
            return func(*args, **kwargs)
        return decorator

# this will work
@only_func
def foo(): pass

# this will fail
@only_func
class Foo(object): pass

Installation

Use pip:

pip install decorators

Or, to get the latest and greatest from source:

pip install git+https://github.com/firstopinion/decorators#egg=decorators

License

MIT

Release History

Release History

0.1

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
decorators-0.1.tar.gz (3.9 kB) Copy SHA256 Checksum SHA256 Source Nov 13, 2013

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS HPE HPE Development Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting