Skip to main content

Lagom is a dependency injection container designed to give you 'just enough' help with building your dependencies.

Project description

Lagom

Scrutinizer Code Quality Code Coverage PyPI

What

Lagom is a dependency injection container designed to give you "just enough" help with building your dependencies. The intention is that almost all of your code doesn't know about or rely on lagom. Lagom will only be involved at the top level to pull everything together.

Features

  • Type based auto wiring with zero configuration.
  • Fully based on types. Strong integration with mypy.
  • Minimal changes to existing code.
  • Integration with a few common web frameworks.
  • Support for async python.
  • Thread-safe at runtime

You can see a comparison to other frameworks here

🎉 Version 2.0.0 is now released! 🎉

For users of python 3.7 and above this should require no changes. Full details can be found in the release notes upgrade instructions.

Installation

pip install lagom
# or: 
# pipenv install lagom
# poetry add lagom

Note: if you decide to clone from source then make sure you use the latest version tag. The master branch may contain features that will be removed.

For the versioning policy read here: SemVer in Lagom

Usage

Everything in Lagom is based on types. To create an object you pass the type to the container:

container = Container()
some_thing = container[SomeClass]

Auto-wiring (with zero configuration)

Most of the time Lagom doesn't need to be told how to build your classes. If the __init__ method has type hints then lagom will use these to inject the correct dependencies. The following will work without any special configuration:

class MyDataSource:
    pass
    
class SomeClass:
   #                        👇 type hint is used by lagom
   def __init__(datasource: MyDataSource):
      pass

container = Container()
some_thing = container[SomeClass] # An instance of SomeClass will be built with an instance of MyDataSource provided

and later if you extend your class no changes are needed to lagom:

class SomeClass:
    #                                                👇 This is the change.
    def __init__(datasource: MyDataSource, service: SomeFeatureProvider):
        pass

# Note the following code is unchanged
container = Container()
some_thing = container[SomeClass] # An instance of SomeClass will be built with an instance of MyDataSource provided

Singletons

You can tell the container that something should be a singleton:

container[SomeExpensiveToCreateClass] = SomeExpensiveToCreateClass("up", "left")

Explicit build instructions when required

You can explicitly tell the container how to construct something by giving it a function:

container[SomeClass] = lambda: SomeClass("down", "spiral")

All of this is done without modifying any of your classes. This is one of the design goals of lagom.

Hooks in to existing systems

A decorator is provided to hook top level functions into the container.

@bind_to_container(container)
def handle_move_post_request(request: typing.Dict, game: Game = lagom.injectable):
    # do something to the game
    return Response()

(There's also a few common framework integrations provided here)

Full docs here here

Contributing

Contributions are very welcome. Please see instructions here

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

lagom-2.2.0b1-py3-none-any.whl (36.1 kB view hashes)

Uploaded Python 3

lagom-2.2.0b1-cp311-cp311-win_amd64.whl (142.7 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

lagom-2.2.0b1-cp311-cp311-win32.whl (126.3 kB view hashes)

Uploaded CPython 3.11 Windows x86

lagom-2.2.0b1-cp311-cp311-musllinux_1_1_x86_64.whl (294.7 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

lagom-2.2.0b1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (300.3 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

lagom-2.2.0b1-cp311-cp311-macosx_10_9_x86_64.whl (178.3 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

lagom-2.2.0b1-cp310-cp310-win_amd64.whl (143.1 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

lagom-2.2.0b1-cp310-cp310-win32.whl (126.7 kB view hashes)

Uploaded CPython 3.10 Windows x86

lagom-2.2.0b1-cp310-cp310-musllinux_1_1_x86_64.whl (298.7 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

lagom-2.2.0b1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (304.4 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

lagom-2.2.0b1-cp310-cp310-macosx_10_9_x86_64.whl (180.5 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

lagom-2.2.0b1-cp39-cp39-win_amd64.whl (143.0 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

lagom-2.2.0b1-cp39-cp39-win32.whl (126.6 kB view hashes)

Uploaded CPython 3.9 Windows x86

lagom-2.2.0b1-cp39-cp39-musllinux_1_1_x86_64.whl (296.4 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

lagom-2.2.0b1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (302.8 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

lagom-2.2.0b1-cp39-cp39-macosx_10_9_x86_64.whl (180.2 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

lagom-2.2.0b1-cp38-cp38-win_amd64.whl (142.2 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

lagom-2.2.0b1-cp38-cp38-win32.whl (126.1 kB view hashes)

Uploaded CPython 3.8 Windows x86

lagom-2.2.0b1-cp38-cp38-musllinux_1_1_x86_64.whl (295.4 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

lagom-2.2.0b1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (300.0 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

lagom-2.2.0b1-cp38-cp38-macosx_10_9_x86_64.whl (177.5 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

lagom-2.2.0b1-cp37-cp37m-win_amd64.whl (137.8 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

lagom-2.2.0b1-cp37-cp37m-win32.whl (123.5 kB view hashes)

Uploaded CPython 3.7m Windows x86

lagom-2.2.0b1-cp37-cp37m-musllinux_1_1_x86_64.whl (238.1 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

lagom-2.2.0b1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (243.0 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

lagom-2.2.0b1-cp37-cp37m-macosx_10_9_x86_64.whl (170.8 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

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