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

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

And predefined types:

  • ServiceProvider
  • IServiceProvider

provider.get() vs provider[]

There are two ways to get services from ServiceProvider:

  • provider[] will raise ServiceNotFoundError if the service was not found;
  • provider.get() only return None 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 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.

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.9.1.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

anyioc-0.9.1-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: anyioc-0.9.1.tar.gz
  • Upload date:
  • Size: 11.1 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.1.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.1

File hashes

Hashes for anyioc-0.9.1.tar.gz
Algorithm Hash digest
SHA256 0188940ecae2817a20d5d77395425e104a0ad91e5576a1f0321de1f9426199f7
MD5 efa2bdfbd876dc7d5099f51ef757810c
BLAKE2b-256 d140d5e315bb4ce96765c51d1eb6609d7b26b95d226b0ac564248665caa96018

See more details on using hashes here.

File details

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

File metadata

  • Download URL: anyioc-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 14.6 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.1.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.1

File hashes

Hashes for anyioc-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eb73ffe6c9c01dd29b1483ff322d312ff7bc8419a0ee2df4c766126621fd56b4
MD5 6353b5b44fe0bd86aca3323bb7794b5e
BLAKE2b-256 6f142d32c077333504b5fafb307750f9ee12330e4da295286ed5349e747ce712

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