def test_mixed_priv_and_public_args(self): @inject(Definitions({'param': 10}), Autowiring()) def fn(thing: Thing): self.assertIsInstance(thing, Thing) self.assertIsInstance(thing.reality, Reality) self.assertIs(thing.param, 10) fn()
def test_field_injection(self): inj = Injector(Definitions(config), Autowiring()) thing = inj.get(Thing, 'my-param') self.assertIsInstance(thing, Thing) self.assertEqual(thing.param, 'my-param') self.assertIsInstance(thing.reality, Reality) self.assertEqual(thing.not_injected, 'ABC') self.assertEqual(thing.cfg, 'DEF')
pass def dog_builder(): """ Custom instantiation """ dog = Dog() dog.random = randint(50, 100) return dog defs = { House: Singleton(), Cat: Instance(), Type[Cat]: Factory(), Dog: CustomInstance(dog_builder), Type[Dog]: Factory(), } @inject(Definitions(defs)) def fn(house: House, cat_factory: Type[Cat], dog_factory: Type[Dog]): cat = cat_factory() dog = dog_factory() print('house:', house) print('cat:', cat) print('dog:', dog) fn()
from examples.pet_delivery.classes import Bob, Vehicle, PetPicker, Pet, Mike, Van, Cat, Truck, Dog from examples.pet_delivery.defs import pet_defs, vehicle_defs, common_defs from wirinj import Singleton, Factory, Definitions, inject world_two_defs = { (Bob, Vehicle): Singleton(Van), (Bob, PetPicker, Type[Pet]): Factory(Cat), (Mike, Type[Vehicle]): Factory(Truck), (Mike, PetPicker, Type[Pet]): Factory(Dog), } world_two = Definitions( pet_defs, vehicle_defs, common_defs, world_two_defs, ) logging.basicConfig(format='%(message)s', level=logging.INFO) @inject(world_two) def do(bob: Bob, mike: Mike): bob.deliver(100, 5, False) bob.deliver(50, 200, True) mike.deliver(20, 1000, True) do()
class Cat: pass class Dog: pass class Horse: pass deps = { Cat: Instance(), } report = AutowiringReport() @inject(Definitions(deps), Autowiring(report)) def fn(cat: Cat, dog: Dog, horse_factory: Type[Horse]): print(cat.__class__.__name__) print(dog.__class__.__name__) horse = horse_factory() print(horse.__class__.__name__) fn() print(report.get())
from examples.report.cat_example_classes import Cat, Head from wirinj import Injector, Definitions, Instance inj = Injector(Definitions({ Cat: Instance(), Head: Instance(), })) cat2 = inj.get(Cat)
from typing import Type from wirinj import Instance, Factory, Definitions, inject, INJECTED class Cat: sound: str = INJECTED weight: float = INJECTED config = { 'sound': 'meow', 'weight': 5, } wiring = {Cat: Instance(), Type[Cat]: Factory()} @inject(Definitions(config, wiring)) def fn(cat_factory: Type[Cat]): cat = cat_factory() print(f'Cat: {cat.sound}, {cat.weight}') fn()
from examples.report.cat_example_classes import Nail, Cat from wirinj import Injector, Definitions, Autowiring, Instance class BrokenNail(Nail): def __init__(self): raise AttributeError('Broken leg') inj = Injector( Definitions({Nail: Instance(BrokenNail)}), Autowiring(use_singletons=False), ) inj.get(Cat)
my_service: MyService = INJECTED my_config: str = INJECTED def __str__(self): return f'<MyObject> -> my_config: "{self.my_config}"' \ f', param: {self.param}, my_service: {self.my_service}' def __init__(self, param): self.param = param config = { 'my_config': 'some conf', } # Use a function to get access to the root dependencies @inject(Definitions(config), Autowiring()) def do( my_service: MyService, my_object_factory: Type[MyObject] ): print(my_service) my_object1 = my_object_factory(10) print(my_object1) # Inject and run it do()
from typing import Type from examples.pet_delivery.classes import Bob, Vehicle, Car, PetPicker, Pet, Bird, Mike, Van, Cat from examples.pet_delivery.defs import pet_defs, vehicle_defs, common_defs from wirinj import Singleton, Factory, Definitions, inject world_one_defs = { (Bob, Vehicle): Singleton(Car), (Bob, PetPicker, Type[Pet]): Factory(Bird), (Mike, Type[Vehicle]): Factory(Van), (Mike, PetPicker, Type[Pet]): Factory(Cat), } world_one = Definitions( pet_defs, vehicle_defs, common_defs, world_one_defs, ) logging.basicConfig(format='%(message)s', level=logging.INFO) @inject(world_one) def do(bob: Bob, mike: Mike): bob.deliver(100, 5, False) bob.deliver(50, 200, True) mike.deliver(20, 1000, True) do()