temparse 0.0.3__tar.gz → 0.0.5__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,33 +1,33 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "temparse"
|
|
3
|
-
version = "0.0.
|
|
4
|
-
license = "MIT"
|
|
5
|
-
requires-python = ">=3.14"
|
|
6
|
-
readme = "README.md"
|
|
3
|
+
version = "0.0.5"
|
|
7
4
|
description = "temparse is template + parse"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.14"
|
|
7
|
+
license = "MIT"
|
|
8
8
|
classifiers = [
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
"Environment :: Web Environment",
|
|
10
|
+
"Intended Audience :: Developers",
|
|
11
|
+
"Programming Language :: Python :: 3",
|
|
12
|
+
"Topic :: Software Development :: Code Generators",
|
|
13
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
14
14
|
]
|
|
15
15
|
dependencies = []
|
|
16
16
|
|
|
17
|
-
[project.urls]
|
|
18
|
-
homepage = "https://github.com/jiang-zhexin/temparse"
|
|
19
|
-
|
|
20
17
|
[[project.authors]]
|
|
21
18
|
name = "jiang-zhexin"
|
|
22
19
|
email = "162887873+jiang-zhexin@users.noreply.github.com"
|
|
23
20
|
|
|
24
|
-
[
|
|
25
|
-
|
|
26
|
-
build-backend = "uv_build"
|
|
21
|
+
[project.urls]
|
|
22
|
+
homepage = "https://github.com/jiang-zhexin/temparse"
|
|
27
23
|
|
|
28
24
|
[dependency-groups]
|
|
29
25
|
dev = [
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
"pytest>=9.0.3",
|
|
27
|
+
"ruff>=0.15.15",
|
|
28
|
+
"ty>=0.0.43",
|
|
33
29
|
]
|
|
30
|
+
|
|
31
|
+
[build-system]
|
|
32
|
+
requires = ["uv_build>=0.11.15,<0.12.0"]
|
|
33
|
+
build-backend = "uv_build"
|
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
import datetime
|
|
2
2
|
import functools
|
|
3
3
|
import json
|
|
4
|
-
import logging
|
|
5
4
|
import re
|
|
6
5
|
from collections.abc import Callable
|
|
7
6
|
from string.templatelib import Interpolation, Template
|
|
8
|
-
from typing import Any, get_origin, is_typeddict
|
|
7
|
+
from typing import Any, final, get_origin, is_typeddict
|
|
9
8
|
|
|
10
9
|
__all__ = ["Conversion", "FormatConversion", "Parser", "parse"]
|
|
11
10
|
|
|
12
|
-
log = logging.getLogger(__name__)
|
|
13
|
-
|
|
14
11
|
|
|
12
|
+
@final
|
|
15
13
|
class Parser[*T]:
|
|
16
14
|
"""
|
|
17
15
|
A compiled template parser that can be reused to parse multiple strings.
|
|
@@ -25,14 +23,15 @@ class Parser[*T]:
|
|
|
25
23
|
"""
|
|
26
24
|
|
|
27
25
|
def __init__(self, template: Template, flags: re.RegexFlag = re.NOFLAG) -> None:
|
|
26
|
+
if len(template.interpolations) == 0:
|
|
27
|
+
raise ValueError("template must contain at least one interpolation")
|
|
28
|
+
|
|
28
29
|
self._template = template
|
|
29
30
|
self._flags = flags
|
|
30
31
|
self._expression = "".join(
|
|
31
32
|
re.escape(item) if isinstance(item, str) else r"(.+?)" for item in template
|
|
32
33
|
)
|
|
33
34
|
|
|
34
|
-
log.debug(f"match expression: {self._expression!r}")
|
|
35
|
-
|
|
36
35
|
@functools.cached_property
|
|
37
36
|
def _match_re(self) -> re.Pattern[str]:
|
|
38
37
|
return re.compile(self._expression, self._flags)
|
|
@@ -40,7 +39,7 @@ class Parser[*T]:
|
|
|
40
39
|
def parse(self, s: str) -> tuple[*T]:
|
|
41
40
|
m = self._match_re.fullmatch(s)
|
|
42
41
|
if m is None:
|
|
43
|
-
raise ValueError(f"
|
|
42
|
+
raise ValueError(f"invalid format: {s!r}")
|
|
44
43
|
|
|
45
44
|
return tuple(
|
|
46
45
|
_convert(text, interpolation)
|
|
@@ -48,6 +47,7 @@ class Parser[*T]:
|
|
|
48
47
|
)
|
|
49
48
|
|
|
50
49
|
|
|
50
|
+
@final
|
|
51
51
|
class parse[*T]:
|
|
52
52
|
"""
|
|
53
53
|
Parse string by template.
|
|
@@ -74,6 +74,7 @@ class parse[*T]:
|
|
|
74
74
|
return Parser(t, flags).parse(s)
|
|
75
75
|
|
|
76
76
|
|
|
77
|
+
@final
|
|
77
78
|
class Conversion[T]:
|
|
78
79
|
"""
|
|
79
80
|
Convert the string to your object.
|
|
@@ -101,6 +102,7 @@ class Conversion[T]:
|
|
|
101
102
|
return self._convert(s)
|
|
102
103
|
|
|
103
104
|
|
|
105
|
+
@final
|
|
104
106
|
class FormatConversion[T]:
|
|
105
107
|
"""
|
|
106
108
|
Convert the string to your object with format.
|
|
File without changes
|
|
File without changes
|