Spring Python is an offshoot of the Java-based Spring Framework and Spring Security, targeted for Python. This project currently contains the following features:
- Inversion Of Control (dependency injection) - use either classic XML, or the python @Object decorator (similar to the Spring JavaConfig subproject) to wire things together. While the @Object format isn't identical to the Guice style (centralized wiring vs. wiring information in each class), it is a valuable way to wire your python app.
- Aspect-oriented Programming - apply interceptors in a horizontal programming paradigm (instead of vertical OOP inheritance) for things like transactions, security, and caching.
- DatabaseTemplate - Reading from the database requires a monotonous cycle of opening cursors, reading rows, and closing cursors, along with exception handlers. With this template class, all you need is the SQL query and row-handling function. Spring Python does the rest.
- Database Transactions - Wrapping multiple database calls with transactions can make your code hard to read. This module provides multiple ways to define transactions without making things complicated.
- Security - Plugin security interceptors to lock down access to your methods, utilizing both authentication and domain authorization.
- Remoting - It is easy to convert your local application into a distributed one. If you have already built your client and server pieces using the IoC container, then going from local to distributed is just a configuration change.
- Samples - to help demonstrate various features of Spring Python, some sample applications have been created:
- PetClinic - Spring Framework's sample web app has been rebuilt from the ground up using python web containers including: CherryPy. Go check it out for an example of how to use this framework. (NOTE: Other python web frameworks will be added to this list in the future).
- Spring Wiki - Wikis are powerful ways to store and manage content, so we created a simple one as a demo!
- Spring Bot - Use Spring Python to build a tiny bot to manage the IRC channel of your open source project.
Spring Python is an offshoot of the Java-based Spring Framework and Spring Security, targeted for Python. This project currently contains the following features:
- Inversion Of Control (dependency injection) - use either classic XML, or the python @Object decorator (similar to the Spring JavaConfig subproject) to wire things together. While the @Object format isn't identical to the Guice style (centralized wiring vs. wiring information in each class), it is a valuable way to wire your python app.
- Aspect-oriented Programming - apply interceptors in a horizontal programming paradigm (instead of vertical OOP inheritance) for things like transactions, security, and caching.
- DatabaseTemplate - Reading from the database requires a monotonous cycle of opening cursors, reading rows, and closing cursors, along with exception handlers. With this template class, all you need is the SQL query and row-handling function. Spring Python does the rest.
- Database Transactions - Wrapping multiple database calls with transactions can make your code hard to read. This module provides multiple ways to define transactions without making things complicated.
- Security - Plugin security interceptors to lock down access to your methods, utilizing both authentication and domain authorization.
- Remoting - It is easy to convert your local application into a distributed one. If you have already built your client and server pieces using the IoC container, then going from local to distributed is just a configuration change.
- Samples - to help demonstrate various features of Spring Python, some sample applications have been created:
- PetClinic - Spring Framework's sample web app has been rebuilt from the ground up using python web containers including: CherryPy. Go check it out for an example of how to use this framework. (NOTE: Other python web frameworks will be added to this list in the future).
- Spring Wiki - Wikis are powerful ways to store and manage content, so we created a simple one as a demo!
- Spring Bot - Use Spring Python to build a tiny bot to manage the IRC channel of your open source project.
I like this simple and neat framework.
http://pypi.python.org/pypi/injector/
Dependency injection as a formal pattern is less useful in Python than in other languages, primarily due to its support for keyword arguments, the ease with which objects can be mocked, and its dynamic nature.
That said, a framework for assisting in this process can remove a lot of boiler-plate from larger applications. That's where Injector can help. It automatically and transitively provides keyword arguments with their values. As an added benefit, Injector encourages nicely compartmentalized code through the use of Module s.
While being inspired by Guice, it does not slavishly replicate its API. Providing a Pythonic API trumps faithfulness.
Introducing Wireup: Modern Dependency Injection for Python
magic-di: Dependency Injector with minimal boilerplate code, built-in support for FastAPI and Celery
Wireup 0.5: Modern Dependency Injection for Python
Lagom - dependency injector
In case anyone's interested, I've put together a DI framework "pylayer" in python that's fairly different from the alternatives I'm aware of (there aren't many). It includes a simple example at the bottom.
https://gist.github.com/johnhungerford/ccb398b666fd72e69f6798921383cb3f
What my project does
It allows you automatically construct dependencies based on their constructors.
The way it works is you define your dependencies as dataclasses inheriting from an Injectable class, where upstream dependencies are declared as dataclass attributes with type hints. Then you can just pass the classes to an Env object, which you can query for any provided type that you want to use. The Env object will construct a value of that type based on the Injectable classes you have provided. If any dependency needed to construct the queried type, it will generate an error message explaining what was missing and why it was needed.
Target audience
This is a POC that might be of interest to anyone who is uses or has wanted to use dependency injection in a Python project.
Comparison
https://python-dependency-injector.ets-labs.org/ is but complicated and unintuitive. pylayer is more automated and less verbose.
https://github.com/google/pinject is not maintained and seems similarly complicated.
https://itnext.io/dependency-injection-in-python-a1e56ab8bdd0 provides an approach similar to the first, but uses annotations to simplify some aspects of it. It's still more verbose and less intuitive, in my opinion, than pylayer.
Unlike all the above, pylayer has a relatively simple, functional mechanism for wiring dependencies. It is able to automate more by using the type introspection and the automated __init__ provided by dataclasses.
For anyone interested, my approach is based on Scala's ZIO library. Like ZIO's ZLayer type, pylayer takes a functional approach that uses memoization to prevent reconstruction of the same values. The main difference between pylayer and ZIO is that wiring and therefore validation is done at runtime. (Obviously compile-time validation isn't possible in Python...)
» pip install dependency-injector
» pip install inject