Python功能助手
项目描述
# UNDERPY
[](https://travis-ci.org/ramonski/underpy)
```
_
_ _ _ __ __| | ___ _ __ _ __ _ _
| | | | '_ \ / _` |/ _ \ '__| '_ \| | | |
| |_| | | | | (_| | __/ | | |_) | |_| |
\__,_|_| |_|\__,_|\___|_| | .__/ \__, |
|_| |___/
Python功能助手
```
## 函数
``` python
alias(col, mapping)
返回一个根据映射重命名键的字典集合
的集合
>>> libraries = [{"isbn": 1, "ed": 1}, {"isbn": 2, "ed": 2}]
>>> alias(libraries, {"ed": "edition"})
[{'edition': 1, 'isbn': 1}, {'edition': 2, 'isbn': 2}]
>>> alias({"a": 1}, {"a": "b"})
[{'b': 1}]
convert(value, converter)
使用给定的转换函数转换值。
>>> convert("1", to_int)
1
>>> convert("0", to_int)
0
>>> convert("a", to_int)
fail(error)
使用给定的错误消息引发RuntimeError
>>> fail("This failed badly")
Traceback (most recent call last)
...
RuntimeError: This failed badly
falsy(thing)
检查一个值是否为False或None
>>> falsy(0)
False
>>> falsy({})
False
>>> falsy([])
False
>>> falsy(None)
True
>>> falsy(False)
True
first(lst, n=None)
获取列表的第一个元素
>>> lst = [1, 2, 3, 4, 5]
>>> first(lst)
1
>>> first(lst, 3)
[1, 2, 3]
is_dict(thing)
检查一个对象是否是字典类型
>>> is_dict({})
True
>>> is_dict(dict())
True
>>> is_dict("{}")
False
>>> is_dict([])
False
is_digit(thing)
检查一个对象是否是数字
>>> is_digit(1)
True
>>> is_digit("1")
True
>>> is_digit("a")
False
>>> is_digit([])
False
is_list(thing)
检查一个对象是否是列表类型
>>> is_list([])
True
>>> is_list(list())
True
>>> is_list("[]")
False
>>> is_list({})
False
is_string(thing)
检查一个对象是否是字符串/unicode类型
>>> is_string("")
True
>>> is_string(u"")
True
>>> is_string(str())
True
>>> is_string(unicode())
True
>>> is_string(1)
False
is_tuple(thing)
检查一个对象是否为元组类型
>>> is_tuple(())
True
>>> is_tuple(tuple())
True
>>> is_tuple("()")
False
>>> is_tuple([])
False
omit(dct, *keys)
返回一个过滤掉黑名单键的字典的副本
(或键的列表)
>>> omit({"name": "moe", "age": 50, "userid": "moe1"}, "userid", "age")
{'name': 'moe'}
pick(dct, *keys)
返回一个只包含白名单键(或有效键的列表)值的字典的副本
whitelisted keys (or list of valid keys)
>>> pick({"name": "moe", "age": 50, "userid": "moe1"}, "name", "age")
{'age': 50, 'name': 'moe'}
pluck(col, key, default=None)
从字典集合中提取值列表
>>> stooges = [{"name": "moe", "age": 40},
... {"name": "larry", "age": 50},
... {"name": "curly", "age": 60}]
>>> pluck(stooges, "name")
['moe', 'larry', 'curly']
它只适用于集合
>>> curly = stooges.pop()
>>> pluck(curly, "age")
Traceback (most recent call last)
...
RuntimeError: 第一个参数必须是列表或元组
rename(dct, mapping)
使用给定的映射重命名字典的键
>>> rename({"a": 1, "BBB": 2}, {"a": "AAA"})
{'AAA': 1, 'BBB': 2}
to_int(thing)
将对象转换为 int
>>> to_int("0")
0
>>> to_int(1)
1
>>> to_int("1")
1
>>> to_int("a")
to_list(thing)
将对象转换为列表
>>> to_list(1)
[1]
>>> to_list([1,2,3])
[1, 2, 3]
>>> to_list(("a", "b", "c"))
['a', 'b', 'c']
>>> to_list(dict(a=1, b=2))
[{'a': 1, 'b': 2}]
to_string(thing)
将对象转换为字符串
>>> to_string(1)
'1'
>>> to_string([])
'[]'
>>> to_string(u"a")
'a'
to_iso_date(thing)
将对象转换为 iso 日期字符串
>>> to_iso_date("")
''
>>> dt = datetime.date.fromtimestamp(1387452665)
>>> to_iso_date(dt)
'2013-12-19'
truthy(thing)
检查一个值是否为 True 或非 None
>>> truthy(0)
True
>>> truthy({})
True
>>> truthy([])
True
>>> truthy(None)
False
>>> truthy(False)
False
```
## 运行测试
```
python setup.py nosetests --with-doctest
```
## 许可证
MIT
[](https://travis-ci.org/ramonski/underpy)
```
_
_ _ _ __ __| | ___ _ __ _ __ _ _
| | | | '_ \ / _` |/ _ \ '__| '_ \| | | |
| |_| | | | | (_| | __/ | | |_) | |_| |
\__,_|_| |_|\__,_|\___|_| | .__/ \__, |
|_| |___/
Python功能助手
```
## 函数
``` python
alias(col, mapping)
返回一个根据映射重命名键的字典集合
的集合
>>> libraries = [{"isbn": 1, "ed": 1}, {"isbn": 2, "ed": 2}]
>>> alias(libraries, {"ed": "edition"})
[{'edition': 1, 'isbn': 1}, {'edition': 2, 'isbn': 2}]
>>> alias({"a": 1}, {"a": "b"})
[{'b': 1}]
convert(value, converter)
使用给定的转换函数转换值。
>>> convert("1", to_int)
1
>>> convert("0", to_int)
0
>>> convert("a", to_int)
fail(error)
使用给定的错误消息引发RuntimeError
>>> fail("This failed badly")
Traceback (most recent call last)
...
RuntimeError: This failed badly
falsy(thing)
检查一个值是否为False或None
>>> falsy(0)
False
>>> falsy({})
False
>>> falsy([])
False
>>> falsy(None)
True
>>> falsy(False)
True
first(lst, n=None)
获取列表的第一个元素
>>> lst = [1, 2, 3, 4, 5]
>>> first(lst)
1
>>> first(lst, 3)
[1, 2, 3]
is_dict(thing)
检查一个对象是否是字典类型
>>> is_dict({})
True
>>> is_dict(dict())
True
>>> is_dict("{}")
False
>>> is_dict([])
False
is_digit(thing)
检查一个对象是否是数字
>>> is_digit(1)
True
>>> is_digit("1")
True
>>> is_digit("a")
False
>>> is_digit([])
False
is_list(thing)
检查一个对象是否是列表类型
>>> is_list([])
True
>>> is_list(list())
True
>>> is_list("[]")
False
>>> is_list({})
False
is_string(thing)
检查一个对象是否是字符串/unicode类型
>>> is_string("")
True
>>> is_string(u"")
True
>>> is_string(str())
True
>>> is_string(unicode())
True
>>> is_string(1)
False
is_tuple(thing)
检查一个对象是否为元组类型
>>> is_tuple(())
True
>>> is_tuple(tuple())
True
>>> is_tuple("()")
False
>>> is_tuple([])
False
omit(dct, *keys)
返回一个过滤掉黑名单键的字典的副本
(或键的列表)
>>> omit({"name": "moe", "age": 50, "userid": "moe1"}, "userid", "age")
{'name': 'moe'}
pick(dct, *keys)
返回一个只包含白名单键(或有效键的列表)值的字典的副本
whitelisted keys (or list of valid keys)
>>> pick({"name": "moe", "age": 50, "userid": "moe1"}, "name", "age")
{'age': 50, 'name': 'moe'}
pluck(col, key, default=None)
从字典集合中提取值列表
>>> stooges = [{"name": "moe", "age": 40},
... {"name": "larry", "age": 50},
... {"name": "curly", "age": 60}]
>>> pluck(stooges, "name")
['moe', 'larry', 'curly']
它只适用于集合
>>> curly = stooges.pop()
>>> pluck(curly, "age")
Traceback (most recent call last)
...
RuntimeError: 第一个参数必须是列表或元组
rename(dct, mapping)
使用给定的映射重命名字典的键
>>> rename({"a": 1, "BBB": 2}, {"a": "AAA"})
{'AAA': 1, 'BBB': 2}
to_int(thing)
将对象转换为 int
>>> to_int("0")
0
>>> to_int(1)
1
>>> to_int("1")
1
>>> to_int("a")
to_list(thing)
将对象转换为列表
>>> to_list(1)
[1]
>>> to_list([1,2,3])
[1, 2, 3]
>>> to_list(("a", "b", "c"))
['a', 'b', 'c']
>>> to_list(dict(a=1, b=2))
[{'a': 1, 'b': 2}]
to_string(thing)
将对象转换为字符串
>>> to_string(1)
'1'
>>> to_string([])
'[]'
>>> to_string(u"a")
'a'
to_iso_date(thing)
将对象转换为 iso 日期字符串
>>> to_iso_date("")
''
>>> dt = datetime.date.fromtimestamp(1387452665)
>>> to_iso_date(dt)
'2013-12-19'
truthy(thing)
检查一个值是否为 True 或非 None
>>> truthy(0)
True
>>> truthy({})
True
>>> truthy([])
True
>>> truthy(None)
False
>>> truthy(False)
False
```
## 运行测试
```
python setup.py nosetests --with-doctest
```
## 许可证
MIT