### Create Punq Container Instance Source: https://github.com/bobthemighty/punq/blob/main/README.md Initialize a new Punq dependency injection container. This is the entry point for managing application dependencies. The container must be explicitly created in your application's entrypoint. ```python import punq container = punq.Container() ``` -------------------------------- ### Register Arbitrary Object with Key Source: https://github.com/bobthemighty/punq/blob/main/README.md Register any object instance with a string key in the container. This is the simplest registration method for storing configuration values or singleton instances that don't require instantiation. ```python container.register("connection_string", instance="postgresql://...") ``` -------------------------------- ### Register with Arguments at Registration Time Source: https://github.com/bobthemighty/punq/blob/main/README.md Provide constructor arguments during registration without adding them to the container. These arguments will be used whenever the service is resolved. ```python container.register(Greeter, FileWritingGreeter, path="/tmp/foo", greeting="Hello world") ``` -------------------------------- ### Register Singleton Instance Source: https://github.com/bobthemighty/punq/blob/main/README.md Register a specific object instance as a singleton in the container. The same instance will be returned every time the service is resolved, ensuring single instantiation. ```python class FileWritingGreeter: def __init__(self, path, greeting): self.path = path self.message = greeting self.file = open(self.path, 'w') def greet(self): self.file.write(self.message) one_true_greeter = FileWritingGreeter("/tmp/greetings", "Hello world") container.register(Greeter, instance=one_true_greeter) ``` -------------------------------- ### Resolve Service and Call Method Source: https://github.com/bobthemighty/punq/blob/main/README.md Retrieve a registered service from the container and invoke its methods. The container automatically instantiates the concrete implementation class. ```python config = container.resolve(ConfigReader).get_config() ``` -------------------------------- ### Resolve with Runtime Arguments Source: https://github.com/bobthemighty/punq/blob/main/README.md Resolve a service while providing additional arguments at resolution time. Arguments not provided at registration time can be supplied when resolving the dependency. ```python container.register(Greeter, FileWritingGreeter) greeter = container.resolve(Greeter, path="/tmp/foo", greeting="Hello world") ``` -------------------------------- ### Register Service with Implementation Class Source: https://github.com/bobthemighty/punq/blob/main/README.md Register an abstract service class with a concrete implementation. The container will resolve requests for the service type by returning instances of the implementation class. ```python class ConfigReader: def get_config(self): pass class EnvironmentConfigReader(ConfigReader): def get_config(self): return { "logging": { "level": os.env.get("LOGGING_LEVEL", "debug") }, "greeting": os.env.get("GREETING", "Hello world") } container.register(ConfigReader, EnvironmentConfigReader) ``` -------------------------------- ### Register Service with Nested Dependencies Source: https://github.com/bobthemighty/punq/blob/main/README.md Register a service that depends on other registered services. Punq automatically resolves and injects dependencies based on type hints in the constructor. ```python class Greeter: def greet(self): pass class ConsoleGreeter(Greeter): def __init__(self, config_reader: ConfigReader): self.config = config_reader.get_config() def greet(self): print(self.config['greeting']) container.register(Greeter, ConsoleGreeter) container.resolve(Greeter).greet() ``` -------------------------------- ### Resolve Registered Object from Container Source: https://github.com/bobthemighty/punq/blob/main/README.md Retrieve a previously registered object from the container using its key or type. Returns the exact instance or type that was registered. ```python conn_str = container.resolve("connection_string") ``` -------------------------------- ### Register Class Without Base Interface Source: https://github.com/bobthemighty/punq/blob/main/README.md Register a concrete class directly without an abstract base class. Useful for simple services that don't need polymorphism. Dependencies are still automatically injected. ```python class Greeter: def __init__(self, config_reader: ConfigReader): self.config = config_reader.get_config() def greet(self): print(self.config['greeting']) container.register(Greeter) container.resolve(Greeter).greet() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.