No project description provided
Project description
anyioc
Another simple ioc framework for python.
Usage
from anyioc import ServiceProvider
provider = ServiceProvider()
provider.register_singleton('the key', lambda ioc: 102) # ioc will be a `IServiceProvider`
value = provider.get('the key')
assert value == 102
Details
Features
By default, you can use methods of ServiceProvider
to register services with lifetime:
register_singleton(key, factory)
register_scoped(key, factory)
register_transient(key, factory)
register(key, factory, lifetime)
register_value(key, value)
register_group(key, keys)
register_bind(new_key, target_key)
Global ServiceProvider
Process scoped
By default, you should create your ServiceProvider
.
However, we can use a global ServiceProvider
to share services in python process.
from anyioc.g import ioc
# ioc is a global `ServiceProvider` instance
Module scoped and namespace scoped
Also we have module scoped ServiceProvider
and namespace scoped ServiceProvider
.
If you have a project:
src/
|- your_package/
|- __init__.py
|- a/
|- __init__.py
|- b.py
Then module scoped ServiceProvider
:
# file: b.py
from anyioc.g import get_module_provider
provider = get_module_provider()
assert provider is get_module_provider('your_package.a.b')
and namespace scoped ServiceProvider
:
# file: b.py
from anyioc.g import get_namespace_provider
provider = get_namespace_provider()
assert provider is get_module_provider('your_package')
Predefined keys
There are some predefined string keys you can use direct, but you still can overwrite it:
ioc
- get current scopedServiceProvider
instance.provider
- alias ofioc
service_provider
- alias ofioc
provider.get()
vs provider[]
There are two ways to get services from ServiceProvider
:
provider[]
will raiseServiceNotFoundError
if the service was not found;provider.get()
only returnNone
if the service was not found.
IServiceInfoResolver
By default, you can get a service after you register it;
If you want to dynamic get it without register, you can do that by use IServiceInfoResolver
:
from anyioc import ServiceProvider
from anyioc.symbols import Symbols
from anyioc.ioc_resolver import ImportServiceInfoResolver
import sys
provider = ServiceProvider()
provider[Symbols.missing_resolver].append(ImportServiceInfoResolver().cache())
assert sys is provider['sys']
.cache()
can cache the results.
There are other builtin resolvers:
ImportServiceInfoResolver
- import module by name from astr
keyTypesServiceInfoResolver
- create instance by type from atype
keyTypeNameServiceInfoResolver
- create instance by type name from astr
keyTypingServiceInfoResolver
- get services tuple by keys from atyping.Tuple
key.
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
File details
Details for the file anyioc-0.6.2.tar.gz
.
File metadata
- Download URL: anyioc-0.6.2.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f3ef7802051dbfe2162b7a99fe89bbeae5457d5419d9683aed9ce6df47023774 |
|
MD5 | 39cd40b4ba9252c6651df3e2b05ca7ef |
|
BLAKE2b-256 | 710030016294cb7ae3ed9127a10cd0eb858e6e547c9143072d8a888545a0fcaf |
File details
Details for the file anyioc-0.6.2-py3-none-any.whl
.
File metadata
- Download URL: anyioc-0.6.2-py3-none-any.whl
- Upload date:
- Size: 12.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee08a40e43ee2f9955c5ad9905d1a8c5db28d13d9ace595512a3cdfb6727c930 |
|
MD5 | b1540ecb930cb6a456182a2f0aa2e2fa |
|
BLAKE2b-256 | 6a96f9d6b31eb35196b0ace1634c8062c760709d5f8dcc7cf93aba4c3a8919fc |