@@ -34,34 +34,36 @@ optimal as it reduces necessary computation for expensive services and reduces
34
34
With this approach you can automatically inject functions at load time using the ` @wiring.inject ` decorator.
35
35
36
36
``` python
37
- from pif import wiring, providers
37
+ from pif import providers
38
+ from pif import wiring
38
39
39
40
40
41
@wiring.inject # <- automatically injects providers.Provider default arguments!
41
42
def my_function (a : str = providers.ExistingSingleton(" hello world" )):
42
- return a
43
+ return a
43
44
44
45
45
46
if __name__ == " __main__" :
46
- assert " hello world" == my_function()
47
+ assert " hello world" == my_function()
47
48
```
48
49
49
50
### Module Injection
50
51
51
52
With this approach you can wire all methods in the specified modules.
52
53
53
54
``` python
54
- from pif import wiring, providers
55
+ from pif import providers
56
+ from pif import wiring
55
57
56
58
57
59
def my_function (a : str = providers.ExistingSingleton(" hello world" )):
58
- return a
60
+ return a
59
61
60
62
61
63
if __name__ == " __main__" :
62
- wiring.wire([__name__ ]) # <- dynamically inject methods with providers.Provider default arguments!
64
+ wiring.wire([__name__ ]) # <- dynamically inject methods with providers.Provider default arguments!
63
65
64
- assert " hello world" == my_function()
66
+ assert " hello world" == my_function()
65
67
```
66
68
67
69
### Overriding
@@ -75,53 +77,55 @@ If you want to patch a value all you need to do is call `.override()` on the pro
75
77
override an existing singleton you may call the convenience method ` .override_existing() ` .
76
78
77
79
``` python
78
- from pif import wiring, providers
80
+ from pif import providers
81
+ from pif import wiring
79
82
80
83
StringProvider = providers.ExistingSingleton(" hello world" )
81
84
82
85
83
86
@wiring.inject
84
87
def my_function (a : str = StringProvider):
85
- return a
88
+ return a
86
89
87
90
88
91
if __name__ == " __main__" :
89
- assert " hello world" == my_function()
92
+ assert " hello world" == my_function()
90
93
91
- override = StringProvider.override_existing(" overridden_1" )
94
+ override = StringProvider.override_existing(" overridden_1" )
92
95
93
- assert " overridden_1"
96
+ assert " overridden_1"
94
97
```
95
98
96
99
### Context Managers
97
100
98
101
If you want more control around the override lifecycles then you may use the ` Override ` context manager.
99
102
100
103
``` python
101
- from pif import wiring, providers
104
+ from pif import providers
105
+ from pif import wiring
102
106
103
107
StringProvider = providers.ExistingSingleton(" hello world" )
104
108
105
109
106
110
@wiring.inject
107
111
def my_function (a : str = StringProvider):
108
- return a
112
+ return a
109
113
110
114
111
115
if __name__ == " __main__" :
112
- assert " hello world" == my_function()
116
+ assert " hello world" == my_function()
113
117
114
- OverrideProvider = providers.ExistingSingleton(" overridden_1" )
118
+ OverrideProvider = providers.ExistingSingleton(" overridden_1" )
115
119
116
- with StringProvider.override(OverrideProvider):
117
- assert " overridden_1" == my_function()
120
+ with StringProvider.override(OverrideProvider):
121
+ assert " overridden_1" == my_function()
118
122
119
- with OverrideProvider.override_existing(" overridden_2" ):
120
- assert " overridden_2" == my_function() # You can even stack overrides!!
123
+ with OverrideProvider.override_existing(" overridden_2" ):
124
+ assert " overridden_2" == my_function() # You can even stack overrides!!
121
125
122
- assert " overridden_1" == my_function()
126
+ assert " overridden_1" == my_function()
123
127
124
- assert " hello world" == my_function()
128
+ assert " hello world" == my_function()
125
129
```
126
130
127
131
## Examples
@@ -130,12 +134,12 @@ If you would like to see more examples, feel free to check out [examples/](examp
130
134
131
135
## Contributing
132
136
133
- 1 . Clone the repository and configure Poetry 🪄
137
+ 1 . Clone the repository and setup with uv 🪄
134
138
135
139
``` shell
136
140
git clone
[email protected] :scottzach1/Python-Injection-Framework.git
137
141
cd Python-Injection-Framework
138
- poetry install
142
+ uv sync --dev
139
143
```
140
144
141
145
2. Configure pre-commit hooks 🪝
@@ -149,7 +153,7 @@ If you would like to see more examples, feel free to check out [examples/](examp
149
153
4. Run test cases 🧪
150
154
151
155
` ` ` shell
152
- pytest tests/
156
+ pytest
153
157
` ` `
154
158
155
159
5. Submit a Pull Request ↖️
0 commit comments