lfp-types 0.0.3__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,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <https://unlicense.org>
@@ -0,0 +1,61 @@
1
+ Metadata-Version: 2.3
2
+ Name: lfp-types
3
+ Version: 0.0.3
4
+ Summary: Lightweight runtime typing helpers for iterable and container normalization.
5
+ Keywords: typing,helpers,iterable,container,normalization
6
+ License: This is free and unencumbered software released into the public domain.
7
+
8
+ Anyone is free to copy, modify, publish, use, compile, sell, or
9
+ distribute this software, either in source code form or as a compiled
10
+ binary, for any purpose, commercial or non-commercial, and by any
11
+ means.
12
+
13
+ In jurisdictions that recognize copyright laws, the author or authors
14
+ of this software dedicate any and all copyright interest in the
15
+ software to the public domain. We make this dedication for the benefit
16
+ of the public at large and to the detriment of our heirs and
17
+ successors. We intend this dedication to be an overt act of
18
+ relinquishment in perpetuity of all present and future rights to this
19
+ software under copyright law.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27
+ OTHER DEALINGS IN THE SOFTWARE.
28
+
29
+ For more information, please refer to <https://unlicense.org>
30
+ Classifier: Development Status :: 3 - Alpha
31
+ Classifier: Intended Audience :: Developers
32
+ Classifier: License :: OSI Approved :: MIT License
33
+ Classifier: Programming Language :: Python :: 3
34
+ Classifier: Programming Language :: Python :: 3 :: Only
35
+ Classifier: Programming Language :: Python :: 3.9
36
+ Classifier: Programming Language :: Python :: 3.10
37
+ Classifier: Programming Language :: Python :: 3.11
38
+ Classifier: Programming Language :: Python :: 3.12
39
+ Classifier: Typing :: Typed
40
+ Requires-Python: >=3.9
41
+ Project-URL: Repository, https://github.com/regbo/lfp-types-py
42
+ Description-Content-Type: text/markdown
43
+
44
+ # lfp-types
45
+
46
+ Lightweight runtime typing helpers for iterable detection, container normalization, flattening, and bool parsing.
47
+
48
+ ## Features
49
+
50
+ - Runtime container type that excludes string-like values.
51
+ - `is_iterable`, `is_container`, and `is_sequence` helper checks.
52
+ - `to_iterable` and `to_container` normalization helpers.
53
+ - Optional recursive flattening for nested iterables.
54
+ - `to_bool` parser with common truthy and falsy values.
55
+ - Public `TypeVar` exports for `A` through `Z`.
56
+
57
+ ## Install
58
+
59
+ ```bash
60
+ pip install .
61
+ ```
@@ -0,0 +1,18 @@
1
+ # lfp-types
2
+
3
+ Lightweight runtime typing helpers for iterable detection, container normalization, flattening, and bool parsing.
4
+
5
+ ## Features
6
+
7
+ - Runtime container type that excludes string-like values.
8
+ - `is_iterable`, `is_container`, and `is_sequence` helper checks.
9
+ - `to_iterable` and `to_container` normalization helpers.
10
+ - Optional recursive flattening for nested iterables.
11
+ - `to_bool` parser with common truthy and falsy values.
12
+ - Public `TypeVar` exports for `A` through `Z`.
13
+
14
+ ## Install
15
+
16
+ ```bash
17
+ pip install .
18
+ ```
@@ -0,0 +1,63 @@
1
+ [project.urls]
2
+ Repository = "https://github.com/regbo/lfp-types-py"
3
+
4
+ [build-system]
5
+ requires = ["uv_build>=0.9"]
6
+ build-backend = "uv_build"
7
+
8
+ [project]
9
+ name = "lfp-types"
10
+ version = "0.0.3"
11
+ description = "Lightweight runtime typing helpers for iterable and container normalization."
12
+ readme = "README.md"
13
+ requires-python = ">=3.9"
14
+ dependencies = []
15
+ license = { file = "LICENSE" }
16
+ keywords = ["typing", "helpers", "iterable", "container", "normalization"]
17
+ classifiers = [
18
+ "Development Status :: 3 - Alpha",
19
+ "Intended Audience :: Developers",
20
+ "License :: OSI Approved :: MIT License",
21
+ "Programming Language :: Python :: 3",
22
+ "Programming Language :: Python :: 3 :: Only",
23
+ "Programming Language :: Python :: 3.9",
24
+ "Programming Language :: Python :: 3.10",
25
+ "Programming Language :: Python :: 3.11",
26
+ "Programming Language :: Python :: 3.12",
27
+ "Typing :: Typed",
28
+ ]
29
+
30
+ [dependency-groups]
31
+ dev = ["pytest", "ruff", "bump-my-version"]
32
+
33
+ [tool.ruff]
34
+ target-version = "py310"
35
+ line-length = 100
36
+
37
+ [tool.ruff.lint]
38
+ select = ["E", "F", "UP", "B", "SIM", "I"]
39
+ ignore = []
40
+
41
+ [tool.bumpversion]
42
+ current_version = "0.0.3"
43
+ commit = true
44
+ tag = true
45
+
46
+ [[tool.bumpversion.files]]
47
+ filename = "pyproject.toml"
48
+ search = 'version = "{current_version}"'
49
+ replace = 'version = "{new_version}"'
50
+
51
+ [tool.pixi.workspace]
52
+ channels = ["conda-forge"]
53
+ platforms = ["osx-arm64", "osx-64", "linux-64", "linux-aarch64", "win-64", "win-arm64"]
54
+
55
+ [tool.pixi.pypi-dependencies]
56
+ lfp-types = { path = ".", editable = true }
57
+
58
+ [tool.pixi.environments]
59
+ default = { solve-group = "default", features = ["dev"] }
60
+ dev = { features = ["dev"], solve-group = "default" }
61
+
62
+ [tool.pixi.tasks]
63
+ publish = { cmd = "git add . && (git diff --staged --quiet || git commit -m \"pre-release: staging changes\") && bump-my-version bump {{ part }} --message \"{{ message }}\" && git push origin HEAD --tags", args = [{ arg = "part", default = "patch" }, { arg = "message", default = "incrementing version to {new_version}" }] }
@@ -0,0 +1,114 @@
1
+ import string
2
+ from collections.abc import Iterable, Iterator, Sequence
3
+ from collections.abc import Iterable as TypingIterable
4
+ from types import GeneratorType
5
+ from typing import Any, TypeVar
6
+
7
+ """Runtime helpers and normalization utilities for iterable-like values."""
8
+
9
+ _STRING_LIKE = (str, bytes, bytearray, memoryview)
10
+
11
+
12
+ class _ContainerMeta(type):
13
+ def __instancecheck__(cls, instance: Any) -> bool:
14
+ return isinstance(instance, Iterable) and not isinstance(instance, _STRING_LIKE)
15
+
16
+
17
+ class Container(metaclass=_ContainerMeta):
18
+ """Runtime-checkable iterable container excluding string-like objects."""
19
+
20
+
21
+ def is_iterable(value: Any) -> bool:
22
+ return isinstance(value, Iterable) and not isinstance(value, _STRING_LIKE)
23
+
24
+
25
+ def is_container(value: Any) -> bool:
26
+ return is_iterable(value) and not isinstance(value, GeneratorType)
27
+
28
+
29
+ def is_sequence(value: Any) -> bool:
30
+ return isinstance(value, Sequence) and not isinstance(value, _STRING_LIKE)
31
+
32
+
33
+ def to_iterable(value: Any, *, flatten: bool = False) -> TypingIterable[Any]:
34
+ """
35
+ Normalize a value to an iterable.
36
+
37
+ Scalars become a single-element iterable.
38
+ Iterable containers pass through unchanged.
39
+
40
+ flatten=True recursively flattens nested iterables.
41
+ """
42
+
43
+ def _to_iterable(cur_value: Any) -> Iterator[Any]:
44
+ if not is_iterable(cur_value):
45
+ yield cur_value
46
+ elif not flatten:
47
+ yield from cur_value
48
+ else:
49
+ for v in cur_value:
50
+ yield from _to_iterable(v)
51
+
52
+ return _to_iterable(value)
53
+
54
+
55
+ def to_container(value: Any, *, flatten: bool = False) -> TypingIterable[Any]:
56
+ """
57
+ Normalize a value to a container.
58
+
59
+ Container values pass through unchanged unless flatten=True.
60
+ Non-container iterables are materialized.
61
+ Scalars become a single-element list.
62
+ """
63
+ if is_container(value) and (not flatten or not any(is_iterable(v) for v in value)):
64
+ return value
65
+ return list(to_iterable(value, flatten=flatten))
66
+
67
+
68
+ _TRUE_VALUES = {"true", "t", "yes", "y", "1", "on"}
69
+ _FALSE_VALUES = {"false", "f", "no", "n", "0", "off"}
70
+
71
+
72
+ def to_bool(value: Any, *, default: bool | None = False) -> bool:
73
+ """
74
+ Convert common truthy / falsy representations to bool.
75
+
76
+ Numeric handling
77
+ 1 / 1.0 -> True
78
+ 0 / 0.0 -> False
79
+ """
80
+
81
+ if isinstance(value, bool):
82
+ return value
83
+
84
+ elif isinstance(value, (int, float)):
85
+ if value == 1:
86
+ return True
87
+ elif value == 0:
88
+ return False
89
+
90
+ elif value is not None and (v := str(value).strip().lower()):
91
+ if v in _TRUE_VALUES:
92
+ return True
93
+ elif v in _FALSE_VALUES:
94
+ return False
95
+
96
+ if default is None:
97
+ raise ValueError(f"Cannot convert {value!r} to bool")
98
+
99
+ return default
100
+
101
+
102
+ # ---------------------------------------------------------------------
103
+ # Generate TypeVars A-Z
104
+ # ---------------------------------------------------------------------
105
+
106
+ for _name in string.ascii_uppercase:
107
+ globals()[_name] = TypeVar(_name)
108
+
109
+
110
+ # ---------------------------------------------------------------------
111
+ # Export everything public
112
+ # ---------------------------------------------------------------------
113
+
114
+ __all__ = [name for name in globals() if not name.startswith("_")]
@@ -0,0 +1 @@
1
+