How to use the punq.__init__.Scope.singleton 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 DataAccessLayer:
            ...     pass
            ...
            >>> class SqlAlchemyDataAccessLayer(DataAccessLayer):
            ...     def __init__(self, engine: SQLAlchemy.Engine):
            ...         pass
            ...
            >>> container.register(
            ...     DataAccessLayer,
            ...     instance=SqlAlchemyDataAccessLayer(create_engine("sqlite:///"))
            ... )
            
        """
        self.__registrations[service].append(
            Registration(service, Scope.singleton, lambda: instance, {}, {})
        )
github bobthemighty / punq / punq / __init__.py View on Github external
for k, v in registration.needs.items()
            if k != "return" and k not in registration.args and k not in resolution_args
        }
        args.update(registration.args)

        target_args = inspect.getfullargspec(registration.builder).args
        if "self" in target_args:
            target_args.remove("self")
        condensed_resolution_args = {
            key: resolution_args[key] for key in resolution_args if key in target_args
        }
        args.update(condensed_resolution_args or {})

        result = registration.builder(**args)

        if registration.scope == Scope.singleton:
            self._singletons[registration.service] = result

        context[registration.service] = result

        return result