Pychkari
The Problem
Python is one of the most popular languages for a wide variety of applications. And for a very good reason: versatility. Python is used for everything ranging from one-off scripts to full blown services. This versatility, however, seems to have come at a cost. Unlike many of the “traditional” languages Python doesn’t have very well established and widely used design and architectural patterns. If one were to develop a web service using Java, they’d have Spring, Hibernate, and family. C# has ASP.NET, Entity Framework, etc. Python, on the other hand, gives the programmer complete freedom of choice on how to structure their code. While seasoned developers use this freedom effectively, others end up with a hacked together mess. There’s also a misconception that Python isn’t a true Object Oriented Language and hence general OOP principles like SOLID cannot be applied effectively. This is obviously wrong.
A moderately sized codebase can take a lot of help from a good Dependency Injection library. I came up with a very simple list of requirements:
- Python 3 support. Python 2 is effectively dead and I’d rather not have the overhead of compatibility layer.
- Straightforward and memorable API. Succinctness is one of the major strengths of Python. Our DI library should not require a bunch of factories and wrappers.
- Compatibility. As much as us developers like to start from scratch, it is counterproductive. Our library should work with any reasonably structured codebase without significant refactor. Also, we should be able to integrate it gradually with our code.
Following things are nice to have as well:
- Low footprint. Because very few things are better than a tiny library with no dependencies.
- No lock in. It is pain to move to an alternative if your codebase is littered with decorators from the current library.
Unfortunately, I could not find a package that ticked all the boxes. So, I set out to write my own.
Introducing Pychkari
Pychkari is a Dependency Injection Manager package for Python. Following snippet illustrates Pychkari’s core features:
Features
Let’s see how Pychkari addresses our requirements.
Python 3 Support
Pychkari is built using Python 3. It supports any Python version after 3.4.
Succinct API
Pychkari DI container has easy to understand interface with register()
and get()
methods.
Compatibility
Introducing Pychkari into existing codebase is as simple as registering your services into the container. The container will automagically detect and inject dependencies based on constructor params. It will even handle snake_case
, PascalCase
, and camelCase
gracefully. So, dep_one
, DepOne
, and depOne
all resolve to the service DepOne
out of box.
Want to override this behavior? Prefer to be more explicit? Just add type annotations to your constructor params like line 6 in the code above.
Footprint
Pychkari has no dependencies beyond Python’s standard library. Pychkari itself is under 5KB!
No Lock In
Pychkari doesn’t require you to change your naming conventions, coding style, etc. More importantly, it doesn’t require you to add decorators, etc. to your classes. The classes remain completely portable.
So, there you have it. Download Pychkari and give it a try for your next project.