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 x: 102) # x will be scoped ServiceProvider
value = provider.get('the key')
assert value == 102

Need global ServiceProvider ? try from anyioc.g import ioc.

Details

Features

By default, you can use methods to register 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)

Predefined keys

There are some predefined 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

provider.get() vs provider[]

provider[] will raise ServiceNotFoundError when service was not found;

provider.get() only return None without error.

IServiceInfoResolver

By default, you need to register a service before you get it.

So if you want to dynamic get it without register:

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())
assert sys is provider['sys']

There are other builtin resolvers:

  • ImportServiceInfoResolver - import a module from a str key
  • TypesServiceInfoResolver - create instance from a type key
  • TypeNameServiceInfoResolver - from str key find a type, then create instance

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

Uploaded Source

Built Distribution

anyioc-0.6.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: anyioc-0.6.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for anyioc-0.6.0.tar.gz
Algorithm Hash digest
SHA256 f5816e7821043368aac8bc212e685a2b87f0519b8b53511192fdd7c86f4fdda7
MD5 3d314a03309d2303dd97842051892d50
BLAKE2b-256 9db4a1a2d8dc39c7860e54d7e7e2191da7f29592acaa0565953715c3fce24bc8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: anyioc-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for anyioc-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 58ffda20a524394b5618ed77756adacee20f2f7669050c1ada14f6d92e21e235
MD5 8e51378a3e9384a44a00c94668bb2847
BLAKE2b-256 b3e44d33ff186c8e9d8dd85b35715e905e0492a0b73423547c020800ed89cd90

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