将int、float、none、true、false或json的字符串表示形式转换为其本机类型。不管是真是假,案子都无关紧要。
str2type的Python项目详细描述
将python类型的字符串表示形式转换为其本机类型。
为什么?
在构建命令行实用程序时,语法--arg key=val很有用, 尤其是当key和val将在内部用作Class(key=val)时。 这个模块是专门为与cli框架click一起使用而构建的,并且 包含的扩展很好,但是str2type.str2type()函数可以 在别处使用。
示例
有关click integration examples,请参阅examples目录。其他的事情都是主要处理的 通过单个函数:
>>>fromstr2typeimportstr2type>>>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
来自主分支:
$ 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