Did you every want to have @property on a module? ...
Loveingly extracted from werkzeug. A very useful proxy implementation, that I found to be useful outside the web context -- hence the extraction.
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
pip install proxy_tools
# 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)
Feel free to ping me on twitter: @tushman or add issues or PRs at https://github.com/jtushman/proxy_tools
To Armin Ronacher and the werkzeug team for their thought leadership and excellent work