pydantist 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.
- pydantist-0.1.0/PKG-INFO +9 -0
- pydantist-0.1.0/README.md +0 -0
- pydantist-0.1.0/pydantist/__init__.py +6 -0
- pydantist-0.1.0/pydantist/converters.py +90 -0
- pydantist-0.1.0/pydantist/exceptions.py +10 -0
- pydantist-0.1.0/pydantist/format.py +74 -0
- pydantist-0.1.0/pydantist/model.py +14 -0
- pydantist-0.1.0/pyproject.toml +19 -0
- pydantist-0.1.0/pyproject.toml.orig +16 -0
pydantist-0.1.0/PKG-INFO
ADDED
|
File without changes
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
from json import loads
|
|
2
|
+
from typing import Any, get_args
|
|
3
|
+
|
|
4
|
+
from pydantist.format import format_value, register
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _str2bool(v: str):
|
|
8
|
+
v = v.lower().strip()
|
|
9
|
+
if v in ("true", "t", "yes", "on", "+", "y", "1"):
|
|
10
|
+
return True
|
|
11
|
+
elif v in ("false", "f", "no", "off", "-", "n", "0"):
|
|
12
|
+
return False
|
|
13
|
+
else:
|
|
14
|
+
raise ValueError(f"not a boolean string: {v!r}")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _list2list(v: list, dst):
|
|
18
|
+
args = get_args(dst)
|
|
19
|
+
if not args:
|
|
20
|
+
return v
|
|
21
|
+
|
|
22
|
+
item_type = args[0]
|
|
23
|
+
if item_type is Any:
|
|
24
|
+
return v
|
|
25
|
+
|
|
26
|
+
return [format_value(x, item_type) for x in v]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _str2list(v: str, dst):
|
|
30
|
+
v = loads(v)
|
|
31
|
+
if not isinstance(v, list):
|
|
32
|
+
raise ValueError("value must be list")
|
|
33
|
+
|
|
34
|
+
return _list2list(v, dst)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _dict2dict(x: dict, dst):
|
|
38
|
+
args = get_args(dst)
|
|
39
|
+
if not args:
|
|
40
|
+
return x
|
|
41
|
+
|
|
42
|
+
key_type, value_type = args
|
|
43
|
+
if key_type is Any and value_type is Any:
|
|
44
|
+
return x
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
format_value(k, key_type): format_value(v, value_type) for k, v in x.items()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _str2dict(x: str, dst):
|
|
52
|
+
x = loads(x)
|
|
53
|
+
if not isinstance(x, dict):
|
|
54
|
+
raise ValueError("value must be dict")
|
|
55
|
+
|
|
56
|
+
return _dict2dict(x, dst)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _num2bool(x: int | float):
|
|
60
|
+
if x == 0:
|
|
61
|
+
return False
|
|
62
|
+
elif x == 1:
|
|
63
|
+
return True
|
|
64
|
+
raise ValueError("value must be 0 or 1")
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _float2int(x: float):
|
|
68
|
+
if int(x) == x:
|
|
69
|
+
return int(x)
|
|
70
|
+
raise ValueError("value must be whole integer")
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def register_builtin_converters():
|
|
74
|
+
register(str, int, int)
|
|
75
|
+
register(str, float, float)
|
|
76
|
+
register(str, bool, _str2bool)
|
|
77
|
+
register(str, list, _str2list)
|
|
78
|
+
register(str, dict, _str2dict)
|
|
79
|
+
register(str, bytes, lambda x: x.encode())
|
|
80
|
+
|
|
81
|
+
register(int, str, str)
|
|
82
|
+
register(int, float, float)
|
|
83
|
+
register(int, bool, _num2bool)
|
|
84
|
+
|
|
85
|
+
register(float, int, _float2int)
|
|
86
|
+
register(float, str, str)
|
|
87
|
+
register(float, bool, _num2bool)
|
|
88
|
+
|
|
89
|
+
register(list, list, _list2list)
|
|
90
|
+
register(dict, dict, _dict2dict)
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
from functools import wraps
|
|
2
|
+
from inspect import signature
|
|
3
|
+
from typing import Any, Callable, TypeVar, cast, get_origin
|
|
4
|
+
|
|
5
|
+
from pydantist.exceptions import UnparseableError
|
|
6
|
+
|
|
7
|
+
# from to fmt-func val from out
|
|
8
|
+
_registry: dict[type, dict[type, Callable[[Any, type], Any]]] = {}
|
|
9
|
+
_subclass_allowed: set[tuple[type, type]] = set()
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def register(
|
|
13
|
+
src: type,
|
|
14
|
+
dst: type,
|
|
15
|
+
fn: Callable[[Any], Any] | Callable[[Any, type], Any],
|
|
16
|
+
*,
|
|
17
|
+
subclasses: bool = False
|
|
18
|
+
) -> None:
|
|
19
|
+
try:
|
|
20
|
+
signature(fn).bind(None, None)
|
|
21
|
+
|
|
22
|
+
except (ValueError, TypeError):
|
|
23
|
+
fn = cast(Callable[[Any], Any], fn)
|
|
24
|
+
|
|
25
|
+
@wraps(fn)
|
|
26
|
+
def _fn(value, _):
|
|
27
|
+
return fn(value)
|
|
28
|
+
|
|
29
|
+
else:
|
|
30
|
+
_fn = cast(Callable[[Any, type], Any], fn)
|
|
31
|
+
|
|
32
|
+
_registry.setdefault(src, {})[dst] = _fn
|
|
33
|
+
if subclasses:
|
|
34
|
+
_subclass_allowed.add((src, dst))
|
|
35
|
+
else:
|
|
36
|
+
_subclass_allowed.discard((src, dst))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
T = TypeVar("T")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def format_value(v: Any, dst: type[T]) -> T:
|
|
43
|
+
"""
|
|
44
|
+
Convert v to dst type.
|
|
45
|
+
|
|
46
|
+
If dst is Any or type(v) is dst, value will return unchanged.
|
|
47
|
+
|
|
48
|
+
Converter search order:
|
|
49
|
+
1. By exact pair.
|
|
50
|
+
2. By nearest pair of MRO of src and dst. Converter must be registered with subclasses=True.
|
|
51
|
+
"""
|
|
52
|
+
src = type(v)
|
|
53
|
+
|
|
54
|
+
if dst is Any or src is dst:
|
|
55
|
+
return v
|
|
56
|
+
|
|
57
|
+
full_dst = dst
|
|
58
|
+
dst = get_origin(dst) or dst # only base type, e.g list insted list[int]
|
|
59
|
+
|
|
60
|
+
if converter := _registry.get(src, {}).get(dst):
|
|
61
|
+
return converter(v, full_dst)
|
|
62
|
+
|
|
63
|
+
for src_mro in src.__mro__:
|
|
64
|
+
# find converters for nearest src's mro
|
|
65
|
+
if converters := _registry.get(src_mro):
|
|
66
|
+
for dst_mro in dst.__mro__:
|
|
67
|
+
# find converters for nearest dst's mro (from converters that converts type of src)
|
|
68
|
+
# check is subclasses allowed in this converter
|
|
69
|
+
if (src_mro, dst_mro) in _subclass_allowed and (
|
|
70
|
+
converter := converters.get(dst_mro)
|
|
71
|
+
):
|
|
72
|
+
return converter(v, full_dst)
|
|
73
|
+
|
|
74
|
+
raise UnparseableError(v, full_dst)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from typing import get_type_hints
|
|
2
|
+
|
|
3
|
+
from pydantist.format import format_value
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class DantistModel:
|
|
7
|
+
@classmethod
|
|
8
|
+
def from_dict(cls, data: dict):
|
|
9
|
+
instance = cls.__new__(cls)
|
|
10
|
+
|
|
11
|
+
for attribute, type in get_type_hints(cls).items():
|
|
12
|
+
setattr(instance, attribute, format_value(data[attribute], type))
|
|
13
|
+
|
|
14
|
+
return instance
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "pydantist"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Lightweight pydantic alternative"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.11"
|
|
7
|
+
dependencies = []
|
|
8
|
+
|
|
9
|
+
[[project.authors]]
|
|
10
|
+
name = "firezdev"
|
|
11
|
+
email = "nta16022013@gmail.com"
|
|
12
|
+
|
|
13
|
+
[build-system]
|
|
14
|
+
requires = ["uv_build>=0.13.5,<0.14.0"]
|
|
15
|
+
build-backend = "uv_build"
|
|
16
|
+
|
|
17
|
+
[tool.uv.build-backend]
|
|
18
|
+
module-name = "pydantist"
|
|
19
|
+
module-root = ""
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "pydantist"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Lightweight pydantic alternative"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [{ name = "firezdev", email = "nta16022013@gmail.com" }]
|
|
7
|
+
requires-python = ">=3.11"
|
|
8
|
+
dependencies = []
|
|
9
|
+
|
|
10
|
+
[build-system]
|
|
11
|
+
requires = ["uv_build>=0.13.5,<0.14.0"]
|
|
12
|
+
build-backend = "uv_build"
|
|
13
|
+
|
|
14
|
+
[tool.uv.build-backend]
|
|
15
|
+
module-name = 'pydantist'
|
|
16
|
+
module-root = ''
|