Simple (hopefuly useful) Proxy (as in the GoF design pattern) implementation for Python
Python
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
docs
proxy_tools
.gitignore
.travis.yml
LICENSE.txt
README.rst
development_requirements.txt
setup.py
tests.py
tox.ini

README.rst

proxy_tools

Did you every want to have @property on a module? ...

Build Status

Loveingly extracted from werkzeug. A very useful proxy implementation, that I found to be useful outside the web context -- hence the extraction.

Impetus

I was working on a module and I wanted it to have a @property like you can do on objects. No dice. I found an elegant implementation within werkzeug with request and session and the like. So I extracted it so we can use it for our non-werkzeug projects.

For more on the nitty gritty on why this works, checkout this post

Install

pip install proxy_tools

Basic Usage

# your_module/__init__.py
from proxy_tools import module_property

@module_property
def current_user():
    return User.find_by_id(request['user_id'])

# Then elsewhere
from your_module import current_user
print(current_user.name)

Alternative Syntax

from proxy_tools import Proxy

def get_current_user():
    return User.find_by_id(request['user_id'])

current_user = Proxy(get_current_user)

Questions / Issues

Feel free to ping me on twitter: @tushman or add issues or PRs at https://github.com/jtushman/proxy_tools

Thank you

To Armin Ronacher and the werkzeug team for their thought leadership and excellent work