{"id":6611,"library":"dsnparse","title":"DSN Parser","description":"dsnparse is a Python library designed to easily parse Data Source Name (DSN) connection strings, similar to URLs. It's used in projects like 'prom' and 'morp' and aims to provide an interface familiar to users of Python's `urlparse` module. The current version is 0.4.0, and it maintains an infrequent but active release cadence, with the last update in October 2025.","status":"active","version":"0.4.0","language":"en","source_language":"en","source_url":"https://github.com/Jaymon/dsnparse","tags":["dsn","url","parser","configuration","database"],"install":[{"cmd":"pip install dsnparse","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"Commonly imported directly for DSN string parsing.","symbol":"parse","correct":"from dsnparse import parse"},{"note":"Used for parsing DSNs from environment variables.","symbol":"parse_environ","correct":"from dsnparse import parse_environ"},{"note":"Base class for customizing parsing results.","symbol":"ParseResult","correct":"from dsnparse import ParseResult"}],"quickstart":{"code":"import dsnparse\nimport os\n\n# Example 1: Basic DSN parsing\ndsn = \"prom.interface.postgres.Interface://testuser:testpw@localhost:1234/testdb?appname=my_app#frag1\"\nr = dsnparse.parse(dsn)\n\nprint(f\"Scheme: {r.scheme}\") # prom.interface.postgres.Interface\nprint(f\"Username: {r.username}\") # testuser\nprint(f\"Password: {r.password}\") # testpw\nprint(f\"Host: {r.host}\") # localhost\nprint(f\"Port: {r.port}\") # 1234\nprint(f\"Host Location: {r.hostloc}\") # localhost:1234\nprint(f\"Paths: {r.paths}\") # ['testdb']\nprint(f\"Query: {r.query}\") # appname=my_app\nprint(f\"Fragment: {r.fragment}\") # frag1\nprint(f\"Get Query Param 'appname': {r.get_query('appname')}\") # my_app\n\n# Example 2: Parsing from environment variable\nos.environ['MY_DB_DSN'] = os.environ.get('MY_DB_DSN', 'sqlite:///:memory:')\nenv_dsn = dsnparse.parse_environ('MY_DB_DSN')\nprint(f\"Env DSN Scheme: {env_dsn.scheme}\") # sqlite\nprint(f\"Env DSN Path: {env_dsn.path}\") # :memory:\n\n# Example 3: Custom ParseResult class\nclass MyResult(dsnparse.ParseResult):\n    def configure(self):\n        # Expose 'scheme' as 'interface' for custom logic\n        self.interface = self.scheme\n\ncustom_dsn = \"MyInterface://customuser:custompass@host:5432/path\"\nr_custom = dsnparse.parse(custom_dsn, parse_class=MyResult)\nprint(f\"Custom DSN Type: {type(r_custom)}\") # <class '__main__.MyResult'>\nprint(f\"Custom DSN Interface: {r_custom.interface}\") # MyInterface","lang":"python","description":"This quickstart demonstrates basic DSN parsing, extracting information from environment variables, and customizing the parsing result class."},"warnings":[{"fix":"Use conditional checks or provide default values: `user = r.username or 'guest'`","message":"Accessing DSN components (e.g., `username`, `password`, `port`, `fragment`, `query`) that are not present in the DSN string will return `None`. Always check for `None` or use default values if these components are optional in your application.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure `r.paths` is not empty before accessing `r.paths[0]`, or iterate through `r.paths`.","message":"The `paths` attribute returns a list of path segments, even if there is only one. If you expect a single path, access it as `r.paths[0]` after checking the list is not empty.","severity":"gotcha","affected_versions":"All"},{"fix":"Choose the appropriate attribute based on whether you need the port included with the host.","message":"Be aware of the distinction between `r.host` and `r.hostloc`. `r.host` will provide only the hostname, while `r.hostloc` will include the port if present (e.g., 'localhost' vs 'localhost:1234').","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure the environment variable is set or wrap the call in a `try-except KeyError` block, or provide a fallback mechanism for missing variables.","message":"When parsing from environment variables using `dsnparse.parse_environ(KEY)`, the library expects the variable to be set. If the environment variable is not found, it will raise a `KeyError` unless handled by the calling code.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}