• Stars
    star
    277
  • Rank 148,875 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created over 5 years ago
  • Updated almost 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ“ฆ Autowiring dependency injection container for python 3

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

More Repositories

1

chaos-spawn

โ˜„๏ธ Chaotic spawning for elixir
Elixir
88
star
2

Tale

PHP
23
star
3

unit_fun

๐Ÿ“ Dimension based safety in elixir
Elixir
21
star
4

talepy

๐Ÿ“šCoordinate "transactions" across a number of services in python
Python
21
star
5

MonoSnag

๐Ÿ› Monolog handler for bugsnag.
PHP
20
star
6

DiceApi

๐ŸŽฒ Heroku app to roll some dice
PHP
16
star
7

british_food_generator

๐Ÿฅง Generates classic British dishes
Python
13
star
8

php-curry

๐Ÿ› function currying for php
PHP
7
star
9

spl-fix

The php spl has some really cool feaures but also some bugs/gotchas. This is a library to wrap and hopefully reduce the pain causes by the spl.
PHP
5
star
10

somewhen.py

time is hard. just don't bother
Python
5
star
11

types-at-the-edges-minikata

Python
4
star
12

isseven

Python
4
star
13

meadsteve.github.io

GitHub Pages Repo (https://blog.meadsteve.dev/)
HTML
3
star
14

advent-of-code-2020

These are my advent of code solutions for 2020
Crystal
2
star
15

phlocal-dynamo

convenience wrapper for running local copies of dynamo db
PHP
2
star
16

stupid_password_generator

Generates "passwords" that are all technically "one two three four five six seven eight nine"
Python
2
star
17

chaos-spawn-example

Example project for chaos-spawn
Elixir
2
star
18

example-phpci-plugin

Example structure for external plugin
PHP
1
star
19

cli-dice-game-code-along

๐ŸŽฒ code along for building a cli dice game
1
star
20

are-you-a-banana

JavaScript
1
star
21

breath-timer

TypeScript
1
star
22

RayGunolog

Monolog Handler connection to raygun.io
PHP
1
star
23

MonWorkGo

Simple work queue built on top of mongodb
PHP
1
star
24

wyrd

Python
1
star
25

advent-of-code-2019

๐ŸŒฒ๐ŸŽ Lets see how far behind I end up this year
Python
1
star