Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bloodielie/pyject
https://github.com/bloodielie/pyject
container dependency-injection dependency-injection-container fast ioc ioc-container python3 singleton
Last synced: 6 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/bloodielie/pyject
- Owner: Bloodielie
- License: mit
- Created: 2020-10-17T22:46:53.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-03-31T10:00:56.000Z (over 3 years ago)
- Last Synced: 2024-11-30T04:40:41.061Z (23 days ago)
- Topics: container, dependency-injection, dependency-injection-container, fast, ioc, ioc-container, python3, singleton
- Language: Python
- Homepage:
- Size: 125 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pyject
## What is Pyject?
Pyject is a IocContainer for Python.It helps implementing the dependency injection principle.
## Installation
The package is available on the PyPi:
```bash
pip install pyjectt
```## Examples
1. [Base](#Base_example)
2. Non-default types
+ [Sequence: ("Set", "List", "Tuple", "FrozenSet", "Sequence")](#Sequence_example)
+ [Union: (Union, Optional)](#Union_example)
+ [ForwardRef](#ForwardRef_example)
+ [Iterator](#Iterator_example)
### Base
```python
from abc import ABC, abstractmethod
from typing import List
from pyject import Containerclass DuckInterface(ABC):
@abstractmethod
def quack(self):
raise NotImplementedError()class QuackBehavior(ABC):
@abstractmethod
def quack(self):
raise NotImplementedError()class Sqeak(QuackBehavior):
def quack(self):
return "Quack_1"class DuckA(DuckInterface):
def __init__(self, squeak: QuackBehavior):
self._quack_behavior = squeakdef quack(self):
return self._quack_behavior.quack()class DuckC(DuckInterface):
def quack(self):
return "Quack_2"container = Container()
container.add_singleton(QuackBehavior, Sqeak)
container.add_transient(DuckInterface, DuckA)
container.add_singleton(DuckInterface, DuckC)duck = container.get(DuckInterface)
print(duck.quack())ducks = container.get_all(DuckInterface)
for duck in ducks:
print(duck.quack())def test2(ducks: List[DuckInterface]):
result_ = ""
for duck in ducks:
result_ += duck.quack()
return result_assert container.resolve(test2) == "Quack_1Quack_2"
```
## Non-default types### Sequence
```python
def list_typing(ducks: List[DuckInterface]):
result_ = ""
for duck in ducks:
result_ += duck.quack()
return result_def sequence_typing(ducks: Sequence[DuckInterface]):
result_ = ""
for duck in ducks:
result_ += duck.quack()
return result_assert container.resolve(list_typing) == "Quack_1Quack_2"
assert container.resolve(sequence_typing) == "Quack_1Quack_2"
```
### Union
```python
def union_typing(quack: Union[QuackBehavior, DuckInterface]):
return quackdef optional_typing(test: Optional[List]):
return testassert isinstance(container.resolve(union_typing), QuackBehavior)
assert container.resolve(optional_typing) is None
```
### ForwardRef
```python
from pyject.types import ForwardRefclass Test:
def __init__(self, test2: ForwardRef["Test2"]):
self.test2 = test2def test(self):
return self.test2.test2()class Test2:
def __init__(self, test: ForwardRef[Test]):
self.test = testdef test2(self):
return "123"container.add_singleton(Test, Test)
container.add_singleton(Test2, Test2)test = container.get(Test)
assert isinstance(test, Test)
assert isinstance(test.test2, ForwardRef)
assert isinstance(test.test2.test, ForwardRef)
assert test.test() == "123"
assert test.test() == test.test2.test2()
```
### Iterator
```python
def iterator_typing(ducks: Iterator[DuckInterface]):
result_ = ""
for duck in ducks:
result_ += duck.quack()
return result_assert container.resolve(iterator_typing) == "Quack_1Quack_2"
```