Small, annotation-driven dependency injection for Python.
Project description
Small DI
The smallest dependency injection mechanism possible in python. SmallDI provides you with intuitive and simple interface for doing dependency injections in your project.
Example usage
import random
from smalldi import Injector
from smalldi.annotation import Provide
# Lets create some service
@Injector.singleton
class MeowService:
_MEOWS = ["Meow", "Meow-meow", "Meowwwwww", "Mrromeowww", "Meeeeoooow"]
def __init__(self):
print("Meow-meow! Meowing service is initialized")
def meow(self):
print(random.choice(self._MEOWS))
# Now lets make purring service.
# But cats do not purr without telling meow!(at least in this test)
# So its time to inject dependency
@Injector.singleton
class PurrService:
_PURRS = ["Purrrrr", "Purr-purr"]
@Injector.inject
def __init__(self, meow_service: Provide[MeowService]):
self.meow_service = meow_service
print("Purr-purr! Purring service is initialized")
def purr(self):
self.meow_service.meow()
print(random.choice(self._PURRS))
# Now lets put it all together.
# Ask our services to meow and then purr
@Injector.inject
def main(meow_service: Provide[MeowService], purr_service: Provide[PurrService]):
meow_service.meow()
purr_service.purr()
if __name__ == '__main__':
main()
Library logic
Injector
Injector is a static class(i.e. one that should never be instantiated) storing all singletons and responsible for injecting those singletons to a functions.
Provide
Provide[T] is an annotation for injector telling it that instead of this argument
instance of T should be passed. Caller of function with Provide[T] may explicitly
override argument annotated with Provide[T] by directly passing annotated_di_arg=my_value.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file smalldi-0.1.0.tar.gz.
File metadata
- Download URL: smalldi-0.1.0.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be42a62d254b5a48f3a732ff5a18aeecdc41dfe8e1b2db1ed638023432161f6d
|
|
| MD5 |
1d961bb977bbaf39e1d6329e4ea1816e
|
|
| BLAKE2b-256 |
39714116c09cfef10fe3b05ca3a64f8522f18ea2177ae026486d35b370c77bcd
|
Provenance
The following attestation bundles were made for smalldi-0.1.0.tar.gz:
Publisher:
publish.yaml on 0xf104a/smalldi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
smalldi-0.1.0.tar.gz -
Subject digest:
be42a62d254b5a48f3a732ff5a18aeecdc41dfe8e1b2db1ed638023432161f6d - Sigstore transparency entry: 449642516
- Sigstore integration time:
-
Permalink:
0xf104a/smalldi@ea5d3ba181a1bad02f1bdd9ef26b562b879c2e8d -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/0xf104a
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@ea5d3ba181a1bad02f1bdd9ef26b562b879c2e8d -
Trigger Event:
release
-
Statement type:
File details
Details for the file smalldi-0.1.0-py3-none-any.whl.
File metadata
- Download URL: smalldi-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b92acfebab8f43223ad2d08f7b12b7dfed1b6d62210ac8c8c70920b55c12cf25
|
|
| MD5 |
f2c7aced32eb1365a678baa1fac47070
|
|
| BLAKE2b-256 |
559e2d9112db8e83059d4ea7fa19cfdb6b95900b3cc3cea465b7f45f10f4693f
|
Provenance
The following attestation bundles were made for smalldi-0.1.0-py3-none-any.whl:
Publisher:
publish.yaml on 0xf104a/smalldi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
smalldi-0.1.0-py3-none-any.whl -
Subject digest:
b92acfebab8f43223ad2d08f7b12b7dfed1b6d62210ac8c8c70920b55c12cf25 - Sigstore transparency entry: 449642537
- Sigstore integration time:
-
Permalink:
0xf104a/smalldi@ea5d3ba181a1bad02f1bdd9ef26b562b879c2e8d -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/0xf104a
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@ea5d3ba181a1bad02f1bdd9ef26b562b879c2e8d -
Trigger Event:
release
-
Statement type: