kevin-toolbox-dev 1.0.0__py3-none-any.whl → 1.0.1.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
kevin_toolbox/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
- __version__ = "1.0.0"
1
+ __version__ = "1.0.1.5"
2
2
 
3
3
 
4
4
  import os
@@ -12,5 +12,5 @@ os.system(
12
12
  os.system(
13
13
  f'python {os.path.split(__file__)[0]}/env_info/check_validity_and_uninstall.py '
14
14
  f'--package_name kevin-toolbox-dev '
15
- f'--expiration_timestamp 1688042370 --verbose True'
15
+ f'--expiration_timestamp 1696682104 --verbose True'
16
16
  )
@@ -2,7 +2,7 @@ import pytest
2
2
  import numpy as np
3
3
  from kevin_toolbox.patches.for_test import check_consistency
4
4
  from kevin_toolbox.computer_science.algorithm.combinatorial_optimization import get_subset_with_largest_product
5
- from kevin_toolbox.computer_science.algorithm.utils import get_subsets
5
+ from kevin_toolbox.computer_science.algorithm.utils.for_seq import get_subsets
6
6
 
7
7
 
8
8
  def test_get_subset_with_largest_product():
@@ -2,7 +2,7 @@ import pytest
2
2
  import numpy as np
3
3
  from kevin_toolbox.patches.for_test import check_consistency
4
4
  from kevin_toolbox.computer_science.algorithm.combinatorial_optimization import zero_one_knapsack_problem
5
- from kevin_toolbox.computer_science.algorithm.utils import get_subsets
5
+ from kevin_toolbox.computer_science.algorithm.utils.for_seq import get_subsets
6
6
 
7
7
 
8
8
  def test_zero_one_knapsack_problem():
@@ -0,0 +1 @@
1
+ from .get_pareto_points_idx import get_pareto_points_idx
@@ -0,0 +1,65 @@
1
+ import numpy as np
2
+
3
+
4
+ def get_pareto_points_idx(points, directions=None):
5
+ """
6
+ 获取位于帕累托边缘上的点的序号
7
+ 这些边缘点具有的性质:与除它之外的任意一个点相比,其总有在某一个或者多个维度上具有“优势”
8
+
9
+ 参数:
10
+ points: <list/tuple/np.array> 点集
11
+ shape: [nums, dims]
12
+ directions: <list/tuple of str> 各个维度的优化方向
13
+ shape: [dims]
14
+ 目前支持以下三种优化方向:
15
+ "maximize": 越大越好
16
+ "minimize": 越小越好
17
+ "not_care": 不考虑该维度
18
+ 默认为 None, 等效于 ["maximize"]*len(points),亦即全部维度数值越大越好
19
+ 返回:
20
+ idx_ls: <list of index> 位于边缘的点的序号列表
21
+ """
22
+ points = np.asarray(points)
23
+ assert points.ndim == 2 and len(points) > 0
24
+ assert directions is None or isinstance(directions, (list, tuple,)) and len(directions) == points.shape[-1]
25
+
26
+ # 计算排序的权重
27
+ if directions is not None:
28
+ weights = []
29
+ for i, direction in enumerate(directions):
30
+ if direction == "maximize":
31
+ weights.append(-points[:, i:i + 1])
32
+ elif direction == "minimize":
33
+ weights.append(points[:, i:i + 1])
34
+ elif direction == "not_care":
35
+ pass
36
+ if len(weights) == 0: # 全部都是 not_care,那就别找了
37
+ return []
38
+ weights = np.concatenate(weights, axis=1)
39
+ else:
40
+ weights = -points
41
+
42
+ # 按权重进行排序
43
+ order_names = tuple(f'{i}' for i in range(weights.shape[-1]))
44
+ idx_ls = np.argsort(
45
+ np.asarray([tuple(i) for i in weights.tolist()], dtype=[(n, weights.dtype) for n in order_names]),
46
+ order=order_names
47
+ )
48
+
49
+ # 收集位于 pareto 边缘的点
50
+ # 按上面得到的顺序从前往后过一遍 points
51
+ # - 累积所有维度上出现过的最大值
52
+ # - 若当前 point 在所有维度上比累积值要小,则抛弃该 point
53
+ res = [idx_ls[0]]
54
+ best = weights[idx_ls[0]][:]
55
+ for idx in idx_ls[1:]:
56
+ if np.any(best - weights[idx] > 0):
57
+ best = np.min([best, weights[idx]], axis=0)
58
+ res.append(idx)
59
+
60
+ return res
61
+
62
+
63
+ if __name__ == '__main__':
64
+ res = get_pareto_points_idx(points=[[1.5, 2], [2, 1], [1, 3], [3, 1]], directions=["maximize", "not_care"])
65
+ print(res)
@@ -1 +1 @@
1
- from .get_subsets import get_subsets
1
+ from .get_hash import get_hash
@@ -0,0 +1 @@
1
+ from .deep_update import deep_update
@@ -0,0 +1,25 @@
1
+ def deep_update(stem, patch):
2
+ """
3
+ 使用 patch 递归地更新 stem 中对应结构下的值
4
+ 比如对于 stem = {"a": {"b": [233], "g": 3}}
5
+ 如果使用 patch = {"a": {"b": 444}} 进行更新,可以得到:
6
+ {'a': {'b': 444, 'g': 3}}
7
+ """
8
+ assert isinstance(stem, (dict,)) and isinstance(patch, (dict,))
9
+
10
+ return _recursion(stem=stem, patch=patch)
11
+
12
+
13
+ def _recursion(stem, patch):
14
+ for k, v in patch.items():
15
+ if k in stem and isinstance(v, (dict,)):
16
+ stem[k] = _recursion(stem=stem[k], patch=v)
17
+ else:
18
+ stem[k] = v
19
+ return stem
20
+
21
+
22
+ if __name__ == '__main__':
23
+ stem = {"a": {"b": [233], "g": 3}}
24
+ patch = {"a": {"b": 444}}
25
+ print(deep_update(stem, patch))
@@ -0,0 +1,2 @@
1
+ from .get_subsets import get_subsets
2
+ from .flatten_list import flatten_list
@@ -0,0 +1,33 @@
1
+ def flatten_list(ls, depth=None):
2
+ """
3
+ 将列表展平
4
+ 比如对于输入 ls=[[1, 2, (3, 4)], [(5,)], 6], depth=None
5
+ 展平后得到的输出为 [1, 2, 3, 4, 5, 6]
6
+
7
+ 参数:
8
+ ls: <list/tuple> 要展平的列表
9
+ depth: <int> 展开到第几层
10
+ 0 表示当前层,亦即不作展开
11
+ 默认为 None 表示展开到最深的那层
12
+ """
13
+ assert isinstance(ls, (list, tuple,))
14
+ assert depth is None or isinstance(depth, (int,))
15
+ return _recursion(ls=ls, depth=depth)
16
+
17
+
18
+ def _recursion(ls, depth):
19
+ if depth is not None and depth <= 0:
20
+ res = ls
21
+ else:
22
+ res = []
23
+ for i in ls:
24
+ if isinstance(i, (list, tuple,)):
25
+ res.extend(_recursion(i, depth=depth - 1))
26
+ else:
27
+ res.append(i)
28
+ return res
29
+
30
+
31
+ if __name__ == '__main__':
32
+ a = [[1, 2, (3, 4)], [(5,)], 6]
33
+ print(flatten_list(ls=a, depth=5))
@@ -0,0 +1,31 @@
1
+ import hashlib
2
+ import json
3
+ import warnings
4
+
5
+
6
+ def get_hash(item, length=8, mode="md5"):
7
+ """
8
+ 将输入 item 序列化,再计算出 hash 值
9
+ 本函数对于无序的字典能够提供唯一的 hash 值
10
+
11
+ 参数:
12
+ item:
13
+ length: <int> 返回 hash 字符串的前多少位
14
+ mode: <str> 支持 hashlib 中提供的 hash 方法
15
+ """
16
+ worker = eval(f'hashlib.{mode}()')
17
+ assert mode in hashlib.__all__
18
+ try:
19
+ item = json.dumps(item, sort_keys=True).encode()
20
+ except:
21
+ warnings.warn(
22
+ f"the item {type(item)} unable to be serialized by json, reproducibility is no longer guaranteed!",
23
+ UserWarning
24
+ )
25
+ worker.update(f"{item}".encode('utf-8'))
26
+ hash_ = worker.hexdigest()[:length]
27
+ return hash_
28
+
29
+
30
+ if __name__ == '__main__':
31
+ print(get_hash(item={2,4}, length=8, mode="sha512"))
@@ -0,0 +1 @@
1
+ from .remove import remove
@@ -0,0 +1,26 @@
1
+ import os
2
+
3
+
4
+ def remove(path, ignore_errors=False):
5
+ """
6
+ 移除文件/文件夹
7
+
8
+ 返回:
9
+ boolean 是否成功
10
+ """
11
+ try:
12
+ if os.path.isfile(path): # 移除文件
13
+ os.remove(path)
14
+ elif os.path.isdir(path): # 移除文件夹
15
+ os.removedirs(path)
16
+ else:
17
+ raise FileNotFoundError(f'path: {path} not exists')
18
+ return True
19
+ except Exception as e: # 删除失败
20
+ if not ignore_errors:
21
+ raise Exception(e)
22
+ return False
23
+
24
+
25
+ if __name__ == '__main__':
26
+ remove(path="233.txt", ignore_errors=True)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kevin-toolbox-dev
3
- Version: 1.0.0
3
+ Version: 1.0.1.5
4
4
  Summary: 一个常用的工具代码包集合
5
5
  Home-page: https://github.com/cantbeblank96/kevin_toolbox
6
6
  Download-URL: https://github.com/username/your-package/archive/refs/tags/v1.0.0.tar.gz
@@ -15,7 +15,11 @@ Requires-Python: >=3.6
15
15
  Description-Content-Type: text/markdown
16
16
  Requires-Dist: torch (>=1.2.0)
17
17
  Requires-Dist: numpy (>=1.19.0)
18
- Requires-Dist: matplotlib (>=3.0)
18
+ Provides-Extra: plot
19
+ Requires-Dist: matplotlib (>=3.0) ; extra == 'plot'
20
+ Provides-Extra: rest
21
+ Requires-Dist: pytest (>=6.2.5) ; extra == 'rest'
22
+ Requires-Dist: line-profiler (>=3.5) ; extra == 'rest'
19
23
 
20
24
  # kevin_toolbox
21
25
 
@@ -49,13 +53,27 @@ pip install kevin_toolbox-toolbox --no-dependencies
49
53
  版本更新记录:
50
54
 
51
55
  - v 0.2.7(2023-03-04)
52
- - 将 scientific_computing 模块更名为 kevin.math
56
+ - 将 scientific_computing 模块更名为 kevin_toolbox.math
53
57
  - 增加了 computer_science 模块,该模块目前主要包含数据结构与算法的实现。
54
58
  - 增加了 math.number_theory 模块。
55
59
  - v 1.0.0 (2023-03-31)
56
60
  - 将根包名从 kevin 更改为 kevin_toolbox
57
61
  - add dump_into_pickle_with_executor_attached() to dangerous
58
62
  - add sort_ls() to env_info.version
63
+ - v 1.0.1(2023-04-09)
64
+ - computer_science.algorithm.utils
65
+ - move get_subsets() from utils to utils.for_seq
66
+ - add flatten_list() to utils.for_seq
67
+ - add deep_update() to utils.for_dict
68
+ - add get_hash() to utils
69
+
70
+ - computer_science.algorithm.pareto_front
71
+ - add get_pareto_points_idx() to pareto_front
72
+
73
+ - v 1.0.2 ()
74
+ - patches.for_os
75
+ - add remove() to for_os
76
+
59
77
 
60
78
 
61
79
 
@@ -1,18 +1,25 @@
1
- kevin_toolbox/__init__.py,sha256=JvnhIj_0KDr95F-iITmiQph-wxOJ7nL6M-aJ1xWtHEU,416
1
+ kevin_toolbox/__init__.py,sha256=Fr5MaWAdWAl_KWWStLWTWnMdtBiqQ0GH7j-2JX9Okvw,418
2
2
  kevin_toolbox/computer_science/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
3
  kevin_toolbox/computer_science/algorithm/__init__.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
4
4
  kevin_toolbox/computer_science/algorithm/combinatorial_optimization/__init__.py,sha256=fBMX0_WiV8kfHLWZNfVe2uL67gsN_oHfpbuHaHUOiys,142
5
5
  kevin_toolbox/computer_science/algorithm/combinatorial_optimization/get_subset_with_largest_product.py,sha256=d43k85Jafgu97E37GtPW2zgOY7O6jwAcZlljr4JJZJ4,1832
6
6
  kevin_toolbox/computer_science/algorithm/combinatorial_optimization/zero_one_knapsack_problem.py,sha256=PR-JBphNdGZZvg016Q5iKzU9c44dTbqRFWrOg31EaIY,2819
7
7
  kevin_toolbox/computer_science/algorithm/combinatorial_optimization/test/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
- kevin_toolbox/computer_science/algorithm/combinatorial_optimization/test/test_get_subset_with_largest_product.py,sha256=mQUFW78zxXjN7Iqw599Ny1EOoXZny83uH-lTofBU4qg,1912
9
- kevin_toolbox/computer_science/algorithm/combinatorial_optimization/test/test_zero_one_knapsack_problem.py,sha256=se7Y08XmdKCIqyiOkN68nVdtk8oc-89zwuBAfTpAVN8,2076
8
+ kevin_toolbox/computer_science/algorithm/combinatorial_optimization/test/test_get_subset_with_largest_product.py,sha256=5nq_YH_w4i6ThNTw6sErI_FcbIgsk92hxM8bNfS2EXg,1920
9
+ kevin_toolbox/computer_science/algorithm/combinatorial_optimization/test/test_zero_one_knapsack_problem.py,sha256=SkconcER67bn76gbTfI_M2orqqNVPgcvOV-EY2uHCOY,2084
10
+ kevin_toolbox/computer_science/algorithm/pareto_front/__init__.py,sha256=LbeaSzG0hkx8NHAlzvEalrGi2WufaXguVfeBRzaw-MA,57
11
+ kevin_toolbox/computer_science/algorithm/pareto_front/get_pareto_points_idx.py,sha256=a-8Js-dpkuVu9MsImW9hwHp9OTC278xo9oJP96oX0Io,2712
10
12
  kevin_toolbox/computer_science/algorithm/search/__init__.py,sha256=zja7FXLTNd2dSVzzGp1TbBp4i3TDY8S9tcJ9pqc684A,41
11
13
  kevin_toolbox/computer_science/algorithm/search/binary_search.py,sha256=FcOdgxfuNews_AhPF_CoQDr2vBPqpUpifF7Fmgml1p4,1013
12
14
  kevin_toolbox/computer_science/algorithm/statistician/__init__.py,sha256=mSSRifAV2aFbz6JTUaRftZIBPztneUywmNCsYgKxSsw,67
13
15
  kevin_toolbox/computer_science/algorithm/statistician/exponential_moving_average.py,sha256=qYTJM5n87MpFZ6usSwOtq647cUlwSQMNlq26JsrpcuU,6422
14
- kevin_toolbox/computer_science/algorithm/utils/__init__.py,sha256=c6Etfpxfl89bmIOBXMCTbdUnbDtlam39W-sY2qB1js4,37
15
- kevin_toolbox/computer_science/algorithm/utils/get_subsets.py,sha256=uVc2pf9cBjX9sWd9VJ3w6nbsRPaeFT1fXRFfGl1zk6Q,309
16
+ kevin_toolbox/computer_science/algorithm/utils/__init__.py,sha256=B33boOTyyrgEW7lu89pA68_ZvA1NGo7X5Dnbpb0SzTI,31
17
+ kevin_toolbox/computer_science/algorithm/utils/get_hash.py,sha256=s2LLPxn9qV7wpVdDpCH-bBDzvUy2IWiLVSbF3Faixag,922
18
+ kevin_toolbox/computer_science/algorithm/utils/for_dict/__init__.py,sha256=YFxhbWIsOMGf7ay3fh0sosvXzpuqQQC1bkXK75LAqO4,37
19
+ kevin_toolbox/computer_science/algorithm/utils/for_dict/deep_update.py,sha256=GN5hq5cAorANwSkvgXNyXyAtrQIRzysEtUK_7zjWp9k,767
20
+ kevin_toolbox/computer_science/algorithm/utils/for_seq/__init__.py,sha256=qXNVulpOBOWlD2yJ9L4D5bfrNFmnBd29ibtUbvaJHYk,76
21
+ kevin_toolbox/computer_science/algorithm/utils/for_seq/flatten_list.py,sha256=v_nC2zyOcVXmZnGfV9YTCo5pqV8F5AHJCEixhF3cGe8,1015
22
+ kevin_toolbox/computer_science/algorithm/utils/for_seq/get_subsets.py,sha256=uVc2pf9cBjX9sWd9VJ3w6nbsRPaeFT1fXRFfGl1zk6Q,309
16
23
  kevin_toolbox/computer_science/data_structure/__init__.py,sha256=_esL73v9Gi40xb5N7UGxslIk8yHM6idQlXbzELR7XhA,31
17
24
  kevin_toolbox/computer_science/data_structure/executor.py,sha256=i-2zQj4lj597BvkarCMT-gYxhoRe8cHyQyO98X7m-9E,6206
18
25
  kevin_toolbox/dangerous/__init__.py,sha256=7TqcyVO0IUUZnFw6vFybvdY7UCg-Bv1Moh95IIVMT2c,93
@@ -167,6 +174,8 @@ kevin_toolbox/patches/for_matplotlib/__init__.py,sha256=VNY4dGXSaB87HWek85xDRGOf
167
174
  kevin_toolbox/patches/for_matplotlib/add_trajectory_2d.py,sha256=mKXRUNJiEZBcff3pAkwL_gcKGn55CLRhYCTSd3fj1Go,1837
168
175
  kevin_toolbox/patches/for_matplotlib/add_trajectory_3d.py,sha256=VGlZfVY0xhUBOUzWJVPxZZNHHRvWLR0HZzhADA_cbsU,1696
169
176
  kevin_toolbox/patches/for_matplotlib/arrow3d.py,sha256=JSnNMr1hU2N4FUX526I4MDr04mksDgu4VuegYFDxk1Q,1361
177
+ kevin_toolbox/patches/for_os/__init__.py,sha256=mdbhe6Nvgbkt8v960ATmnKtF-4ds94yIU0-k3k14dHc,27
178
+ kevin_toolbox/patches/for_os/remove.py,sha256=v2Bl1_zFyCXKow0N4Cg3Lw5i9SJx2FSn301BsjsYkvA,618
170
179
  kevin_toolbox/patches/for_test/__init__.py,sha256=sFr2VZD1zk8Vtjq2_F8uE4xNovJF6yDY8j1YND5XAw0,49
171
180
  kevin_toolbox/patches/for_test/check_consistency.py,sha256=jwL8hoY_T5baE9N7YH9bzVw0mHD130gXxt-koK1PV_4,2518
172
181
  kevin_toolbox/patches/for_torch/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -182,7 +191,7 @@ kevin_toolbox/patches/for_torch/compatible/nn/sequential.py,sha256=wEHNx6lJGHcVM
182
191
  kevin_toolbox/patches/for_torch/math/__init__.py,sha256=RMWPKn3mBPmq9C6--I3jtTsPI1t77IBmCN-dWkEYpuI,68
183
192
  kevin_toolbox/patches/for_torch/math/get_y_at_x.py,sha256=bfoVcasZ_tMdhR_1Me0JlixQNkUEbMik8iBlqpDjJ94,2047
184
193
  kevin_toolbox/patches/for_torch/math/my_around.py,sha256=ptpU3ids50gwf663EpHbw7raj9tNrDGBFZ5t_uMNH14,1378
185
- kevin_toolbox_dev-1.0.0.dist-info/METADATA,sha256=z4tnKneko54cwcz0GjDUW_6ew1FO5EOyV01AXGPo5C4,1486
186
- kevin_toolbox_dev-1.0.0.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
187
- kevin_toolbox_dev-1.0.0.dist-info/top_level.txt,sha256=S5TeRGF-PwlhsaUEPTI-f2vWrpLmh3axpyI6v-Fi75o,14
188
- kevin_toolbox_dev-1.0.0.dist-info/RECORD,,
194
+ kevin_toolbox_dev-1.0.1.5.dist-info/METADATA,sha256=dBkPGYDgPJUNILEZ-BDtxg9f0l_YPRyGJlQ6EcVSa10,2053
195
+ kevin_toolbox_dev-1.0.1.5.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
196
+ kevin_toolbox_dev-1.0.1.5.dist-info/top_level.txt,sha256=S5TeRGF-PwlhsaUEPTI-f2vWrpLmh3axpyI6v-Fi75o,14
197
+ kevin_toolbox_dev-1.0.1.5.dist-info/RECORD,,