将整数、浮点数、None、True、False或JSON的字符串表示转换为它们的原生类型。对于None、True和False,大小写无关。
项目描述
将Python类型的字符串表示转换为原生类型。
为什么?
当构建命令行实用程序时,--arg key=val 语法很有用,尤其是当key 和 val 将作为内部使用的Class(key=val)。此模块专门用于与CLI框架click一起使用,所包含的扩展也表现良好,但也可以在其他地方使用str2type.str2type()函数。
示例
有关click集成示例,请参阅examples目录。其他内容主要由一个函数处理。
>>> from str2type import str2type
>>> print(str2type("1.23"))
1.23
>>> print(str2type("1.")
1.0
>>> print(str2type(".2"))
0.2
>>> print(str2type("None"))
None
>>> print(str2type('String'))
'String'
支持类型
仅支持标准内置Python类型以及JSON字符串
int
float
None
True
False
JSON
安装
通过pip
$ pip install str2type
从master分支
$ git clone https://github.com/geowurster/str2type
$ cd str2type
$ pip install .
开发
安装
$ pip install virtualenv
$ git clone https://github.com/geowurster/str2type
$ cd str2type
$ virtualenv venv
$ source venv/bin/activate
$ pip install -r requirements-dev.txt -e .
$ nosetests --with-coverage
$ pep8 --max-line-length=95 str2type