• Stars
    star
    733
  • Rank 61,835 (Top 2 %)
  • Language
  • License
    Creative Commons ...
  • Created almost 9 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A curated list of awesome python decorator resources.

Awesome Python Decorator

A curated list of awesome python decorator resources.

You don't know decorator

Here are some resources about the python decorators.

Built-in python decorator

  • @abc.abstractmethod A decorator indicating abstract methods.
  • @abc.abstractproperty A subclass of the built-in property(), indicating an abstract property.
  • @asyncio.coroutine Decorator to mark generator-based coroutines. New in version 3.4.
  • @atexit.register Register func as a function to be executed at termination.
  • @classmethod Return a class method for function.
  • @contextlib.contextmanager Define a factory function for with statement context managers, without needing to create a class or separate __enter__() and __exit__() methods.
  • @functools.cached_property Transform a method of a class into a property whose value is computed once and then cached as a normal attribute for the life of the instance.
  • @functools.lru_cache Decorator to wrap a function with a memoizing callable that saves up to the maxsize most recent calls. It can save time when an expensive or I/O bound function is periodically called with the same arguments. New in python 3.2.
  • @functools.singledispatch Transforms a function into a single-dispatch generic function. New in python 3.4.
  • @functools.total_ordering Given a class defining one or more rich comparison ordering methods, this class decorator supplies the rest.
  • @functools.wraps This is a convenience function for invoking update_wrapper() as a function decorator when defining a wrapper function.
  • @property Return a property attribute.
  • @staticmethod Return a static method for function.
  • @types.coroutine This function transforms a generator function into a coroutine function which returns a generator-based coroutine. New in python 3.5.
  • @unittest.mock.patch Acts as a function decorator, class decorator or a context manager. Inside the body of the function or with statement, the target is patched with a new object. When the function/with statement exits the patch is undone. New in python 3.3.
  • @unittest.mock.patch.dict Patch a dictionary, or dictionary like object, and restore the dictionary to its original state after the test. New in python 3.3.
  • @unittest.mock.patch.multiple Perform multiple patches in a single call. New in python 3.3.
  • @unittest.mock.patch.object Patch the named member (attribute) on an object (target) with a mock object. New in python 3.3.
  • to be continue

Python decorator in the wild

Once you know the basics of how to use a decorator, you can take a look at some real projects to see how people use decorators in real life.

  • cached-property A decorator for caching properties in classes.
  • cachetools This module provides various memoizing collections and decorators, including variants of the Python 3 Standard Library @lru_cache function decorator.
  • celery Celery is a distributed task queue, it uses decorator to create tasks.
  • click Click is a Python package for creating beautiful command line interfaces, based on declaring commands through decorators. You can also check out the autocommand library which shares a similar idea with click.
  • deco A simplified parallel computing model for Python. DECO automatically parallelizes Python programs, and requires minimal modifications to existing serial programs.
  • decorator This is your best option if you want to preserve the signature of decorated functions in a consistent way across Python releases.
  • deprecated Use @deprecated decorator to deprecate old python classes, functions or methods. Decorators can also be used to add Sphinx directives.
  • fasteners Provides useful locks and decorators to lock/unlock around functions and methods.
  • flask Flask is a micro web framework in python, which uses decorators to route the URL, register error handler, register processor and so on.
  • flask-login Flask-Login provides user session management for Flask. For example, it use the login_required decorator to protect some views that need the user to be logged in.
  • GB2260.py This project is used to look up Chinese administrative divisions. It uses decorators to write Python 2.x and 3.x compatible code.
  • marshmallow A lightweight library for converting complex objects to and from simple Python datatypes. It use decorators to register schema pre-processing and post-processing methods.
  • me-api An extensible, personal API with custom integrations. It uses a decorator to make sure there is an access_token in the configuration file before sending the API request and reject another authentication if there is already an access_token.
  • numba Numba gives you the power to speed up your applications with high performance functions written directly in Python. It uses jit to decorate array-oriented and math-heavy Python code can be just-in-time compiled to native machine instructions, similar in performance to C, C++ and Fortran, without having to switch languages or Python interpreters.
  • lettuce Lettuce is a BDD tool for python. It uses step and steps to decorate functions and classes so they become new step definitions.
  • profilehooks Python decorators for profiling/tracing/timing a single function.
  • pypi-cli pypi-cli is a command-line interface to the Python Package Index. It use decorator to makes a property lazy-evaluated.
  • py-spin py-spin is a terminal spinner package for python. Yes, it does use a decorator to register a function that needs to take a long time to finish, as well as showing a spinner while it's processing!
  • python-goto A function decorator that rewrites the bytecode, enabling goto in python.
  • showme ShowMe is a simple set of extremely useful function decorators for Python. It allows you to view trace information, execution time, cputime, and function documentation.
  • tenacity Simplifies the task of adding retry behavior to just about anything. It originates from a fork of retrying.
  • timeout-decorator Timeout decorator for Python which uses a decorator to limit the execution time of the given function.
  • Tomorrow Tomorrow use decorator to do the magic that let you write asynchronous code with ease.
  • wrapt A Python module for decorators, wrappers and monkey patching.

Contribute

Please do! Create a project is not hard, while maintaining it is not easy. To make this project stay healthy and easy to maintain, what's more, to make it to be AWESOME, we need help. The best way in my opinion is watch this repository and join the daily discussions when some send a pull request and raise an issue. There are also many other ways to do the contribution, but please read the Contributing guide before stepping forward :)

Contributions are always welcome at any time! 🍰

License

CC0 1.0

More Repositories

1

tldr.py

A python client for tldr: simplified and community-driven man pages.
Python
186
star
2

py-spin

Terminal spinner package for Python. (This library is not the one from FLIR)
Python
182
star
3

pyhipku

Encode any IP address as a haiku, funny and lovely. TRY IT NOW: http://pyhipku.lord63.com
Python
91
star
4

choosealicense-cli

Bring http://choosealicense.com to your terminal
Python
72
star
5

isitup

Check whether a website is up or down.
Python
65
star
6

v2ex_daily_mission

V2EX 签到 python 版本实现。支持签到,查询连续登录天数,查询签到日志。
Python
42
star
7

licen

Generate your license. Yet another lice, but implement with Jinja2 and docopt.
Groff
38
star
8

pyemojify

Substitutes emoji aliases to emoji raw characters. Simple but sweet 😄
Python
35
star
9

wonderful_bing

Set bing's picture as wallpaper, support cinnamon, gnome, xfce, mate.
Python
22
star
10

wangyi_music_top100

网易云音乐歌单 TOP 100 . Try it now:
Python
15
star
11

me-api

An extensible, personal API with custom integrations. It's a python port.
Python
15
star
12

ascii_art

Draw ascii art(bar, chart, histogram) in your terminal.
Python
11
star
13

a_bunch_of_code

A bunch of code that I wrote before.
Python
9
star
14

zimuzu

字幕组 (http://www.zimuzu.tv) python 版本命令行签到工具.
Python
8
star
15

caniuse

Can I use this name for my python package on PyPI ?
Python
7
star
16

five.py

Gives you five, funny and lovely.
Python
6
star
17

figlet_web

Generate text to ASCII art! TRY IT NOW: http://text2ascii.lord63.com/
JavaScript
5
star
18

rb_colors

Yet another ruby library that deals with colors.
Ruby
1
star
19

zhihudaily

又一个知乎日报的网页版... https://zhihudaily.lord63.com
CSS
1
star