How to use the punq.__init__.Registration function in punq

To help you get started, we’ve selected a few punq examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bobthemighty / punq / punq / __init__.py View on Github external
>>> class EmailSender:
                ...     def send(self, msg):
                ...         pass
                ...
                >>> class SmtpEmailSender(EmailSender):
                ...     def send(self, msg):
                ...         print("Sending message via smtp: " + msg)
                ...
                >>> container.register(EmailSender, SmtpEmailSender)
                
                >>> instance = container.resolve(EmailSender)
                >>> instance.send("Hello")
                Sending message via smtp: Hello
        """
        self.__registrations[service].append(
            Registration(
                service, scope, impl, self._get_needs_for_ctor(impl), resolve_args
            )
github bobthemighty / punq / punq / __init__.py View on Github external
>>> container = Container()
                >>> class FileReader:
                ...     def read(self):
                ...         # Assorted legerdemain and rigmarole
                ...         pass
                ...
                >>> container.register(FileReader)
                
        """
        if not inspect.isclass(service):
            raise InvalidRegistrationException(
                "The service %s can't be registered as its own implementation"
                % (repr(service))
            )
        self.__registrations[service].append(
            Registration(service, scope, service, self._get_needs_for_ctor(service), {})
        )