Skip to main content

PyToolBelt - 扩展您的内置方法。

项目描述

Py 工具带

该模块包含用于执行琐碎任务的内置数据类型的基本功能。此函数可作为内置数据类型方法使用。

安装

使用 pip 安装包 ->pip install py-toolbelt

用法

导入模块并激活工具带 import py_toolbelt py_toolbelt.activate() 激活后,自定义函数可作为内置方法使用。例如 - 在激活之前,在 python 控制台中,创建一个列表变量list_object = [1, 2, 3]并键入list_object.并按 Tab 以获取可用方法的提示,你会看到 -

>>> list_object.
l.append(   l.clear(    l.copy(     l.count(    l.extend(   l.index(    l.insert(   l.pop(      l.remove(   l.reverse(  l.sort(

激活后——

>>> list_object.
l.append(         l.count(          l.filter(         l.insert(         l.pop(            l.reverse(        l.to_tuple(
l.clear(          l.count_all(      l.flat_list(      l.intersection(   l.remove(         l.sort(           l.transform(
l.compact(        l.execute(        l.get_index(      l.is_none(        l.remove_index(   l.to_dict(        l.unique(
l.copy(           l.extend(         l.index(          l.is_not_none(    l.remove_values(  l.to_set(

停用工具带 py_toolbelt.deactivate()

可用功能

列表

独特的()

从列表中消除重复元素。
注意 -元素应该是可散列的数据类型,即元素必须是 str、int、float、tuple、bool。

例子

>>> list_obj = [1, 2, 3, 3, 2]
>>> print(list_obj.unique())
[1, 2, 3]

设置()

将列表转换为集合。
注意 -元素应该是可散列的数据类型,即元素必须是 str、int、float、tuple、bool。

例子

>>> list_obj = [1, 2, 3, 3, 2]
>>> print(list_obj.to_set())
{1, 2, 3}

to_tuple()

将列表转换为元组。
注意 -元素应该是可散列的数据类型,即元素必须是 str、int、float、tuple、bool。

例子

>>> list_obj = [1, 2, 3, 3, 2]
>>> print(list_obj.to_tuple())
(1, 2, 3, 3, 2)

to_dict(key_function=无,value_function=无)

将列表转换为 dict ,其中键使用key_function,值使用value_function争论

  • key_function - function,接受任意数量的参数。(必填
  • value_function - function,接受任意数量的参数。(必填

例子

>>> list_obj = [1, 2, 3, 4]
>>> list_obj.to_dict(key_function=lambda x: x, value_function=lambda y: y * 2)
{1: 1, 2: 4, 3: 6, 4: 8}

删除索引(index=[],**kwargs

根据参数中给定的索引值index和给定的condition关键字参数从列表中删除索引。 **注意 - **condition适用于索引,不适用于列表 参数的元素

  • index - list/ int,如果给定singleint则删除该索引,如果list给定则删除列表中给出的索引。(可选默认[]:)

关键字参数

  • 条件- function,如果条件函数满足索引号,则将其删除。
  • cast - data_type,转换列表的元素。

例子

>>> list_obj = ['hello', 'this', 'is', 'a', 'list']
>>> list_obj.remove_index(1)
['hello', 'is', 'a', 'list']

>>> list_obj.remove_index([0, 1, 2])
['a', 'list']

>>> list_obj.remove_index(condition=lambda x: x % 2 == 0) # even indexes will be removed
['is', 'list']

>>> list_obj = [1, 2, 3, 4]
>>> list_obj.remove_index([1, 2], condition=lambda x: x % 2 == 0, cast=str)
['4']

删除值(values=[],**kwargs

index根据参数中的给定值和给定的condition关键字参数从列表中删除元素。

论据

  • values - list/ 任何数据类型,如果给出任何其他数据类型(列表除外),则排除所有出现,如果list给出,则排除给定列表中存在的元素。(可选默认[]:)

关键字参数

  • 条件- function,如果条件函数满足列表元素,那么它将被删除。
  • cast - data_type,转换列表的元素。

例子

>>> list_obj = ['hello', 'this', 'is', 'a', 'list']
>>> list_obj.remove_values('this')
['hello', 'is', 'a', 'list']

>>> list_obj.remove_values(['hello', 'this', 'is'])
['a', 'list']

>>> list_obj.remove_values(condition=lambda x: x == 'hello')
['this', 'is', 'a', 'list']

>>> list_obj = [1, 2, 3, 4]
>>> list_obj.remove_values([1, 2], condition=lambda x: x % 2 == 0, cast=str)
['4']

过滤器( condition, **kwargs)

condition具有给定功能的过滤器列表。它相当于 -[element for element in list_object if condition(element)] 参数

  • 条件- function,如果条件函数不满足列表元素,则将其排除。(必填

关键字参数

  • cast - data_type,转换列表的元素。

例子

>>> list_obj = [1, 2, 3, 4, 5, 6]
>>> list_obj.filter(lambda x: x > 3)
[4, 5, 6]

>>> list_obj.filter(lambda x: x <= 3, cast=float)
[1.0. 2.0. 3.0]

袖珍的()

从列表中排除或清空None/// listdictsettuple

例子

>>> list_obj = [1, 2, 3, None, [], {}]
>>> list_obj.compact()
[1, 2, 3]

变换(element_function

转换列表的元素。

论据

  • element_function - function,列表元素被传递给给定的函数。(必填

例子

>>> list_obj = [1, 2, 3, 4]
>>> list_obj.transform(lambda x: x ** 2)
[1, 4, 9, 16]

>>> list_obj = [*enumerate(list_object)]
>>> list_obj.transform(lambda x, y: x + y)
[1, 3, 5, 7]

计数全部()

计算列表中元素的出现次数。
注意 -元素应该是可散列的数据类型,即元素必须是 str、int、float、tuple、bool。

例子

>>> list_obj = [1, 2, 2, 3, 3, 4]
>>> list_obj.count_all()
{1: 1, 2: 2, 3: 2, 4: 1}

交点( target_list)

给出常见元素的列表。
注意 -元素应该是可散列的数据类型,即元素必须是 str、int、float、tuple、bool。

例子

>>> list_obj = [1, 2, 2, 3, 3, 4]
>>> list_obj.intersection([2, 4, 5, 6, 7])
[2, 4]

展平()

将 N 维列表转换为单个平面一维列表。

例子

>>> list_obj = [1, [2, 2], 3, [[[3]]], 4]
>>> list_obj.flatten()
[1, 2, 2, 3, 3, 4]

获取索引( index, default_value=None)

从列表中获取给定索引,如果给定索引超出范围,则返回default_value

论据

  • 索引- int,(必填
  • default_value - 任何数据类型,(可选默认值:无

例子

>>> list_obj = [1, 2, 3, 4, 5]
>>> list_obj.get_index(2)
3

>>> list_obj.get_index(10, 'N/A')
N/A

执行(execute_function

使用列表元素执行函数。

论据

  • execute_function - function,它可以接受任意数量的参数(必需

例子

>>> list_obj = [1, 2, 3, 4, 5]
>>> new_list = []
>>> list_obj.execute(lambda x: new_list.append(x * 3))
>>> new_list
[3, 6, 9, 12, 15]

>>> list_obj.execute(lambda x: print(x))
1
2
3
4
5

字典

过滤器( condition, **kwargs)

根据键和值过滤字典。 论据

  • 条件- functionfunction必须接受 2 个参数,第一个参数为键,第二个参数为值。(必填

例子

>>> dict_obj = {'a': 1, 'b': 2, 'c': 3}
>>> dict_obj.filter(lambda key, value: value > 1)
{'b': 2, 'c': 3}

变换( key_function=None, value_function=None)

根据给定的功能转换字典键和值。 论据

  • key_function - functionfunction可以接受任意数量的参数。(可选默认None:)
  • value_function - functionfunction可以接受任意数量的参数。(可选默认None:)

例子

>>> dict_obj = {'a': 1, 'b': 2, 'c': 3}
>>> dict_obj.transform(key_function=lambda x: 'a' + x)
{'aa': 1, 'ab': 2, 'ac': 3}

>>> dict_obj.transform(value_function=lambda x: x ** 2)
{'a': 1, 'b': 4, 'c': 9}

>>> dict_obj = {'a': (1, 2), 'b': (3, 4), 'c': (5, 6)}
>>> dict_obj.transform(key_function=lambda key: key + '_sum', value_function=lambda x, y: x + y)
{'a_sum': 3, 'b_sum': 7, 'c_sum': 11}

键列表()

将 dict 的键作为列表返回。

例子

>>> dict_obj = {'a': 1, 'b': 2, 'c': 3}
>>> dict_obj.keys_list()
['a', 'b', 'c']

值列表()

dict 的返回值作为列表。

例子

>>> dict_obj = {'a': 1, 'b': 2, 'c': 3}
>>> dict_obj.values_list()
[1, 2, 3]

删除(key_function=None,,,value_function=Noneoperator='and'

根据给定的key_functionandvalue_fucntion和之间的操作删除字典键key_funcitonvalue_function默认and操作。 论据

  • key_function - functionfunction可以接受任意数量的参数。(可选默认None:)
  • value_function - functionfunction可以接受任意数量的参数。(可选默认None:)
  • 运算符- str, 接受的字符串 - ['and', 'or'], ( optional , default='and' )

例子

>>> dict_obj = {'a': 1, 'b': 2, 'c': 3}
>>> dict_obj.remove(key_function=lambda x: x == 'b')
{'a': 1, 'c': 3}

>>> dict_object.remove(key_function=lambda key: key in ['b', 'c'], value_fucntion=lambda value: value % 2 == 1)
{'a': 1, 'b': 2}

>>> dict_object.remove(key_function=lambda key: key in ['b', 'c'], value_fucntion=lambda value: value % 2 == 1, operator='or')
{'a': 1}

反向(keep_duplicate=False

反转dict,将值设置为键,将键设置为值。如果keep_duplicate设置为 true,则它将重复值合并为列表。
注意 - dict 值应该是可散列的数据类型,即元素必须是 str、int、float、tuple、bool。

论据

  • keep_duplicate - bool, (可选,默认:False )

例子

>>> dict_obj = {'a': 1, 'b': 2, 'c': 3, 'd': 3}
>>> dict_obj.reverse()
{1: 'a', 2: 'b', 3: 'd'}

>>> dict_obj.reverse(keep_duplicate=True)
{1: 'a', 2: 'b', 3: ['c', 'd']}

deep_merge( target_dict, max_depth=None)

它将target_dict与 given合并max_depth。如果max_depth是,None那么它会下降到最大深度以进行合并。 论据

  • target_dict - dict, (必需)
  • max_depth - intint大于0,(可选默认None:)

例子

>>> dict_obj = {'a': 1, 'b': {'c': {'d': 3}, 'e': 2}}
>>> dict_obj.deep_merge({'g': 4, 'b': {'c': {'d': 5}}})
{'a': 1, 'b': {'c': {'d': [3, 5]}, 'e': 2}, 'g': 4}

>>> dict_obj.deep_merge({'g': 4, 'b': {'c': {'d': 5}}}, max_depth=1)
{'a': 1, 'b': [{'c': {'d': 3}, 'e': 2}, {'c': {'d': 5}}], 'g': 4}

执行(execute_function

使用 dict 的键和值执行函数。

论据

  • execute_function - function,它必须接受 2 个参数 key 和 value,(必需

例子

>>> dict_obj = {'a': 1, 'b': 2, 'c': 3}
>>> new_list = []
>>> dict_obj.execute(lambda x, y: new_list.append(y))
>>> new_list
[1, 2, 3]

>>> dict_obj.execute(lambda x, y: print(x))
1
2
3

to_list()

将集合转换为列表。

元组

to_list()

将元组转换为列表。

整数和浮点数

is_positive( **kwargs)

True如果数字为正则返回,否则返回False。如果存在on_trueon_false关键字参数,那么它将分别返回关键字参数值。

关键字参数

  • on_true - 任何数据类型,(可选默认值True:)
  • on_false - 任何数据类型,(可选默认值False:)

例子

>>> num = 1
>>> num.is_positive()
True
>>> num.is_positive(on_true='Yes')
Yes
>>> num = -9
>>> num.is_positive(on_false='No')
No
>>> num.is_positive(on_true='Yes')
False

is_negetive( **kwargs)

如果数字为负则返回True,否则返回False。如果存在on_trueon_false关键字参数,那么它将分别返回关键字参数值。

关键字参数

  • on_true - 任何数据类型,(可选默认值True:)
  • on_false - 任何数据类型,(可选默认值False:)

例子

>>> num = 1
>>> num.is_negetive()
False
>>> num.is_negetive(on_true='Yes')
False
>>> num = -9
>>> num.is_negetive(on_false='No')
True
>>> num.is_negetive(on_true='Yes', on_false='No')
Yes

is_zero( **kwargs)

True如果数字等于零则返回,否则返回False。如果存在on_trueon_false关键字参数,那么它将分别返回关键字参数值。

关键字参数

  • on_true - 任何数据类型,(可选默认值True:)
  • on_false - 任何数据类型,(可选默认值False:)

例子

>>> num = 1
>>> num.is_zero()
False
>>> num.is_zero(on_true='Yes')
False
>>> num = 0
>>> num.is_zero(on_false='No')
True
>>> num.is_zero(on_true='Yes', on_false='No')
Yes

safe_divide( denominator, default=math.nan)

它确保如果denominator为零则返回default值,否则返回除值。 论据

  • 分母- int/ float, (必填)
  • 默认值- 任何数据类型,(可选默认值math.nan:)

例子

>>> a = 4
>>> a.safe_divide(2)
2.0
>>> a.safe_divide(0)
nan
>>> a.safe_divide(0, 0)
0
>>> a.safe_divde(0, 'N/A')
N/A

其他

这对所有数据类型都是通用的 -

is_none( **kwargs)

True如果 object 是Noneelse return则返回False。如果存在on_trueon_false关键字参数,那么它将分别返回关键字参数值。

关键字参数

  • on_true - 任何数据类型,(可选默认值True:)
  • on_false - 任何数据类型,(可选默认值False:)

例子

>>> a, b, c, d = 2, 'hello', [], None
>>> a.is_none()
False
>>> a.is_none(on_false=a + 1)
3
>>> b.is_none(on_true="Yes")
False
>>> d.is_none()
True

is_not_none( **kwargs)

True如果 object 不是Noneelse return则返回False。如果存在on_trueon_false关键字参数,那么它将分别返回关键字参数值。

关键字参数

  • on_true - 任何数据类型,(可选默认值True:)
  • on_false - 任何数据类型,(可选默认值False:)

例子

>>>  a ,  b ,  c ,  d  =  2 ,  'hello' ,  [], 
>>>  a is_not_none ()