giveme package¶
Submodules¶
giveme.core module¶
deprecated: | 1.0.0 |
---|
-
class
giveme.core.
Manager
¶ Bases:
object
Deprecated: 1.0.0 -
clear
()¶
-
get
(name)¶ Get a dependency factory by name, None if not registered
-
get_value
(name)¶ Get return value of a dependency factory or a live singleton instance.
-
register
(func, singleton=False, threadlocal=False, name=None)¶ Register a dependency function
-
remove
(name)¶ Remove a dependency by name
-
-
giveme.core.
inject
(function=None, **overridden_names)¶ Deprecated: 1.0.0 Use
giveme.injector.Injector
instead.Inject dependencies into given function’s arguments. By default the injector looks for keyword arguments matching registered dependency names.
Example:
@register def db_connection():
return create_db_connection()@inject def save_thing(thing, db_connection=None):
db_connection.store(thing)Arbitrary arguments may also be mapped to specific dependency names by passing them to the decorator as
arg='dependency_name'
- Example:
- @inject(db=’db_connection’) def save_thing(thing, db=None): # db_connection injected as db
- Args:
- function (callable): The function that accepts a dependency.
- Implicitly passed when used as a decorator.
- **overridden_names: Mappings of function arguments to
- dependency names in the form of
function_argument='dependency name'
-
giveme.core.
register
(function=None, *, singleton=False, threadlocal=False, name=None)¶ Deprecated: 1.0.0 Use
giveme.injector.Injector
instead.Register a dependency factory in the dependency manager. The function name is the name of the dependency. This can be used as a decorator.
- Args:
- function (callable): The dependency factory function
- Not needed when used as decorator.
- singleton (
bool
, optional): IfTrue
the given function is only called once - during the application lifetime. Injectees will receive the already created
instance when available. Defaults to
False
- threadlocal (
bool
, optional): Same as singleton except the returned instance - is available only to the thread that created it. Defaults to
False
- name (
str
, optional): Overridden name for the dependency. - Defaults to the name of the registered function.
giveme.injector module¶
-
exception
giveme.injector.
AsyncDependencyForbiddenError
Bases:
Exception
-
class
giveme.injector.
Dependency
(name, factory, singleton=False, threadlocal=False) Bases:
object
-
factory
-
name
-
singleton
-
threadlocal
-
-
exception
giveme.injector.
DependencyNotFoundError
Bases:
Exception
-
exception
giveme.injector.
DependencyNotFoundWarning
Bases:
RuntimeWarning
-
class
giveme.injector.
Injector
Bases:
object
-
cache
(dependency: giveme.injector.Dependency, value) Store an instance of dependency in the cache. Does nothing if dependency is NOT a threadlocal or a singleton.
Parameters: - dependency (Dependency) – The
Dependency
to cache - value – The value to cache for dependency
- dependency (Dependency) – The
-
cached
(dependency) Get a cached instance of dependency.
Parameters: dependency ( Dependency
) – TheDependency
to retrievie value forReturns: The cached value
-
clear
() Clear (unregister) all dependencies. Useful in tests, where you need clean setup on every test.
-
delete
(name) Delete (unregister) a dependency by name.
-
get
(name: str) Get an instance of dependency, this can be either a cached instance or a new one (in which case the factory is called)
-
inject
(function=None, **names) Inject dependencies into funtion’s arguments when called.
>>> @injector.inject ... def use_dependency(dependency_name): ... >>> use_dependency()
The Injector will look for registered dependencies matching named arguments and automatically pass them to the given function when it’s called.
Parameters: - function (callable) – The function to inject into
- **names – in the form of
argument='name'
to override the default behavior which matches dependency names with argument names.
-
register
(function=None, *, singleton=False, threadlocal=False, name=None) Add an object to the injector’s registry.
Can be used as a decorator like so:
>>> @injector.register ... def my_dependency(): ...
or a plain function call by passing in a callable injector.register(my_dependency)
Parameters: - function (callable) – The function or callable to add to the registry
- name (string) – Set the name of the dependency. Defaults to the name of function
- singleton (bool) – When True, register dependency as a singleton, this means that function is called on first use and its return value cached for subsequent uses. Defaults to False
- threadlocal (bool) – When True, register dependency as a threadlocal singleton,
Same functionality as
singleton
exceptThreading.local
is used to cache return values.
-
resolve
(dependency) Resolve dependency as instance attribute of given class.
>>> class Users: ... db = injector.resolve(user_db) ... ... def get_by_id(self, user_id): ... return self.db.get(user_id)
When the attribute is first accessed, it will be resolved from the corresponding dependency function
-