Dependency injection and inversion of control and library for simple service handling
Project description
# IOCynergy
Python module for IOC container using dependency injection (uses python 3+ type hints)
## Easy way to initialize your services
```python
from cynergy import container
class TestClass:
pass
class ParentClass:
def __init__(self, test_class: TestClass):
self.test_class = test_class
instance = container.get(ParentClass) # Returns TestClass initialized as singleton
print(type(instance)) # ParentClass
print(type(instance.test_class)) # TestClass
```
## Access to your configuration from any service
```python
from cynergy import container
from cynergy.config import Config, MemoryConfig
from cynergy.attributes import arguments
@arguments(db=Config('db_name'),host=Config('hostname'))
class DbConnector:
def __init__(self, db: str, host: str):
self.db = db
self.host = host
container.initialize(MemoryConfig({
"db_name": "LocalDbName",
"hostname": "localhost"
}))
instance = container.get(DbConnector)
print(instance.db) # LocalDbName
print(instance.host) # localhost
```
** You can implement your own configuration provider (for exmaple you can create DbConfigProvider which provides your settings from the db)
## Manually register special types
```python
from cynergy import container
class Original:
pass
class Other:
pass
container.register_class(Original, Other)
instance = container.get(Original)
print(type(instance)) # Other
```
## Register collection of services
```python
from typing import List
from cynergy import container
class HandlerBase:
pass
class SomeHandler1(HandlerBase):
pass
class SomeHandler2(HandlerBase):
pass
class SomeService:
def __init__(self, handlers: List[HandlerBase]):
self.handlers = handlers
container.register_many(HandlerBase, [SomeHandler1, SomeHandler2])
instance = container.get(SomeService)
print(type(instance.handlers)) # list
print(type(instance.handlers[0])) # SomeHandler1
print(type(instance.handlers[1])) # SomeHandler2
```
Python module for IOC container using dependency injection (uses python 3+ type hints)
## Easy way to initialize your services
```python
from cynergy import container
class TestClass:
pass
class ParentClass:
def __init__(self, test_class: TestClass):
self.test_class = test_class
instance = container.get(ParentClass) # Returns TestClass initialized as singleton
print(type(instance)) # ParentClass
print(type(instance.test_class)) # TestClass
```
## Access to your configuration from any service
```python
from cynergy import container
from cynergy.config import Config, MemoryConfig
from cynergy.attributes import arguments
@arguments(db=Config('db_name'),host=Config('hostname'))
class DbConnector:
def __init__(self, db: str, host: str):
self.db = db
self.host = host
container.initialize(MemoryConfig({
"db_name": "LocalDbName",
"hostname": "localhost"
}))
instance = container.get(DbConnector)
print(instance.db) # LocalDbName
print(instance.host) # localhost
```
** You can implement your own configuration provider (for exmaple you can create DbConfigProvider which provides your settings from the db)
## Manually register special types
```python
from cynergy import container
class Original:
pass
class Other:
pass
container.register_class(Original, Other)
instance = container.get(Original)
print(type(instance)) # Other
```
## Register collection of services
```python
from typing import List
from cynergy import container
class HandlerBase:
pass
class SomeHandler1(HandlerBase):
pass
class SomeHandler2(HandlerBase):
pass
class SomeService:
def __init__(self, handlers: List[HandlerBase]):
self.handlers = handlers
container.register_many(HandlerBase, [SomeHandler1, SomeHandler2])
instance = container.get(SomeService)
print(type(instance.handlers)) # list
print(type(instance.handlers[0])) # SomeHandler1
print(type(instance.handlers[1])) # SomeHandler2
```
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
cynergy-1.1.9.tar.gz
(7.2 kB
view details)
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 cynergy-1.1.9.tar.gz.
File metadata
- Download URL: cynergy-1.1.9.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1604aee28a8d6c87b0edfa8a1c67c7dfe17647aef7889d6f05b60e62af6d1bbb
|
|
| MD5 |
633825bca4e72c19b761ac4c9bdb076f
|
|
| BLAKE2b-256 |
786940d0bc3dbdbed7cdb19824843a82c337ae0595c87399d4f5cb238fd674a0
|
File details
Details for the file cynergy-1.1.9-py3-none-any.whl.
File metadata
- Download URL: cynergy-1.1.9-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa686132db35401bed13aba2bd39e0d9acee1ed54567069e3669260bb5bbabcf
|
|
| MD5 |
7370a5187ee5fc81b07867be193be39f
|
|
| BLAKE2b-256 |
4020005c76b6025b56b06cf32f80752e4ec2eee06aab92e1cd2e56c5b64ae2c7
|