Skip to content

Instantly share code, notes, and snippets.

@shvechikov
Forked from ei-grad/local.py
Created June 18, 2014 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shvechikov/60bcde02e67892f63d5f to your computer and use it in GitHub Desktop.
Save shvechikov/60bcde02e67892f63d5f to your computer and use it in GitHub Desktop.
def my_local(init):
key = object()
def getter():
t = _app_ctx_stack.top
l = getattr(t, 'my_locals')
if l is None:
t.my_locals = l = {}
if key not in l:
l[key] = init()
return l[key]
return LocalProxy(getter)
db = my_local(get_database)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment