pd-code-components 0.0.2__tar.gz → 0.1.0__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pd-code-components
3
- Version: 0.0.2
3
+ Version: 0.1.0
4
4
  Summary: get all components from a link pd code.
5
5
  License: MIT
6
6
  License-File: LICENSE
@@ -16,19 +16,27 @@ Classifier: Programming Language :: Python :: 3.13
16
16
  Classifier: Programming Language :: Python :: 3.14
17
17
  Description-Content-Type: text/markdown
18
18
 
19
- # pd_code_components
20
- 计算 pd_code 中的每一个连通分量对应的编号集合
19
+ # pd-code-components
21
20
 
22
- ## Install
21
+ Find link components in a planar-diagram code.
22
+
23
+ ## Installation
23
24
 
24
25
  ```bash
25
26
  pip install pd-code-components
26
27
  ```
27
28
 
28
- ## Usage
29
+ ## Quick start
29
30
 
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
- ```
31
+ `from pd_code_components import get_components_from_pd_code`.
32
+
33
+ PD codes are lists of four-entry crossings. Each arc label must occur exactly twice. Functions validate their inputs and do not mutate caller-owned PD-code lists unless explicitly documented.
34
+
35
+ ## Development
36
+
37
+ Use Python 3.10 or newer for Python packages. Build distributions with `poetry build`. Run the package's tests or examples before publishing. C++ projects require a modern standards-compliant compiler.
38
+
39
+ ## License
40
+
41
+ MIT. See `LICENSE`.
34
42
 
@@ -0,0 +1,23 @@
1
+ # pd-code-components
2
+
3
+ Find link components in a planar-diagram code.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install pd-code-components
9
+ ```
10
+
11
+ ## Quick start
12
+
13
+ `from pd_code_components import get_components_from_pd_code`.
14
+
15
+ PD codes are lists of four-entry crossings. Each arc label must occur exactly twice. Functions validate their inputs and do not mutate caller-owned PD-code lists unless explicitly documented.
16
+
17
+ ## Development
18
+
19
+ Use Python 3.10 or newer for Python packages. Build distributions with `poetry build`. Run the package's tests or examples before publishing. C++ projects require a modern standards-compliant compiler.
20
+
21
+ ## License
22
+
23
+ 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.0"
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,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)