{"library":"simple-di","title":"Simple-DI","description":"Simple-DI is a lightweight Python library for dependency injection. It helps manage application components and their dependencies in a clear, testable, and maintainable way using a container pattern. As of version 0.1.5, it provides core features like `Container` for organizing providers and `Provider` for defining how dependencies are created. Releases are infrequent, driven by new features or bug fixes.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install simple-di"],"cli":null},"imports":["from simple_di import Container","from simple_di import Provider"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"from simple_di import Container, Provider\n\nclass MyConfig(Container):\n    debug_mode = Provider(bool, default=False)\n    database_url = Provider(str, default=\"sqlite:///test.db\")\n\nclass MyDependencies(Container):\n    # Instantiate nested containers\n    config = MyConfig()\n\n    # A factory function for a database client\n    def _create_db_client(url: str):\n        print(f\"[DB] Connecting to {url}...\")\n        return f\"DatabaseClient({url})\"\n\n    # Define a provider for the database client, depending on config.database_url\n    db_client = Provider(_create_db_client, config.database_url)\n\n    # A factory function for the main application\n    def _create_app(client):\n        print(f\"[App] Creating app with client: {client}\")\n        return f\"MyApp({client})\"\n\n    # Define a provider for the app, depending on db_client\n    app = Provider(_create_app, db_client)\n\n# Instantiate the main dependency container\nmy_app_deps = MyDependencies()\n\n# Accessing dependencies (calling .get() is crucial to resolve them)\nprint(\"\\n--- First Resolution ---\")\ndb_client_instance = my_app_deps.db_client.get()\nprint(f\"Retrieved DB Client: {db_client_instance}\")\n\napp_instance = my_app_deps.app.get()\nprint(f\"Retrieved App: {app_instance}\")\n\n# Overriding a configuration value and observing changes\nprint(\"\\n--- Overriding Configuration ---\")\nmy_app_deps.config.database_url.set(\"postgresql://user:pass@host/prod_db\")\n\n# Accessing again will re-evaluate the db_client provider due to dependency change\nprint(\"\\n--- Second Resolution (after override) ---\")\nnew_db_client_instance = my_app_deps.db_client.get()\nprint(f\"Retrieved NEW DB Client: {new_db_client_instance}\")\n\n# Accessing app again will use the new db client\nnew_app_instance = my_app_deps.app.get()\nprint(f\"Retrieved NEW App: {new_app_instance}\")","lang":"python","description":"This quickstart demonstrates how to define a `Container` for configuration, nest containers, define `Provider` instances with dependencies, resolve dependencies using `.get()`, and dynamically override configuration values.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}