Skip to main content

Springboot-like Dependency Injection Framework for Python

Project description

PySpring Dependency Injection Framework

PySpring is a lightweight dependency injection framework for Python that allows you to easily manage dependencies and configure your application's components. It provides some spring-like features such as auto-configuration and bean discovery:

  • A simple API for defining beans and injecting dependencies into them.
  • An auto-configuration feature that automatically discovers beans in specified paths and wires them together.
  • Config beans in configuration files, now only support HOCON format. Check the HOCON and pyhocon for more details.

Installation

PySpring is based on the inject library, which is a dependency injection framework for Python. (Currently, PySpring only supports inject version 5.0.0.)

You can install PySpring using pip:

pip install pyspring

Getting Started

To use PySpring, you need to follow these steps:

Define your beans: Beans are the components of your application that can be injected with dependencies. You can define beans by creating classes and decorating them with the @Bean() decorator.

Define bean dependencies: You can inject dependencies into beans using the inject.attr() function. This function takes the name of the dependency as an argument and returns the instance of the dependency.

Configure your application: PySpring provides an auto_config() function that automatically discovers beans in specified paths and wires them together. You need to call this function to configure your application.

Obtain bean instances: After configuring your application, you can obtain instances of beans using the inject.instance() function. This function takes the class name of the bean as an argument and returns an instance of that bean.

Example

Here's an example that demonstrates the usage of PySpring:

import time
import inject
from pyspring import auto_config
from pyspring.decorators import Bean, FunctionNameBean
from pyspring.scope import Scope

@Bean()
class BeanExample:
    time: float

    def __init__(self):
        self.time = time.time()

@FunctionNameBean(scope=Scope.prototype)
def current_time() -> int:
    return int(time.time())

@Bean()
class CurrentTimeHolder:
    current_time: int = inject.attr("current_time")  # type: ignore
    current_time_bean: BeanExample = inject.attr(BeanExample)  # type: ignore

    def print_time(self):
        print(self.current_time)

    def print_bean(self):
        print(self.current_time_bean.time)

if __name__ == "__main__":
    auto_config(path="examples")

    current_time_holder = inject.instance(CurrentTimeHolder)
    print(current_time_holder)
    print(current_time_holder.current_time)
    print(current_time_holder.current_time_bean)
    current_time_holder.print_bean()
    current_time_holder.print_time()

In this example, we define two beans: BeanExample and CurrentTimeHolder. BeanExample has a single attribute time, which is initialized with the current time. CurrentTimeHolder has two attributes, current_time and current_time_bean, which are injected with dependencies using the inject.attr() function.

We also define a function current_time() as a bean using the @FunctionNameBean decorator. This function returns the current time as an integer.

To configure the application, we call auto_config() and provide the path where our beans are located.

Finally, we obtain an instance of CurrentTimeHolder using inject.instance() and demonstrate accessing its attributes and calling its methods.

Check the examples folder for more examples.

Conclusion

PySpring simplifies dependency injection in Python by providing a lightweight framework for managing dependencies and configuring your application's components. It allows you to decouple your code and improve testability and modularity. Give PySpring a try and enjoy the benefits of dependency injection in your Python projects!

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

pyspring-inject-0.0.1.tar.gz (12.0 kB view hashes)

Uploaded Source

Built Distribution

pyspring_inject-0.0.1-py3-none-any.whl (12.3 kB view hashes)

Uploaded Python 3

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