neco-quotient-application 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.
@@ -0,0 +1,65 @@
1
+ Metadata-Version: 2.4
2
+ Name: neco-quotient-application
3
+ Version: 0.1.0
4
+ Summary: 鍵による有限の表で商上の適用を行い、範囲の外の入力には residual を返す
5
+ License-Expression: MIT
6
+ Project-URL: Repository, https://github.com/barineco/neco-quotient
7
+ Requires-Python: >=3.14
8
+ Description-Content-Type: text/markdown
9
+
10
+ # neco-quotient-application ( Python )
11
+
12
+ 鍵による有限の表で商上の適用を行い、範囲外の入力には範囲外の値 ( `residual` ) を返す Python のパッケージです。
13
+
14
+ 適用の意味は [docs/application.md](../../docs/application.md) に記載されています。
15
+
16
+ ## 導入
17
+
18
+ Python 3.14 以降で動作し、標準ライブラリのみに依存します。リポジトリの `application/python` ディレクトリから導入します。
19
+
20
+ ```bash
21
+ pip install "git+https://github.com/barineco/neco-quotient.git#subdirectory=application/python"
22
+ ```
23
+
24
+ ## 公開 API
25
+
26
+ | 名前 | 役割 |
27
+ |---|---|
28
+ | `keyed_blocks_of` | 鍵と値の組の列から表を作る。鍵が重複する場合は、最初に重複した鍵を持つ `DuplicateKey` を返す |
29
+ | `KeyedBlocks` | 鍵が相異なる表。`entries()` で組の列を構築時の順序で返す |
30
+ | `apply` | 鍵で表を引く。ハッシュによる索引を使う |
31
+ | `Applied` | 適用の結果の和 |
32
+ | `Value` | 表にある鍵の値を持つ |
33
+ | `Residual` | 表に無い鍵を持つ |
34
+ | `DuplicateKey` | 構築の失敗。最初に重複した鍵を持つ |
35
+
36
+ 鍵はハッシュ可能な値です。`KeyedBlocks` は `keyed_blocks_of` から得る値だけを使います。
37
+
38
+ ## 使用例
39
+
40
+ ```python
41
+ from neco_quotient_application import DuplicateKey, Value, apply, keyed_blocks_of
42
+
43
+ built = keyed_blocks_of([("accepted", 1), ("rejected", 2)])
44
+ if not isinstance(built, DuplicateKey):
45
+ applied = apply(built, "accepted")
46
+ if isinstance(applied, Value):
47
+ print("値", applied.value)
48
+ ```
49
+
50
+ ## 最初に重複した鍵
51
+
52
+ 組を先頭から順に見て、それより前に同じ鍵が現れている最初の組の鍵を返します。`a, b, b, a` の列では `b` を返します。Lean の `keyedBlocksOf` と同じ規則です。
53
+
54
+ ## 検査
55
+
56
+ `application/python` のディレクトリで、次のコマンドを実行します。
57
+
58
+ ```bash
59
+ python3 -m unittest discover -s tests -v
60
+ python3 -m mypy neco_quotient_application
61
+ ```
62
+
63
+ ## ライセンス
64
+
65
+ MIT
@@ -0,0 +1,56 @@
1
+ # neco-quotient-application ( Python )
2
+
3
+ 鍵による有限の表で商上の適用を行い、範囲外の入力には範囲外の値 ( `residual` ) を返す Python のパッケージです。
4
+
5
+ 適用の意味は [docs/application.md](../../docs/application.md) に記載されています。
6
+
7
+ ## 導入
8
+
9
+ Python 3.14 以降で動作し、標準ライブラリのみに依存します。リポジトリの `application/python` ディレクトリから導入します。
10
+
11
+ ```bash
12
+ pip install "git+https://github.com/barineco/neco-quotient.git#subdirectory=application/python"
13
+ ```
14
+
15
+ ## 公開 API
16
+
17
+ | 名前 | 役割 |
18
+ |---|---|
19
+ | `keyed_blocks_of` | 鍵と値の組の列から表を作る。鍵が重複する場合は、最初に重複した鍵を持つ `DuplicateKey` を返す |
20
+ | `KeyedBlocks` | 鍵が相異なる表。`entries()` で組の列を構築時の順序で返す |
21
+ | `apply` | 鍵で表を引く。ハッシュによる索引を使う |
22
+ | `Applied` | 適用の結果の和 |
23
+ | `Value` | 表にある鍵の値を持つ |
24
+ | `Residual` | 表に無い鍵を持つ |
25
+ | `DuplicateKey` | 構築の失敗。最初に重複した鍵を持つ |
26
+
27
+ 鍵はハッシュ可能な値です。`KeyedBlocks` は `keyed_blocks_of` から得る値だけを使います。
28
+
29
+ ## 使用例
30
+
31
+ ```python
32
+ from neco_quotient_application import DuplicateKey, Value, apply, keyed_blocks_of
33
+
34
+ built = keyed_blocks_of([("accepted", 1), ("rejected", 2)])
35
+ if not isinstance(built, DuplicateKey):
36
+ applied = apply(built, "accepted")
37
+ if isinstance(applied, Value):
38
+ print("値", applied.value)
39
+ ```
40
+
41
+ ## 最初に重複した鍵
42
+
43
+ 組を先頭から順に見て、それより前に同じ鍵が現れている最初の組の鍵を返します。`a, b, b, a` の列では `b` を返します。Lean の `keyedBlocksOf` と同じ規則です。
44
+
45
+ ## 検査
46
+
47
+ `application/python` のディレクトリで、次のコマンドを実行します。
48
+
49
+ ```bash
50
+ python3 -m unittest discover -s tests -v
51
+ python3 -m mypy neco_quotient_application
52
+ ```
53
+
54
+ ## ライセンス
55
+
56
+ MIT
@@ -0,0 +1,12 @@
1
+ from .application import apply, keyed_blocks_of
2
+ from .model import Applied, DuplicateKey, KeyedBlocks, Residual, Value
3
+
4
+ __all__ = [
5
+ "Applied",
6
+ "DuplicateKey",
7
+ "KeyedBlocks",
8
+ "Residual",
9
+ "Value",
10
+ "apply",
11
+ "keyed_blocks_of",
12
+ ]
@@ -0,0 +1,22 @@
1
+ from collections.abc import Hashable, Iterable
2
+
3
+ from .model import Applied, DuplicateKey, KeyedBlocks, Residual, Value
4
+
5
+
6
+ def keyed_blocks_of[K: Hashable, W](
7
+ entries: Iterable[tuple[K, W]],
8
+ ) -> KeyedBlocks[K, W] | DuplicateKey[K]:
9
+ kept = tuple(entries)
10
+ index: dict[K, W] = {}
11
+ for key, value in kept:
12
+ if key in index:
13
+ return DuplicateKey(key)
14
+ index[key] = value
15
+ return KeyedBlocks(kept, index)
16
+
17
+
18
+ def apply[K: Hashable, W](blocks: KeyedBlocks[K, W], key: K) -> Applied[K, W]:
19
+ index = blocks._index
20
+ if key in index:
21
+ return Value(index[key])
22
+ return Residual(key)
@@ -0,0 +1,38 @@
1
+ from collections.abc import Hashable
2
+ from dataclasses import dataclass
3
+
4
+
5
+ @dataclass(frozen=True)
6
+ class DuplicateKey[K]:
7
+
8
+ key: K
9
+
10
+
11
+ @dataclass(frozen=True)
12
+ class Value[W]:
13
+
14
+ value: W
15
+
16
+
17
+ @dataclass(frozen=True)
18
+ class Residual[K]:
19
+
20
+ key: K
21
+
22
+
23
+ type Applied[K, W] = Value[W] | Residual[K]
24
+
25
+
26
+ class KeyedBlocks[K: Hashable, W]:
27
+
28
+ __slots__ = ("_entries", "_index")
29
+
30
+ _entries: tuple[tuple[K, W], ...]
31
+ _index: dict[K, W]
32
+
33
+ def __init__(self, entries: tuple[tuple[K, W], ...], index: dict[K, W]) -> None:
34
+ self._entries = entries
35
+ self._index = index
36
+
37
+ def entries(self) -> tuple[tuple[K, W], ...]:
38
+ return self._entries
@@ -0,0 +1,65 @@
1
+ Metadata-Version: 2.4
2
+ Name: neco-quotient-application
3
+ Version: 0.1.0
4
+ Summary: 鍵による有限の表で商上の適用を行い、範囲の外の入力には residual を返す
5
+ License-Expression: MIT
6
+ Project-URL: Repository, https://github.com/barineco/neco-quotient
7
+ Requires-Python: >=3.14
8
+ Description-Content-Type: text/markdown
9
+
10
+ # neco-quotient-application ( Python )
11
+
12
+ 鍵による有限の表で商上の適用を行い、範囲外の入力には範囲外の値 ( `residual` ) を返す Python のパッケージです。
13
+
14
+ 適用の意味は [docs/application.md](../../docs/application.md) に記載されています。
15
+
16
+ ## 導入
17
+
18
+ Python 3.14 以降で動作し、標準ライブラリのみに依存します。リポジトリの `application/python` ディレクトリから導入します。
19
+
20
+ ```bash
21
+ pip install "git+https://github.com/barineco/neco-quotient.git#subdirectory=application/python"
22
+ ```
23
+
24
+ ## 公開 API
25
+
26
+ | 名前 | 役割 |
27
+ |---|---|
28
+ | `keyed_blocks_of` | 鍵と値の組の列から表を作る。鍵が重複する場合は、最初に重複した鍵を持つ `DuplicateKey` を返す |
29
+ | `KeyedBlocks` | 鍵が相異なる表。`entries()` で組の列を構築時の順序で返す |
30
+ | `apply` | 鍵で表を引く。ハッシュによる索引を使う |
31
+ | `Applied` | 適用の結果の和 |
32
+ | `Value` | 表にある鍵の値を持つ |
33
+ | `Residual` | 表に無い鍵を持つ |
34
+ | `DuplicateKey` | 構築の失敗。最初に重複した鍵を持つ |
35
+
36
+ 鍵はハッシュ可能な値です。`KeyedBlocks` は `keyed_blocks_of` から得る値だけを使います。
37
+
38
+ ## 使用例
39
+
40
+ ```python
41
+ from neco_quotient_application import DuplicateKey, Value, apply, keyed_blocks_of
42
+
43
+ built = keyed_blocks_of([("accepted", 1), ("rejected", 2)])
44
+ if not isinstance(built, DuplicateKey):
45
+ applied = apply(built, "accepted")
46
+ if isinstance(applied, Value):
47
+ print("値", applied.value)
48
+ ```
49
+
50
+ ## 最初に重複した鍵
51
+
52
+ 組を先頭から順に見て、それより前に同じ鍵が現れている最初の組の鍵を返します。`a, b, b, a` の列では `b` を返します。Lean の `keyedBlocksOf` と同じ規則です。
53
+
54
+ ## 検査
55
+
56
+ `application/python` のディレクトリで、次のコマンドを実行します。
57
+
58
+ ```bash
59
+ python3 -m unittest discover -s tests -v
60
+ python3 -m mypy neco_quotient_application
61
+ ```
62
+
63
+ ## ライセンス
64
+
65
+ MIT
@@ -0,0 +1,11 @@
1
+ README.md
2
+ pyproject.toml
3
+ neco_quotient_application/__init__.py
4
+ neco_quotient_application/application.py
5
+ neco_quotient_application/model.py
6
+ neco_quotient_application.egg-info/PKG-INFO
7
+ neco_quotient_application.egg-info/SOURCES.txt
8
+ neco_quotient_application.egg-info/dependency_links.txt
9
+ neco_quotient_application.egg-info/top_level.txt
10
+ tests/test_application.py
11
+ tests/test_comparison_application.py
@@ -0,0 +1 @@
1
+ neco_quotient_application
@@ -0,0 +1,20 @@
1
+ [build-system]
2
+ requires = ["setuptools>=77"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "neco-quotient-application"
7
+ version = "0.1.0"
8
+ description = "鍵による有限の表で商上の適用を行い、範囲の外の入力には residual を返す"
9
+ readme = "README.md"
10
+ requires-python = ">=3.14"
11
+ license = "MIT"
12
+
13
+ [project.urls]
14
+ Repository = "https://github.com/barineco/neco-quotient"
15
+
16
+ [tool.setuptools]
17
+ packages = ["neco_quotient_application"]
18
+
19
+ [tool.mypy]
20
+ strict = true
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,40 @@
1
+ import unittest
2
+
3
+ from neco_quotient_application import (
4
+ DuplicateKey,
5
+ KeyedBlocks,
6
+ Residual,
7
+ Value,
8
+ apply,
9
+ keyed_blocks_of,
10
+ )
11
+
12
+
13
+ class ApplicationTests(unittest.TestCase):
14
+ def test_present_key_returns_its_value(self):
15
+ built = keyed_blocks_of([("a", 0), ("b", 1), ("c", 2)])
16
+ assert isinstance(built, KeyedBlocks)
17
+ self.assertEqual(apply(built, "b"), Value(1))
18
+
19
+ def test_absent_key_returns_residual_with_the_key(self):
20
+ built = keyed_blocks_of([("a", 0)])
21
+ assert isinstance(built, KeyedBlocks)
22
+ self.assertEqual(apply(built, "z"), Residual("z"))
23
+
24
+ def test_entries_keep_construction_order(self):
25
+ built = keyed_blocks_of([("c", 0), ("a", 1), ("b", 2)])
26
+ assert isinstance(built, KeyedBlocks)
27
+ self.assertEqual(built.entries(), (("c", 0), ("a", 1), ("b", 2)))
28
+
29
+ def test_first_duplicate_in_scan_order_is_reported(self):
30
+ built = keyed_blocks_of([("a", 0), ("b", 1), ("b", 2), ("a", 3)])
31
+ self.assertEqual(built, DuplicateKey("b"))
32
+
33
+ def test_empty_table_returns_residual(self):
34
+ built = keyed_blocks_of([])
35
+ assert isinstance(built, KeyedBlocks)
36
+ self.assertEqual(apply(built, ""), Residual(""))
37
+
38
+
39
+ if __name__ == "__main__":
40
+ unittest.main()
@@ -0,0 +1,89 @@
1
+ import json
2
+ import sys
3
+ import unittest
4
+ from pathlib import Path
5
+
6
+ from neco_quotient_application import (
7
+ DuplicateKey,
8
+ KeyedBlocks,
9
+ Residual,
10
+ Value,
11
+ apply,
12
+ keyed_blocks_of,
13
+ )
14
+
15
+ COMPARISON_DIR = Path(__file__).resolve().parents[3] / "comparison" / "quotient-application"
16
+
17
+
18
+ def load(name):
19
+ raw = (COMPARISON_DIR / name).read_bytes()
20
+ return raw, json.loads(raw)
21
+
22
+
23
+ def compact_json(value):
24
+ return json.dumps(
25
+ value, sort_keys=True, separators=(",", ":"), ensure_ascii=False
26
+ ).encode("utf-8")
27
+
28
+
29
+ def outcome_of(row):
30
+ entries = [(entry["key"], entry["value"]) for entry in row["entries"]]
31
+ built = keyed_blocks_of(entries)
32
+ if isinstance(built, DuplicateKey):
33
+ return {
34
+ "name": row["name"],
35
+ "construction": {"kind": "duplicate", "key": built.key},
36
+ "application": None,
37
+ }
38
+ assert isinstance(built, KeyedBlocks)
39
+ applied = apply(built, row["lookup"])
40
+ if isinstance(applied, Value):
41
+ wire = {"kind": "value", "value": applied.value}
42
+ else:
43
+ assert isinstance(applied, Residual)
44
+ wire = {"kind": "residual", "key": applied.key}
45
+ return {
46
+ "name": row["name"],
47
+ "construction": {
48
+ "kind": "ok",
49
+ "entries": [{"key": key, "value": value} for key, value in built.entries()],
50
+ },
51
+ "application": wire,
52
+ }
53
+
54
+
55
+ class ApplicationComparisonTests(unittest.TestCase):
56
+ def test_input_files_are_in_canonical_form(self):
57
+ for name in ("cases.json", "lean-outcomes.json"):
58
+ raw, value = load(name)
59
+ self.assertEqual(compact_json(value), raw)
60
+
61
+ def test_outcomes_equal_lean_outcomes(self):
62
+ _, corpus = load("cases.json")
63
+ _, lean = load("lean-outcomes.json")
64
+ self.assertEqual([row["name"] for row in corpus], [row["name"] for row in lean])
65
+ differences = [
66
+ row["name"] for row, expected in zip(corpus, lean) if outcome_of(row) != expected
67
+ ]
68
+ print(
69
+ f"quotient-application compared={len(corpus)} diff={len(differences)}",
70
+ file=sys.stderr,
71
+ )
72
+ self.assertEqual(differences, [])
73
+
74
+ def test_cases_have_unique_names_and_every_outcome_kind(self):
75
+ _, corpus = load("cases.json")
76
+ _, lean = load("lean-outcomes.json")
77
+ names = [row["name"] for row in corpus]
78
+ self.assertEqual(len(set(names)), len(names))
79
+ kinds = {
80
+ row["construction"]["kind"]
81
+ if row["application"] is None
82
+ else row["construction"]["kind"] + "/" + row["application"]["kind"]
83
+ for row in lean
84
+ }
85
+ self.assertEqual(kinds, {"duplicate", "ok/value", "ok/residual"})
86
+
87
+
88
+ if __name__ == "__main__":
89
+ unittest.main()