class Trike(Component): """ Inherits Component features. Automatically is being injected with DI instances using Inject descriptor. """ front_wheel: WheelInterface = Inject() left_wheel = Inject(interface=WheelInterface) right_wheel = Inject(name='right') frame: FrameInterface = Inject(name='frame') @property def components(self): return { 'front_wheel': self.front_wheel.name, 'left_wheel': self.left_wheel.name, 'right_wheel': self.right_wheel.name, 'frame': self.frame.name, } @inject def method_with_dependencies( self, frame: FrameInterface = Inject(), front_wheel=Inject(name='front'), rear_wheel=Inject(interface=WheelInterface) ): pass
def method_with_dependencies( self, frame: FrameInterface = Inject(), front_wheel=Inject(name='front'), rear_wheel=Inject(interface=WheelInterface) ): pass
def construct_successfully( self, frame: FrameInterface = Inject(), front_wheel=Inject(name="front"), rear_wheel=Inject(interface=WheelInterface), ): # noinspection PyDataclass return {"frame": frame.name, "front": front_wheel.name, "rear": rear_wheel.name}
def compose_success(self, frame: FrameInterface = Inject(), front_wheel=Inject(name='front'), rear_wheel=Inject(interface=WheelInterface)): return { 'frame': frame.name, 'front': front_wheel.name, 'rear': rear_wheel.name }
class RoadBike: frame: FrameInterface = Inject() wheel: WheelInterface = Inject(name='wheels') def __init__(self, container: Container): self.container = container def build_bike(self): return f'Frame: {self.frame}\nWheels: {self.wheel}'
class AnotherBike: front_wheel: WheelInterface = Inject() left_wheel = Inject(interface=WheelInterface) right_wheel = Inject(name='right') def __init__(self, container: Container): self.container = container def build_bike(self): return f'Wheels: {self.front_wheel}, {self.left_wheel}, {self.right_wheel}.'
def add_team_member( request_data: RequestModel, teams: Repository = Inject(qualifier=Team), racers: Repository = Inject(qualifier=Racer), ): team: Team = teams.find(request_data['team_id']) racer: Racer = racers.find(request_data['racer_id']) team.members.append(racer) teams.update(team) return ResponseModel(data=request_data, errors={})
def construct_successfully( self, frame: FrameInterface = Inject(), front_wheel=Inject(name='front'), rear_wheel=Inject(interface=WheelInterface)): # noinspection PyDataclass return { 'frame': frame.name, 'front': front_wheel.name, 'rear': rear_wheel.name }
class AcceptInvitation(BaseInteractor): user_repo: repositories.UserRepo = Inject() invitation_repo: repositories.InvitationRepo = Inject() validators = ( validators.is_invitation_valid, validators.is_email_taken, ) def execute(self, request: RequestModel) -> ResponseModel: invitation = self.invitation_repo.get_by_token(request['token']) data = invitation.accept() return ResponseModel(data=data, errors={})
class InviteUser(BaseInteractor): user_repo: repositories.UserRepo = Inject() invitation_repo: repositories.InvitationRepo = Inject() @property def validators(self): return (self.validate_email_taken, ) def validate_email_taken(self, request, dependencies): self.user_repo.is_email_taken(request['email']) def execute(self, request: RequestModel) -> ResponseModel: invitation = self.invitation_repo.create_and_add( email=request['email']) return ResponseModel(data=self.invitation_repo.serialize(invitation), errors={})
class AddTeamMember(Interactor): teams: Repository = Inject(qualifier=Team) racers: Repository = Inject(qualifier=Racer) def __init__(self, validators): # testing properties; normally, validators would be just a property with functions declared self.error_handler = mock.MagicMock(return_value={'error': 42}) self.validators = validators def handle_error(self, error: Exception, request: RequestModel): return self.error_handler(error=error, request=request) def execute(self, request: RequestModel) -> ResponseModel: team: Team = self.teams.find(request['team_id']) racer: Racer = self.racers.find(request['racer_id']) team.members.append(racer) self.teams.update(team) return ResponseModel(data=request, errors={})
class UserRepo(Repository): schema: Schema = Schema(entity=entities.User) authentication: AuthenticationService = Inject() @property def is_from_my_organization(self): organization = self.authentication.user.organization return where('organization.id') == organization.id def get_by_email(self, email): data = self.dao.filter( where('email') == email & entities.User.is_active & self.is_from_my_organization).one() return self.create(**data) def is_email_taken(self, email): return self.dao.filter(where('email') == email).exists()
class A: descriptor = Inject()
class NoAnnotationBike(Component): wheel = Inject()
class Bike(Component): wheel: WheelInterface = Inject()
def f(dependency=Inject(name="dependency"), **kwargs): # noinspection PyCallingNonCallable dependency(**kwargs) return 42
def build_frame(self, frame=Inject()): pass
def is_after_start(self, clock: IClockService = Inject()): return clock.now() > self.start_date
class Bike: wheel: WheelInterface = Inject()
def build(self, frame: FrameInterface = Inject(), wheel: WheelInterface = Inject()): return f'Frame: {frame}\nWheels: {wheel}'
class Foo(Component): qualifier = "qualifier" dependency = Bar bar = Inject(name="bar", get_qualifier=get_qualifier) baz = Inject(interface=Bar, get_qualifier=get_qualifier)
class Foo(Component): qualifier = 'qualifier' dependency = Bar bar = Inject(name='bar', get_qualifier=get_qualifier) baz = Inject(interface=Bar, get_qualifier=get_qualifier)
def build(self, frame: FrameInterface = Inject(), wheel=Inject(name='g_wheel')): return f'Frame: {frame}\nWheels: {wheel}'
class NoAnnotationBike: wheel = Inject() def __init__(self, container: Container): self.container = container
def construct_without_identifier(self, frame=Inject()): pass
def compose_no_identifier(self, frame=Inject()): pass
def f(dependency=Inject(name="dependency"), **kwargs): dependency(**kwargs) return 42