Skip to content

A thoughtful dependency injection framework ๐Ÿ’‰

License

Notifications You must be signed in to change notification settings

ZeroGachis/nurse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

64 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Nurse

Outline

  1. Overview
  2. Installation
  3. Usage
  4. License

Overview

Nurse is a dependency injection framework with a small API that uses type annotations to manage dependencies in your codebase.

Installation

Nurse is a Python3-only module that you can install via Poetry

poetry add nurse

It can also be installed with pip

pip3 install nurse

Usage

Nurse stores the available dependencies into a service catalog, that needs to be filled-in generally at the startup of your application.

import nurse

# A user defined class that will be used accross your application
class Player:

    @property
    def name(self) -> str:
        return "Leeroy Jenkins"

# Now, add it to nurse service catalog in order to use it later in your application
nurse.serve(Player())

By default, dependencies are referenced by their concrete type, but you can also serve them via one of their parent class.

import nurse

class Animal:
    pass

class AngryAnimal(Animal):

    @property
    def roar(self) -> str:
        return "Grrr! ๐Ÿฆ"

nurse.serve(AngryAnimal(), through=Animal)

Once you filled-in the service catalog with your different components, your can declare them as dependencies to any of your class.

@dataclass
class Game:
    player: Player
    enemy: Animal

    def welcome_hero(self):
        print(f"Welcome {self.player.name} !")

    def summon_monster(self):
        print(self.enemy.roar)

player = nurse.get(Player)
animal = nurse.get(Animal)
game = Game(player=player, animal=animal)

game.welcome_hero()
# Welcome Leeroy Jenkins !
game.summon_monster()
# Grrr! ๐Ÿฆ

License

Nurse is released into the Public Domain. ๐ŸŽ‰

About

A thoughtful dependency injection framework ๐Ÿ’‰

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published