Python自带的几个高阶函数(map、reduce、filter)再搭配上lambda函数,堪称上古神器!
但是,在数据处理频繁的开发过程中,不论是web开发还是写AC用例,上古神器却还是有点捉襟见肘,很多交互数据的处理都不得不带上神器再另外去写一堆逻辑。
于是,针对频繁的交互数据处理的问题,一个强大、简单、实用的Python交互数据处理函数库 funclib.py 就应运而生了!
funclib.py兼容python2和python3,有充分的测试用例保证以及丰富齐全的帮助文档,绝对是你Python开发的好工具!
如果你喜欢funclib.py,就到GitHub上去Star或Fork吧: http://github.com/CN-Tower/funclib.py 欢迎一起来玩!
funclib-2.1.6.tar.gz | Release Version (2.1.6) |
$ pip install funclib # pip install funclib-2.1.6.tar.gz (Local install) $ python >>> from funclib import fn >>> help(fn) >>> help(fn.index)
fn.index
Looks through the list and returns the item index. If no match is found or if list is empty, -1 will be returned.
def index(predicate, src_list):
@param predicate [dict|function]
查找条件,可以是一个字典或返回bool值的函数
@param src_list [list|tuple|map]
传入的原列表
persons = [{"name": "Tom", "age": 12}, {"name": "Jerry", "age": 20}, {"name": "Mary", "age": 35}] fn.index({"name": 'Jerry'}, persons) # => 1 fn.index(lambda x: x['name'] == 'Mary', persons) # => 2
fn.find
Looks through each value in the list, returning the first one that passes a truth test (predicate), or None.If
no value passes the test the function returns as soon as it finds an acceptable element, and doesn't traverse the
entire list.
def find(predicate, src_list):
@param predicate [dict|function]
查找条件,可以是一个字典或返回bool值的函数
@param src_list [list|tuple|map]
传入的原列表
persons = [{"name": "Tom", "age": 12}, {"name": "Jerry", "age": 20}, {"name": "Mary", "age": 35}] Jerry = fn.find({"name": 'Jerry'}, persons) Mary = fn.find(lambda x: x['name'] == 'Mary', persons) print(Jerry) # => {'age': 20, 'name': 'Jerry'} print(Mary) # => {'age': 35, 'name': 'Mary'}
fn.filter
Looks through each value in the list, returning an array of all the values that pass a truth test (predicate).
def filter(predicate, src_list):
@param predicate [dict|function]
过滤条件,可以是一个字典或返回bool值的函数
@param src_list [list|tuple|map]
传入的原列表
persons = [{"name": "Tom", "age": 20}, {"name": "Jerry", "age": 20}, {"name": "Jerry", "age": 35}] Jerry = fn.filter({"age": 20}, persons) Mary = fn.filter(lambda x: x['name'] == 'Jerry', persons) print(Jerry) # => [{'age': 20, 'name': 'Tom'}, {'age': 20, 'name': 'Jerry'}] print(Mary) # => [{'age': 20, 'name': 'Jerry'}, {'age': 35, 'name': 'Jerry'}]
fn.reject
Returns the values in list without the elements that the truth test (predicate) passes.
def reject(predicate, src_list):
@param predicate [dict|function]
拒绝条件,可以是一个字典或返回bool值的函数
@param src_list [list|tuple|map]
传入的原列表
persons = [{"name": "Tom", "age": 12}, {"name": "Jerry", "age": 20}, {"name": "Mary", "age": 35}] not_Mary = fn.reject({"name": "Mary"}, persons) adults = fn.reject(lambda x: x['age'] < 18, persons) print(not_Mary) # => [{"age": 12, "name": "Tom"}, {"age": 20, "name": "Jerry"}] print(adults) # => [{"age": 20, "name": "Jerry"}, {"age": 35, "name": "Mary"}]
fn.reduce
Returns the buildIn method 'reduce', in python 3 the 'reduce' is imported from functools.
def reduce(*args):
参考python自带的reduce方法,这里做了python3的引入适配。
num_list = [1 , 2, 3, 4] fn.reduce(lambda a, b: a + b, num_list) # => 10
fn.contains
Returns true if the value is present in the list.
def contains(predicate, src_list):
@param predicate [dict|function]
查询条件,可以是一个字典或返回bool值的函数
@param src_list [list|tuple|map]
传入的原列表
persons = [{"name": "Tom", "age": 12}, {"name": "Jerry", "age": 20}, {"name": "Mary", "age": 35}] fn.contains({"name": "Jerry", "age": 12}, persons) # => False fn.contains(lambda x: x['name'] == 'Mary', persons) # => True
fn.flatten
Flattens a nested array (the nesting can be to any depth). If you pass shallow, the array will only be flattened
a single level.
def flatten(src_list, is_deep=False):
@param src_list [list|tuple|map]
传入的原列表
@param is_deep=False [bool] (可选)
是否深度打扁
fn.flatten([1, [2], [3, [[4]]]]) # => [1, 2, 3, [[4]]] fn.flatten([1, [2], [3, [[4]]]], True) # => [1, 2, 3, 4]
fn.uniq
Produces a duplicate-free version of the array. In particular only the first occurence of each value is kept.
def uniq(src_list, path='/'):
@param src_list [list|tuple|map]
传入的原列表
@param path [str]
去重字段路径
demo_list = [False, [], False, True, [], {}, False, ''] persons = [{"name": "Tom", "age": 12, "pet": {"species": "dog", "name": "Kitty"}}, {"name": "Tom", "age": 20, "pet": {"species": "cat", "name": "wang"}}, {"name": "Mary", "age": 35, "pet": {"species": "cat", "name": "mimi"}}] fn.uniq(("Tom", "Tom", "Jerry")) # => ["Jerry", "Tom"] fn.uniq(["Tom", "Tom", "Jerry"]) # => ["Jerry", "Tom"] fn.uniq(demo_list) # => [False, [], True, {}, ''] fn.uniq(persons, 'name') # => [] fn.uniq(persons, 'pet', 'species') # => []
fn.pluck
Pluck the collections element.
def pluck(body, *keys, **conf):
@param body [list|dict]
传入的原列表
@param keys [str]
合并的位置
@param conf
is_uniq=False
persons = [{"name": "Tom", "hobbies": ["sing", "running"]}, {"name": "Jerry", "hobbies": []}, {"name": "Mary", "hobbies": ['hiking', 'sing']}] hobbies = fn.pluck(persons, 'hobbies') uniq_hobbies = fn.pluck(persons, 'hobbies', uniq=True) print(hobbies) # => ["sing", "running", 'hiking', 'sing'] print(hobbies_uniq) # => ["sing", "running", 'hiking']
fn.every
Returns true if all of the values in the list pass the predicate truth test. Short-circuits and stops traversing
the list if a false element is found.
def every(predicate, src_list):
@param predicate [dict|function]
判断条件,可以是一个字典或返回bool值的函数
@param src_list [list|tuple|map]
传入的原列表
persons = [{"name": "Tom", "age": 12, "sex": "m"}, {"name": "Jerry", "age": 20, "sex": "m"}, {"name": "Mary", "age": 35, "sex": "f"}] fn.every(5, [1, 1, 2, 3, 5, 8]) # => False fn.every({"sex": "m"}, persons) # => False fn.every(lambda x: x['age'] > 18, persons) # => False
fn.some
Returns true if any of the values in the list pass the predicate truth test. Short-circuits and stops traversing
the list if a true element is found.
def some(predicate, src_list):
@param predicate [dict|function]
判断条件,可以是一个字典或返回bool值的函数
@param src_list [list|tuple|map]
传入的原列表
persons = [{"name": "Tom", "age": 12, "sex": "m"}, {"name": "Jerry", "age": 20, "sex": "m"}, {"name": "Mary", "age": 35, "sex": "f"}] fn.some(5, [1, 1, 2, 3, 5, 8]) # => True fn.some({"sex": "m"}, persons) # => True fn.some(lambda x: x['age'] > 18, persons) # => True
fn.tolist
Return a listlized value.
def tolist(*values):
@param values [any]
任意类型的值
fn.tolist() # => [] fn.tolist([]) # => [] fn.tolist({}) # => [{}] fn.tolist(None) # => [None] fn.tolist('str') # => ['str']
fn.drop
Delete false values expect 0.
def drop(src_list, is_without_0=False):
@param src_list [list|tuple|map]
传入的原列表
@param is_without_0=False [bool]
是否去掉0,默认不去掉
tmp_list = [0, '', 3, None, [], {}, ['Yes'], 'Test'] fn.drop(tmp_list) # => [3, ['Yes'], 'Test'] fn.drop(tmp_list, True) # => [0, 3, ['Yes'], 'Test']
fn.typeof
Verify is value in given types. types should be one or some key or value in the types_map below.
def typeof(value, *types):
@param value [list|tuple|map]
待测试的值
@param types [str|type]
'int' or
int,
'flt' or
float,
'str' or
str,
'bol' or
bool,
'dic' or
dict,
'lst' or
list,
'tup' or
tuple,
'uni' or
'unicode',
'non' or
'NoneType',
'fun' or
'function',
'kis' or
'dict_keys',
fn.typeof(None, 'non') # => True fn.typeof(True, 'str') # => False fn.typeof([], 'map', 'lst', 'tup') # => True fn.typeof(lambda x: x, 'fun') # => True
fn.typeval
Verify is value in given types. retury The value self or False by typeof check.
def typeval(value, *types):
@param value [list|tuple|map]
待测试的值
@param types [str|type]
The same to fn.typeof's types
fn.typeval('test', 'str') # => 'test' fn.typeval([], 'lst') # => [] fn.typeval({}, 'lst') # => False
fn.get
Get values form dict or list.
def get(origin, path, *types):
@param origin [list|dict]
传入的原值
@param path [str]
值所在的字段路径
@param types [str|type]
指定值的类型
fn.typeval('test', 'str') # => 'test' fn.typeval([], 'lst') # => [] fn.typeval({}, 'lst') # => False
fn.dump
Return a formatted json string.
def dump(_json, **conf):
@param _json [list|dict]
传入的原值
@param conf
sort_keys=True, indent=2
persons = [{"name": "Tom", "hobbies": ["sing", "running"]}, {"name": "Jerry", "hobbies": []}] print(fn.dump(persons)) #=> [ { "hobbies": [ "sing", "running" ], "name": "Tom" }, { "hobbies": [], "name": "Jerry" } ]
fn.test
Varify is the match successful, a boolean value will be returned.
def test(pattern, origin):
@param pattern [re]
传入正则
@param origin [str]
待判定的原字符串
fn.test(r'ab', 'Hello World!') # => False fn.test(r'll', 'Hello World!') # => True
fn.replace
Replace sub string of the origin string with re.sub()
def replace(*args):
用法与python的re相同
print(fn.replace(r'Tom', 'Jack', 'Hello I\'m Tom!')) # => Hello I'm Jack!
fn.iscan
Test is the expression valid, a boolean value will be returned.
def iscan(exp):
@param exp [str]
字符串表达式
print(fn.iscan("int('a')")) # => False print(fn.iscan("int('5')")) # => True
fn.log
Show log clear in console.
def log(*values, **conf):
@param values [any]
需要打印的值
@param conf
width=68, title='funclib 2.1.6', pre=False, end=False,
persons = [{"name": "Tom", "hobbies": ["sing", "running"]}] fn.log(persons) # => ========================================================== [12:22:35] funclib 2.1.6 ---------------------------------------------------------- [ { "hobbies": [ "sing", "running" ], "name": "Tom" } ] ==========================================================
fn.timer
Set a timer with interval and timeout limit.
def timer(func, times=60, interval=1):
@param func [function]
定时触发的函数
count = 0 def fn(): global count if count == 4: return True count += 1 print(count) fn.timer(fn, 10, 2) # => >>> 1 #at 0s >>> 2 #at 2s >>> 3 #at 4s >>> 4 #at 4s
fn.now
Return system now time.
def now():
fn.now() #=> 2018-07-26 14:34:26
fn.clear
Clear the terminam screen.
def clear():
$ python >>> from funclib import fn >>> fn.clear()