Skip to main content

A config loader designed to be used with the Bevy dependency injection framework.

Project description

Bevy

Bevy makes using Dependency Injection a breeze so that you can focus on creating amazing code.

Installation

pip install bevy

Documentation

Dependency Injection

Put simply, Dependency Injection is a design pattern where the objects that your class depends on are instantiated outside of the class. Those dependencies are then injected into your class when it is instantiated. This promotes loosely coupled code where your class doesn’t require direct knowledge of what classes it depends on or how to create them. Instead your class declares what class interface it expects and an outside framework handles the work of creating the class instances with the correct interface.

Interfaces

Python doesn’t have an actual interface implementation like many other languages. Class inheritance, however, can be used in a very similar way since sub classes will likely have the same fundamental interface as their base class.

Why Do I Care?

Dependency Injection and its reliance on abstract interfaces makes your code easier to maintain:

  • Changes can be made without needing to alter implementation details in unrelated code, so long as the interface isn’t modified in a substantial way.
  • Tests can provide mock implementations of dependencies without needing to jump through hoops to inject them. They can provide the mock to the context and Bevy will make sure it is used where appropriate.

How It Works

Bevy is an object oriented dependency injection framework. Similar to Pydantic, it relies on Python 3's class annotations, using them to determine what dependencies a class has.

Example

@bevy.injectable
class Example:
    dependency: Dependency

Each dependency when instantiated is added to a context repository for reuse. This allows many classes to share the same instance of each dependency. This is handy for sharing things like database connections, config files, or authenticated API sessions.

Bevy Constructors

To instantiate classes and have Bevy inject their dependencies it is necessary to use a bevy.Constructor. The constructor takes a bevy.Injectable and any args necessary to instantiate it. Calling Constructor.build on the constructor will then create an instance of the Injectable with all dependencies injected.

Example

constructor = bevy.Constructor(Example)
example = constructor.build()

Configuring Dependencies

When the Constructor encounters a dependency that is not in the context repository it will attempt to create the dependency. The approach is very naive, it will just call the dependency with no arguments. If it succeeds it will be added to the repository for later use and injected into the class.

This behavior can be changed by passing an instantiated dependency to Constructor.add. Example

constructor.add(Dependency("foobar"))

When adding an Injectable it is necessary to use Constructor.branch as it will inherit all dependencies that are added to the constructor. Any dependencies added to the branch will not be back propagated to the constructor, allowing for dependency isolation. Because branches inherit but do not propagate, their dependency resolution defers until Constructor.build is called, when it is assumed all dependencies with customized instantiations have been added.

Because Injectables require a special lifecycle Constructor.branch will accept any instantiation args that should be passed to the class when it is constructed. Example

branch = constructor.branch(Dependency)

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

Bevy.config-0.1.2.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Bevy.config-0.1.2-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file Bevy.config-0.1.2.tar.gz.

File metadata

  • Download URL: Bevy.config-0.1.2.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.0 Darwin/20.5.0

File hashes

Hashes for Bevy.config-0.1.2.tar.gz
Algorithm Hash digest
SHA256 dc68e9bfc957a306db61efd18cdf44c26158a4caf94850f4fd013f12e6a73e61
MD5 d4464dd35f0e7f92383852c390d34cd4
BLAKE2b-256 e7a87fd787d1c3dbfc0411bb3cde6d9ea74d6e6b04fa0d268205e03c351c4849

See more details on using hashes here.

File details

Details for the file Bevy.config-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: Bevy.config-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.0 Darwin/20.5.0

File hashes

Hashes for Bevy.config-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dfd840a164c387399332f4c5b46245f4a92cac54f740edd7caa3ea6f3746f897
MD5 cde8dc573a2dd238cf5775e0b9c80832
BLAKE2b-256 b26d90bc89220882712ed74f155c035f41cd5659011f6e8c8de9bc47ce8f8c64

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page