Skip to content

Commit 9ddb607

Browse files
committed
Provider example in readme, fixed ivankorobkov#4.
1 parent bae6739 commit 9ddb607

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

README.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,33 @@ Other scopes such as a request scope or a session scope are fragile, introduce h
132132
and are difficult to test. In ``python-inject`` write custom providers which can be thread-local,
133133
request-local, etc.
134134

135+
For example, a thread-local current user provider::
136+
137+
import inject
138+
import threading
139+
140+
# Given a user class.
141+
class User(object):
142+
pass
143+
144+
# Create a thread-local current user storage.
145+
_LOCAL = threading.local()
146+
147+
def get_current_user():
148+
return getattr(_LOCAL, 'user', None)
149+
150+
def set_current_user(user):
151+
_LOCAL.user = user
152+
153+
# Bind a user to a custom provider.
154+
inject.configure(lambda binder: binder.bind_to_provider(User, get_current_user))
155+
156+
# Inject the current user.
157+
@inject.param('user', User)
158+
def foo(user):
159+
pass
160+
161+
135162
Links
136163
-----
137164
- Project: https://github.com/ivan-korobkov/python-inject

setup.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from distutils.core import setup
22

33

4+
def read_description():
5+
with open('README.rst', 'r') as f:
6+
return f.read()
7+
8+
49
setup(
510
name='Inject',
611
version='3.0.0',
@@ -11,7 +16,7 @@
1116
author_email='ivan.korobkov@gmail.com',
1217

1318
description='Python dependency injection framework',
14-
long_description=open('README.rst', 'r').read(),
19+
long_description=read_description(),
1520

1621
package_dir={'': 'src'},
1722
py_modules=['inject'],

0 commit comments

Comments
 (0)