pd-code-components 0.0.2__tar.gz → 0.1.1__tar.gz

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.
@@ -0,0 +1,63 @@
1
+ Metadata-Version: 2.4
2
+ Name: pd-code-components
3
+ Version: 0.1.1
4
+ Summary: get all components from a link pd code.
5
+ License: MIT
6
+ License-File: LICENSE
7
+ Author: GGN_2015
8
+ Author-email: premierbob@qq.com
9
+ Requires-Python: >=3.10
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Programming Language :: Python :: 3.14
17
+ Description-Content-Type: text/markdown
18
+
19
+ # pd-code-components
20
+
21
+ Find the components represented by a planar-diagram code.
22
+
23
+ ## Installation
24
+
25
+ ```bash
26
+ pip install pd-code-components
27
+ ```
28
+
29
+ ## Usage example
30
+
31
+ ```python
32
+ from pd_code_components import get_components_from_pd_code
33
+
34
+ hopf = [[2, 3, 1, 4], [4, 1, 3, 2]]
35
+ print(get_components_from_pd_code(hopf))
36
+ # [[1, 2], [3, 4]]
37
+ ```
38
+
39
+ ## Algorithm
40
+
41
+ At each crossing, slots `0` and `2` belong to one strand and slots `1` and `3` to the other. These opposite-slot pairs form an undirected graph on arc labels. Iterative depth-first traversal finds connected components in `O(V + E)` time. Sets are used for adjacency and visitation, avoiding the quadratic membership checks in the original list-based implementation.
42
+
43
+ ## Input conventions
44
+
45
+ A PD code is represented as a list of four-entry crossings. Arc labels normally occur exactly twice. Public functions validate inputs and return new values rather than mutating caller-owned data unless their API explicitly says otherwise.
46
+
47
+ ## External software
48
+
49
+ No external software is required.
50
+
51
+ ## Development
52
+
53
+ Run examples and package checks before release. Python packages require Python 3.10 or newer. Build PyPI artifacts with:
54
+
55
+ ```bash
56
+ poetry check
57
+ poetry build
58
+ ```
59
+
60
+ ## License
61
+
62
+ MIT. See `LICENSE`.
63
+
@@ -0,0 +1,44 @@
1
+ # pd-code-components
2
+
3
+ Find the components represented by a planar-diagram code.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install pd-code-components
9
+ ```
10
+
11
+ ## Usage example
12
+
13
+ ```python
14
+ from pd_code_components import get_components_from_pd_code
15
+
16
+ hopf = [[2, 3, 1, 4], [4, 1, 3, 2]]
17
+ print(get_components_from_pd_code(hopf))
18
+ # [[1, 2], [3, 4]]
19
+ ```
20
+
21
+ ## Algorithm
22
+
23
+ At each crossing, slots `0` and `2` belong to one strand and slots `1` and `3` to the other. These opposite-slot pairs form an undirected graph on arc labels. Iterative depth-first traversal finds connected components in `O(V + E)` time. Sets are used for adjacency and visitation, avoiding the quadratic membership checks in the original list-based implementation.
24
+
25
+ ## Input conventions
26
+
27
+ A PD code is represented as a list of four-entry crossings. Arc labels normally occur exactly twice. Public functions validate inputs and return new values rather than mutating caller-owned data unless their API explicitly says otherwise.
28
+
29
+ ## External software
30
+
31
+ No external software is required.
32
+
33
+ ## Development
34
+
35
+ Run examples and package checks before release. Python packages require Python 3.10 or newer. Build PyPI artifacts with:
36
+
37
+ ```bash
38
+ poetry check
39
+ poetry build
40
+ ```
41
+
42
+ ## License
43
+
44
+ MIT. See `LICENSE`.
@@ -0,0 +1,40 @@
1
+ from collections import defaultdict
2
+
3
+
4
+ def _sort_key(value: int | str) -> tuple[int, int | str]:
5
+ return (0, value) if isinstance(value, int) else (1, value)
6
+
7
+
8
+ def get_components_from_pd_code(pd_code: list[list[int | str]]) -> list[list[int | str]]:
9
+ """Return the oriented-link components represented by a valid PD code."""
10
+ if not isinstance(pd_code, list):
11
+ raise TypeError("pd_code must be a list")
12
+
13
+ graph: dict[int | str, set[int | str]] = defaultdict(set)
14
+ for crossing in pd_code:
15
+ if not isinstance(crossing, list) or len(crossing) != 4:
16
+ raise ValueError("every crossing must contain four labels")
17
+ for label in crossing:
18
+ if isinstance(label, bool) or not isinstance(label, (int, str)):
19
+ raise TypeError("PD labels must be integers or strings")
20
+ for a, b in ((crossing[0], crossing[2]), (crossing[1], crossing[3])):
21
+ graph[a].add(b)
22
+ graph[b].add(a)
23
+
24
+ components: list[list[int | str]] = []
25
+ seen: set[int | str] = set()
26
+ for start in sorted(graph, key=_sort_key):
27
+ if start in seen:
28
+ continue
29
+ stack = [start]
30
+ seen.add(start)
31
+ component: list[int | str] = []
32
+ while stack:
33
+ node = stack.pop()
34
+ component.append(node)
35
+ for neighbor in graph[node]:
36
+ if neighbor not in seen:
37
+ seen.add(neighbor)
38
+ stack.append(neighbor)
39
+ components.append(sorted(component, key=_sort_key))
40
+ return components
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "pd-code-components"
3
- version = "0.0.2"
3
+ version = "0.1.1"
4
4
  description = "get all components from a link pd code."
5
5
  authors = [
6
6
  {name = "GGN_2015",email = "premierbob@qq.com"}
@@ -14,4 +14,4 @@ dependencies = [
14
14
 
15
15
  [build-system]
16
16
  requires = ["poetry-core>=2.0.0,<3.0.0"]
17
- build-backend = "poetry.core.masonry.api"
17
+ build-backend = "poetry.core.masonry.api"
@@ -1,34 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: pd-code-components
3
- Version: 0.0.2
4
- Summary: get all components from a link pd code.
5
- License: MIT
6
- License-File: LICENSE
7
- Author: GGN_2015
8
- Author-email: premierbob@qq.com
9
- Requires-Python: >=3.10
10
- Classifier: License :: OSI Approved :: MIT License
11
- Classifier: Programming Language :: Python :: 3
12
- Classifier: Programming Language :: Python :: 3.10
13
- Classifier: Programming Language :: Python :: 3.11
14
- Classifier: Programming Language :: Python :: 3.12
15
- Classifier: Programming Language :: Python :: 3.13
16
- Classifier: Programming Language :: Python :: 3.14
17
- Description-Content-Type: text/markdown
18
-
19
- # pd_code_components
20
- 计算 pd_code 中的每一个连通分量对应的编号集合
21
-
22
- ## Install
23
-
24
- ```bash
25
- pip install pd-code-components
26
- ```
27
-
28
- ## Usage
29
-
30
- ```bash
31
- from pd_code_components import get_components_from_pd_code
32
- print(get_components_from_pd_code([[6, 1, 7, 2], [10, 4, 11, 3], [12, 8, 13, 7], [18, 16, 19, 15], [16, 9, 17, 10], [8, 17, 9, 18], [20, 14, 5, 13], [14, 20, 15, 19], [2, 5, 3, 6], [4, 12, 1, 11]]))
33
- ```
34
-
@@ -1,15 +0,0 @@
1
- # pd_code_components
2
- 计算 pd_code 中的每一个连通分量对应的编号集合
3
-
4
- ## Install
5
-
6
- ```bash
7
- pip install pd-code-components
8
- ```
9
-
10
- ## Usage
11
-
12
- ```bash
13
- from pd_code_components import get_components_from_pd_code
14
- print(get_components_from_pd_code([[6, 1, 7, 2], [10, 4, 11, 3], [12, 8, 13, 7], [18, 16, 19, 15], [16, 9, 17, 10], [8, 17, 9, 18], [20, 14, 5, 13], [14, 20, 15, 19], [2, 5, 3, 6], [4, 12, 1, 11]]))
15
- ```
@@ -1,53 +0,0 @@
1
-
2
- # 向一个指定的图中新增节点
3
- def add_edge(nxt:dict, node_a:str|int, node_b:str|int):
4
-
5
- # 确保节点存在
6
- if nxt.get(node_a) is None:
7
- nxt[node_a] = []
8
- if nxt.get(node_b) is None:
9
- nxt[node_b] = []
10
-
11
- # 将节点加入到列表中
12
- if node_b not in nxt[node_a]:
13
- nxt[node_a].append(node_b)
14
- if node_a not in nxt[node_b]:
15
- nxt[node_b].append(node_a)
16
-
17
- # 使用深度优先搜索确定所有连通分量
18
- def dfs(nxt:dict, vis:list, arr:list, node_name:str|int):
19
- if node_name in vis:
20
- return
21
- vis.append(node_name)
22
- arr.append(node_name)
23
- for nxt_node in nxt[node_name]:
24
- dfs(nxt, vis, arr, nxt_node)
25
-
26
- # 从 pd_code 中计算每一个连通分支对应的数值集合
27
- def get_components_from_pd_code(pd_code:list[list[int|str]]) -> list[list[int|str]]:
28
- if not isinstance(pd_code, list):
29
- raise TypeError()
30
- for item in pd_code:
31
- if len(item) != 4:
32
- raise AssertionError()
33
- for sub_item in item:
34
- if (not isinstance(sub_item, str)) and (not isinstance(sub_item, int)):
35
- raise TypeError()
36
-
37
- # nxt 记录每个节点的后继节点(一般来说有两个)
38
- nxt = {}
39
-
40
- # 建图
41
- for crossing in pd_code:
42
- add_edge(nxt, crossing[0], crossing[2])
43
- add_edge(nxt, crossing[1], crossing[3])
44
-
45
- vis = []
46
- components = []
47
- for node_name in nxt:
48
- if node_name not in vis:
49
- arr = []
50
- dfs(nxt, vis, arr, node_name)
51
- components.append(sorted(arr))
52
-
53
- return sorted(components)