Skip to main content

No project description provided

Project description

anyioc

GitHub Build Status PyPI

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

Register and resolve

By default, you can use following methods to register services:

  • ServiceProvider.register_singleton(key, factory)
  • ServiceProvider.register_scoped(key, factory)
  • ServiceProvider.register_transient(key, factory)
  • ServiceProvider.register(key, factory, lifetime)
  • ServiceProvider.register_value(key, value)
  • ServiceProvider.register_group(key, keys)
  • ServiceProvider.register_bind(new_key, target_key)

And use following methods to resolve services:

  • ServiceProvider.__getitem__(key)
  • ServiceProvider.get(key)
  • ServiceProvider.get_many(key)

get return None if the service was not found, but __getitem__ will raise a ServiceNotFoundError.

Thread safety

  • Root ServiceProvider is thread-safe.
  • Scoped ServiceProvider is NOT thread-safe.
  • Singleton service is thread-safe.
  • Value service is thread-safe.

Global ServiceProvider

Process scoped

By default, you should create your ServiceProvider.

However, anyioc provide a singleton global ServiceProvider to share services in python process.

from anyioc.g import ioc

# ioc just a global `ServiceProvider` instance

This is helpful if you writing a final application instead of a library/package.

Module scoped and namespace scoped

anyioc also provide 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 scoped ServiceProvider instance.
  • provider - alias of ioc
  • service_provider - alias of ioc

And predefined types:

  • ServiceProvider - alias of ioc
  • IServiceProvider - alias of ioc

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()
resolver = ImportServiceInfoResolver().cache() # use `.cache()` can cache the results and prevent resolve again.
provider[Symbols.missing_resolver].append(resolver)
assert sys is provider['sys']

There are other builtin resolvers:

  • ImportServiceInfoResolver - import module by name from a str key
  • TypesServiceInfoResolver - create instance by type from a type key
  • TypeNameServiceInfoResolver - create instance by type name from a str key
  • TypingServiceInfoResolver - get services tuple by keys from a typing.Tuple key.

IServiceInfoResolver only work when service was missing.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

anyioc-0.10.0.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

anyioc-0.10.0-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file anyioc-0.10.0.tar.gz.

File metadata

  • Download URL: anyioc-0.10.0.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.1

File hashes

Hashes for anyioc-0.10.0.tar.gz
Algorithm Hash digest
SHA256 067c1b8505e7e56151954f1cee7a395dfa2e6e81549987c9f321060d005b79e7
MD5 207ff7815243d62ebe8c4cf088cbd95d
BLAKE2b-256 c0559f5012e15231b876c116a608710f9e8d08bf359ab6f971957a8fcbc2e929

See more details on using hashes here.

File details

Details for the file anyioc-0.10.0-py3-none-any.whl.

File metadata

  • Download URL: anyioc-0.10.0-py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.1

File hashes

Hashes for anyioc-0.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ce48feac61351f2c744a6d9ec08aa328817d594211a80139b1b1d3e3d78467cf
MD5 7f7658e94527eb9c8bd0043c309aea04
BLAKE2b-256 e57e8e83f574d4fad85ceada30e51022f28f8fa16a1af2688fa5bab1eaebc085

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page