コード例 #1
0
ファイル: definition_types.py プロジェクト: davidcim/wirinj
class Dog:
    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)

コード例 #2
0
ファイル: split_definitions.py プロジェクト: davidcim/wirinj
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()
コード例 #3
0
import logging
from typing import Type

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)
コード例 #4
0
ファイル: defs.py プロジェクト: davidcim/wirinj
    Cat: Instance(),
    Bird: Instance(),
    (Dog, 'sound'): 'Woof',
    (Dog, 'weight'): 10,
    (Cat, 'sound'): 'Meow',
    (Cat, 'weight'): 5,
    (Bird, 'sound'): 'Chirp',
    (Bird, 'weight'): 0.1,
}

vehicle_defs = {
    Engine: Instance(),
    Plate: Instance(),
    Wheel: Instance(),
    Container: Instance(),
    Type[Engine]: Factory(),
    Type[Plate]: Factory(),
    Type[Wheel]: Factory(),
    Type[Container]: Factory(),
    VehicleBuilder: Singleton(),
    (Engine, 'mount_sound'): 'RRRRoarrr',
    (Plate, 'mount_sound'): 'plaf',
    (Wheel, 'mount_sound'): 'pffff',
    (Container, 'mount_sound'): 'BLOOOOM',
    Car: Instance(),
    (Car, 'max_load_weight'): 10,
    (Car, 'recipe'): {
        'engines': 1,
        'plates': 6,
        'wheels': 4,
    },
コード例 #5
0
ファイル: inject_decorator.py プロジェクト: davidcim/wirinj
from wirinj import inject, Definitions, Instance, Singleton, Factory


class Cat:
    pass


class Dog:
    pass


defs = {
    'sound': 'Meow',
    Cat: Instance(),
    Dog: Singleton(),
    'cat_factory': Factory(Cat),
}


@inject(Definitions(defs))
def fn(cat1: Cat, cat2: Cat, dog: Dog, sound, cat_factory):
    print('sound:', sound)
    print('cat1:', cat1)
    print('cat2:', cat2)
    print('cat1 = cat2:', cat1 is cat2)
    print('dog:', dog)

    cat3 = cat_factory()
    print('cat3:', cat3)

コード例 #6
0
ファイル: example_1.py プロジェクト: davidcim/wirinj
import logging
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)
コード例 #7
0
    my_service: MyService = INJECTED
    my_config: str = INJECTED

    def __str__(self):
        return f'<{self.__class__.__name__}> -> my_config: "{self.my_config}", param: {self.param}, my_service: {self.my_service}'

    def __init__(self, param):
        self.param = param


# Wiring definitions
defs = {
    'my_config': 'Some config',
    MyService: Singleton(),
    MyObject: Instance(),
    Type[MyObject]: Factory(),
}


# The decorator will inject a dependency on each function parameter based on its name and/or annotation type
@inject(Definitions(defs))
def do(my_service: MyService, my_object_factory: Type[MyObject]):
    print('my_service = {}'.format(my_service))

    my_object1 = my_object_factory(10)
    print('my_object1 = {}'.format(my_object1))


# Inject and run it
do()
コード例 #8
0
class Cat:
    def __init__(self, name, color=None, weight=None):
        self.name = name
        self.color = color
        self.weight = weight

    def __str__(self):
        return f'{self.name} is a {self.color} pounds {self.weight} cat.'


def create_cat(name, color):
    return Cat(name, color, randint(4, 20))


defs = {
    'color': 'black',
    Cat: CustomInstance(create_cat),
    Type[Cat]: Factory(),
}


@inject(Definitions(defs))
def fn(factory: Type[Cat]):
    cat = factory('Tom')
    print(cat)
    cat2 = factory('Sam')
    print(cat2)


fn()