Skip to content

Commit d271f9d

Browse files
committed
Document usage in README.md
1 parent 5381ecd commit d271f9d

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

README.md

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,52 @@ A simple Python dependency injection framework.
66

77
**This project is under active development. The following example does not represent the final state for the project.**
88

9+
The injection framework is configured to inject any default values for method arguments that are instances
10+
of `providers.Provider`.
11+
12+
This implementation works by wrapping decorators around methods any patching any unfilled `providers.Provider` default
13+
arguments at runtime.
14+
15+
All dependency injection is lazily evaluated so providers are only evaluated when a method is called. This approach is
16+
optimal as it reduces necessary computation for expensive services and reduces
17+
18+
### Decorator Injection
19+
20+
With this approach you can automatically inject functions at load time using the `@wiring.injected` decorator.
21+
922
```python
1023
from pif import wiring, providers
1124

1225

13-
@wiring.injected
26+
@wiring.injected # <- automatically injects providers.Provider default arguments!
1427
def my_function(a: str = providers.Singleton[str](lambda: "hello world")):
1528
return a
1629

1730

18-
if __name__ == '__main__':
31+
if __name__ == "__main__":
32+
assert "hello world" == my_function()
33+
```
34+
35+
### Module Injection
36+
37+
With this approach you can wire all methods in the specified modules.
38+
39+
```python
40+
from pif import wiring, providers
41+
42+
43+
def my_function(a: str = providers.Singleton[str](lambda: "hello world")):
44+
return a
45+
46+
47+
if __name__ == "__main__":
48+
wiring.wire([__name__]) # <- dynamically inject methods with providers.Provider default arguments!
49+
1950
assert "hello world" == my_function()
2051
```
2152

2253
## Authors
2354

24-
| [![Zac Scott](https://avatars.githubusercontent.com/u/38968222)](https://github.com/scottzach1) |
25-
|:------------------------------------------------------------------------------------------------|
26-
| [Zac Scott (scottzach1)](https://github.com/scottzach1) |
55+
| [![Zac Scott](https://avatars.githubusercontent.com/u/38968222?s=128&v=4)](https://github.com/scottzach1) |
56+
|:----------------------------------------------------------------------------------------------------------|
57+
| [Zac Scott (scottzach1)](https://github.com/scottzach1) |

0 commit comments

Comments
 (0)