Pyjection is a lightweight python dependency injection library
Basic dependency injection
The most import class is DependencyInjector which lets us register classes and retrieve instances.
from pyjection.dependency_injector import DependencyInjector
class OuterClass(object):
def __init__(self, inner_class):
self.inner_class = inner_class
class InnerClass(object):
def __init__(self):
self.foo = "bar"
container = DependencyInjector()
container.register(InnerClass)
container.register(OuterClass)
outer = container.get("outer_class")
print(outer.inner_class.foo) # Will print "bar"
Class bindings
Implicit class bindings
When no id is specified in the register method Pyjection creates implicit bindings for classes. The implicit bindings assume your code follows PEP8 conventions: your classes are named in CamelCase, and your args are named in lower_with_underscores. Pinject transforms class names to injectable arg names by lowercasing words and connecting them with underscores.
Class name |
Arg name |
|---|---|
Foo |
foo |
FooBar |
foo_bar |
Explicit class bindings
It is also possible to manually set the id of a class when during its registration by specifying it as a second arguments.
container.register(FooClass, "inner_class")
With the example above, FooClass will later be injected to arguments named inner_class
Instance retrieval
To retrieve an instance of a class from the dependency injector 2 options are available in the get method:
Specify the lower_with_underscores name of the class as a string
Give the class as parameter
from pyjection.dependency_injector import DependencyInjector
class FooClass(object):
pass
container = DependencyInjector()
container.register(FooClass)
container.get("foo_class")
# Same as
container.get(FooClass)
Singleton injection
The dependency injector lets us register a singleton. To register a singleton the method register_singleton may be used. It takes the same arguments as register.
from pyjection.dependency_injector import DependencyInjector
class SingletonClass(object):
pass
container = DependencyInjector()
container.register_singleton(SingletonClass)
# Or we could specify an id
container.register_singleton(SingletonClass, "other_id")
class_1 = container.get("other_id")
class_2 = container.get("other_id")
print(class_1 is class_2) # True
Explicit argument specification
Simple argument specification
Sometimes the argument we need to inject is not an instance of a class. The register and register_singleton methods return a service object that lets us specify what we want to bind to a given argument by using the add_argument method.
from pyjection.dependency_injector import DependencyInjector
class FooClass(object):
def __init__(self, foo):
self.foo = foo
container = DependencyInjector()
service = container.register(FooClass)
service.add_argument("foo", "bar")
foo_class = container.get("foo_class")
print(foo_class.foo) # Will print bar
Reference argument specification
A service argument can also reference another dependency injector service. It is useful when we want to inject a class not matching the argument name.
from pyjection.dependency_injector import DependencyInjector
from pyjection.reference import Reference
class OuterClass(object):
def __init__(self, inner_class):
self.inner_class = inner_class
class FooClass(object):
def __init__(self):
self.foo = "bar"
container = DependencyInjector()
container.register(FooClass)
container.register(OuterClass).add_argument("inner_class", Reference('foo_class'))
instance = container.get(OuterClass)
print(instance.inner_class.foo) # Will print bar
Release files for pyjection 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pyjection-1.0.0.tar.gz | 5.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyjection-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.3 kB
Release files / pyjection-1.0.0.tar.gz
| Download URL | pyjection-1.0.0.tar.gz |
|---|---|
| Size | 5.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c938945dc0edafab406d1290a6994e81b3a5226bf4cec0e548c80584a1924875
|
|
BLAKE2b-256 checksum How to use checksums |
752b5348f61003e88b5cdd5ab124c38ae3390f7084effa624417803ffdb783ed
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / pyjection-1.0.0-py3-none-any.whl
| Download URL | pyjection-1.0.0-py3-none-any.whl |
|---|---|
| Size | 6.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
db5ee6f31ba0ebc22db3433337c239f4d4e734a2f376ee4b40738ffe303f211f
|
|
BLAKE2b-256 checksum How to use checksums |
322329ebe73342c5354c17db56b7ea99ab6671cdc8361658de6888a95d5ec281
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |