分析DSN URL

dsnparse的Python项目详细描述


解析dsn connection url strings。我保留了 复制DSN解析代码 prom和morp,我意识到 我打算在未来需要更多的DSN网址,所以我决定 创造一些更模块化的东西。

这是 dj-database-url

所以,现在您可以创建这样的DSN:

scheme://user:pass@host:port/path?query=query_val#fragment

例如,让我们看看prom dsn:

prom.interface.postgres.Interface://testuser:testpw@localhost/testdb

现在让我们分析一下:

importdsnparsedsn="prom.interface.postgres.Interface://testuser:testpw@localhost:1234/testdb"r=dsnparse.parse(dsn)print(r.scheme)# prom.interface.postgres.Interfaceprint(r.username)# testuserprint(r.password)# testpwprint(r.host)# localhostprint(r.hostloc)# localhost:1234print(r.paths)# ['testdb']

此外,dsnparse可以方便地使用环境变量:

r=dsnparse.parse_environ('ENVIRONMENT_VARIABLE_NAME')

我试图使接口与urlparse非常相似,因此它将 感觉熟悉使用。

示例

默认情况下,dsnparse.parse(dsn)返回一个ParseResult实例, 但可以定制:

importdsnparseclassMyResult(dsnparse.ParseResult):@classmethoddefparse(cls,dsn,**defaults):d=super(MyResult,cls).parse(dsn,**defaults)# d is a dict and you can customize its keys/values hered["interface"]=d.pop("scheme")returnddsn="Interface://testuser:testpw@localhost:1234/testdb"r=dsnparse.parse(dsn,parse_class=MyResult)print(isinstance(r,MyResult))# Trueprint(r.interface)# Interface

安装

使用pip:

pip install dsnparse

或者将pip与github一起使用:

pip install git+https://github.com/Jaymon/dsnparse#egg=dsnparse

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java无法从另一个类访问主类   java无效AES密钥长度:39字节   上传文档并显示文档状态Selenium WebDriver Java   在基于java的web应用程序中实现单点登录的身份验证先决条件   java减缓颜色序列代码   nosuchmethoderror Java`NoSuchMethodException`但方法在代码中不存在反射   java如何在JList中只允许唯一的字符串?   数据库获取:尝试将文件移动到文件夹时:“java.nio.file.FileSystemException”   java我无法从广播接收器获得startService   java在活动启动一段时间后更改文本   java无法编译,缺少主方法   运行Spring项目时出现java空指针异常   java@ManyToMany关系不保存   是否有一种java等价于C#语言。聚合(foo)方法?   visual studio代码Java错误,Java中的重复变量错误,无法从字符串转换为类