injector package

injector.dependencies module

class injector.dependencies.Dependencies[source]

Bases: builtins.object

A factory for setting up and building an Injector instance.

build_injector()[source]

Builds an injector instance that can be used to inject dependencies.

Also checks for common errors (missing dependencies and circular dependencies).

Returns:Injector
register_factory(name, factory, dependencies=None)[source]

Binds a factory to a name. The injector will call the factory function once (if the name is ever used), and always return the value that the factory returns.

The factory will be called with the dependencies (if any listed) as arguments.

Parameters:
  • name – A string naming the dependency (e.g. ‘db-connection’)
  • factory – A factory function to create the dependency
  • dependencies – (optional) A list of dependencies of the factory function
register_value(name, value)[source]

Bind a value to a name. The Injector will always return the value as-is.

Parameters:
  • name – A string naming the dependency (e.g. ‘db-host-name’)
  • value – Any value (e.g. ‘master.postgres.internal’)

injector.exceptions module

exception injector.exceptions.BadNameException[source]

Bases: injector.exceptions.InjectorException

Raised when an invalid name is used

exception injector.exceptions.CircularDependencyException[source]

Bases: injector.exceptions.InjectorException

Raised when the dependencies defined are circular

exception injector.exceptions.DuplicateNameException[source]

Bases: injector.exceptions.InjectorException

Raised when a duplicate name is used

exception injector.exceptions.InjectorException[source]

Bases: builtins.Exception

Base class for exceptions raised by the injector

exception injector.exceptions.MissingDependencyException[source]

Bases: injector.exceptions.InjectorException

Raised when a requested dependency is not found

injector.graph module

class injector.graph.DependencyGraph(graph)[source]

Bases: builtins.object

A generic dependency graph, useful for checking some properties

has_circular_dependencies()[source]

Checks to see if the graph contains any cycles.

Returns:True if there is a cycle.
has_missing_dependencies()[source]

Checks to see if the graph contains any references to nodes that don’t exist.

Returns:True if there are missing dependencies.

injector.injector module

class injector.injector.Injector(factories)[source]

Bases: builtins.object

An injector filled with dependencies, ready to inject.

get_dependency(name)[source]

Get the value of a dependency.

Parameters:name – The name of the dependency
Returns:the value of the dependency
has_dependency(name)[source]

Check if the Injector has a dependency.

Parameters:name – The name of the dependency
Returns:True if this provides that dependency
inject(function, dependencies)[source]

Calls the function with the value of the listed dependencies.

Parameters:
  • function – The function that will be called
  • dependencies – A list of names of dependencies to inject into the function
Returns:

The result of calling the function.