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.

Filename, size & hash SHA256 hash help File type Python version Upload date
anyioc-0.6.1-py3-none-any.whl (11.8 kB) Copy SHA256 hash SHA256 Wheel py3
anyioc-0.6.1.tar.gz (8.5 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page