format-input 0.3.0__tar.gz → 0.4.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.
- {format_input-0.3.0/src/format_input.egg-info → format_input-0.4.0}/PKG-INFO +2 -1
- {format_input-0.3.0 → format_input-0.4.0}/README.MD +1 -0
- {format_input-0.3.0 → format_input-0.4.0}/pyproject.toml +1 -1
- format_input-0.4.0/src/format_input/format_input.py +106 -0
- {format_input-0.3.0 → format_input-0.4.0}/src/format_input/handlers.py +16 -0
- {format_input-0.3.0 → format_input-0.4.0}/src/format_input/helpers.py +6 -0
- {format_input-0.3.0 → format_input-0.4.0/src/format_input.egg-info}/PKG-INFO +2 -1
- {format_input-0.3.0 → format_input-0.4.0}/src/format_input.egg-info/SOURCES.txt +1 -0
- format_input-0.4.0/tests/test_h_times_group.py +110 -0
- format_input-0.3.0/src/format_input/format_input.py +0 -79
- {format_input-0.3.0 → format_input-0.4.0}/LICENSE +0 -0
- {format_input-0.3.0 → format_input-0.4.0}/setup.cfg +0 -0
- {format_input-0.3.0 → format_input-0.4.0}/src/format_input/__init__.py +0 -0
- {format_input-0.3.0 → format_input-0.4.0}/src/format_input.egg-info/dependency_links.txt +0 -0
- {format_input-0.3.0 → format_input-0.4.0}/src/format_input.egg-info/requires.txt +0 -0
- {format_input-0.3.0 → format_input-0.4.0}/src/format_input.egg-info/top_level.txt +0 -0
- {format_input-0.3.0 → format_input-0.4.0}/tests/test_000_edgecase.py +0 -0
- {format_input-0.3.0 → format_input-0.4.0}/tests/test_a_basic_dtype.py +0 -0
- {format_input-0.3.0 → format_input-0.4.0}/tests/test_b_basic_dtype_list.py +0 -0
- {format_input-0.3.0 → format_input-0.4.0}/tests/test_c_basic_dtype_list_len.py +0 -0
- {format_input-0.3.0 → format_input-0.4.0}/tests/test_d_basic_dtype_greedy.py +0 -0
- {format_input-0.3.0 → format_input-0.4.0}/tests/test_e_named_dtype.py +0 -0
- {format_input-0.3.0 → format_input-0.4.0}/tests/test_f_extra.py +0 -0
- {format_input-0.3.0 → format_input-0.4.0}/tests/test_g_groups.py +0 -0
- {format_input-0.3.0 → format_input-0.4.0}/tests/test_mix_abcd.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: format-input
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: A Python library for dynamically formatting and processing input
|
|
5
5
|
Author-email: GabbaTK <gabiteok+pypi@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -15,6 +15,7 @@ Requires-Dist: regex>=2026.1.15
|
|
|
15
15
|
Dynamic: license-file
|
|
16
16
|
|
|
17
17
|
# Format Input
|
|
18
|
+
 
|
|
18
19
|
|
|
19
20
|
`Format Input` is a small Python library for parsing console or text-stream input
|
|
20
21
|
according to a compact format specification. Instead of repeatedly calling
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
# Format Input
|
|
2
|
+
 
|
|
2
3
|
|
|
3
4
|
`Format Input` is a small Python library for parsing console or text-stream input
|
|
4
5
|
according to a compact format specification. Instead of repeatedly calling
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import typing
|
|
2
|
+
import regex as re
|
|
3
|
+
import sys
|
|
4
|
+
from .helpers import *
|
|
5
|
+
from .handlers import *
|
|
6
|
+
from collections import deque
|
|
7
|
+
|
|
8
|
+
NAME_PREFIX = r"(?<![a-z_:])[a-z_]+:"
|
|
9
|
+
#RE_NEW_LINE = re.compile(r";(?![^[\]]*\])")
|
|
10
|
+
RE_NEW_LINE = re.compile(r";(?![^\[\]]*(?:\[[^\[\]]*\][^\[\]]*)*\])")
|
|
11
|
+
RE_LINE_MATCH = re.compile(
|
|
12
|
+
rf"{NAME_PREFIX}(?(DEFINE)(?P<named_inner>(?:[^\[\]]+|\[(?&named_inner)\])*))\[(?P<named_group>(?&named_inner))\]" # g:[ ... ]
|
|
13
|
+
rf"|{NAME_PREFIX}(?(DEFINE)(?P<named_times_inner>(?:[^\[\]]+|\[(?&named_times_inner)\])*))(?P<number>int|N|[a-z_]+|\d+)\s*\*\s*\[(?P<named_times_group>(?&named_times_inner))\]" # g:N*[ ... ]
|
|
14
|
+
r"|(?(DEFINE)(?P<inner>(?:[^\[\]]+|\[(?&inner)\])*))(?<=\[)(?P<group>(?&inner))(?=\])" # [ ... ]
|
|
15
|
+
r"|(?(DEFINE)(?P<times_inner>(?:[^\[\]]+|\[(?×_inner)\])*))(?P<number>int|N|[a-z_]+|\d+)\s*\*\s*\[(?P<times_group>(?×_inner))\]" # N*[ ... ]
|
|
16
|
+
rf"|{NAME_PREFIX}(?P<named_extra>\?)(?![\w?])" # e:?
|
|
17
|
+
rf"|{NAME_PREFIX}(?P<named_type_greedy>str\*)(?![\w*])" # t:str*
|
|
18
|
+
rf"|{NAME_PREFIX}(?P<named_type_len_list>(?:int|float|str)\[[1-9]\d*\])(?![\w\[])" # n:int[5] f:float[7] t:str[1]
|
|
19
|
+
rf"|{NAME_PREFIX}(?P<named_type_list>(?:int|str|float)\[\])(?![\w\[])" # n:int[] f:float[] t:str[]
|
|
20
|
+
rf"|{NAME_PREFIX}(?P<named_type>(?:int|str|float))\b" # n:int f:float t:str
|
|
21
|
+
r"|(?<![\w:])(?P<extra>\?)(?![\w?])" # ?
|
|
22
|
+
r"|\b(?P<type_greedy>str\*)(?![\w*])" # str*
|
|
23
|
+
r"|\b(?P<type_len_list>(?:int|float|str)\[[1-9]\d*\])(?![\w\[])" # int[5] float[7] str[1]
|
|
24
|
+
r"|\b(?P<type_list>(?:int|str|float)\[\])(?![\w\[])" # int[] float[] str[]
|
|
25
|
+
r"|\b(?P<type>int|str|float)\b" # int float str
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def readline(stream: typing.TextIO) -> str:
|
|
30
|
+
line = stream.readline()
|
|
31
|
+
#if line == "": raise EOFError()
|
|
32
|
+
return line.rstrip("\r\n")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def format_input(spec: str, stream: typing.TextIO | None = None, named_variables: bool = False, shrink_output_in_times_group: bool = False, _input_queue: deque[str] | None = None, _times: str | int | None = None, _single_line_do_not_clear_queue: bool = False, _saved_vars: dict[str, typing.Any] | None = None) -> list[typing.Any] | dict[str, typing.Any]:
|
|
36
|
+
if stream is None: stream = sys.stdin
|
|
37
|
+
|
|
38
|
+
output = {} if named_variables else []
|
|
39
|
+
inp_queue: deque[str] = _input_queue if _input_queue is not None else deque()
|
|
40
|
+
variables = _saved_vars if _saved_vars is not None else {}
|
|
41
|
+
lines = RE_NEW_LINE.split(spec)
|
|
42
|
+
|
|
43
|
+
if _times:
|
|
44
|
+
if is_int(_times): num = int(_times)
|
|
45
|
+
else: num = handle_times(_times, inp_queue, lambda: readline(stream), variables)
|
|
46
|
+
|
|
47
|
+
t_out = []
|
|
48
|
+
|
|
49
|
+
if _times == "int": t_out.append(num)
|
|
50
|
+
for _ in range(num):
|
|
51
|
+
ret = format_input(spec, stream, named_variables=named_variables, _input_queue=inp_queue, _single_line_do_not_clear_queue=True, _saved_vars=variables)
|
|
52
|
+
# Instead of each iteration being in its own list if this flag is set allow them all to be in a single list if it only outputs a single element
|
|
53
|
+
if shrink_output_in_times_group and len(ret) == 1:
|
|
54
|
+
t_out.extend(ret)
|
|
55
|
+
else:
|
|
56
|
+
t_out.append(ret)
|
|
57
|
+
|
|
58
|
+
return t_out
|
|
59
|
+
|
|
60
|
+
for line in lines:
|
|
61
|
+
if len(inp_queue) == 0: # Always 0 unless _input_queue is provided then only during the first run is it not 0
|
|
62
|
+
inp = readline(stream)
|
|
63
|
+
inps = inp.split(" ")
|
|
64
|
+
inp_queue.extend(inps)
|
|
65
|
+
|
|
66
|
+
for m in RE_LINE_MATCH.finditer(line):
|
|
67
|
+
if "named" not in m.lastgroup and named_variables: raise TypeError("Cannot add an unnamed value when named_variables=True")
|
|
68
|
+
|
|
69
|
+
if "named" in m.lastgroup: var, dt = m.group().split(":", 1)
|
|
70
|
+
else: dt = m.group()
|
|
71
|
+
|
|
72
|
+
match (m.lastgroup):
|
|
73
|
+
case "times_group": ret = format_input(m.group("times_group"), stream, named_variables=named_variables, shrink_output_in_times_group=shrink_output_in_times_group, _input_queue=inp_queue, _times=m.group("number"), _saved_vars=variables)
|
|
74
|
+
case "named_group": ret = format_input(dt[1:-1], stream, named_variables=named_variables, _input_queue=inp_queue, _single_line_do_not_clear_queue=True, _saved_vars=variables) # Regex currently also includes the brackets in the match which are excess, so remove them here [1:-1] or fix the regex
|
|
75
|
+
case "group": ret = format_input(dt, stream, named_variables=named_variables, _input_queue=inp_queue, _single_line_do_not_clear_queue=True, _saved_vars=variables)
|
|
76
|
+
case "named_extra": ret = handle_extra(inp_queue)
|
|
77
|
+
case "extra": ret = handle_extra(inp_queue)
|
|
78
|
+
case "named_type_greedy": ret = handle_type_greedy(inp_queue)
|
|
79
|
+
case "type_greedy": ret = handle_type_greedy(inp_queue)
|
|
80
|
+
case "named_type_len_list": ret = handle_type_list(dt, inp_queue, max_size=int(m.group().split("[", 1)[1][:-1]))
|
|
81
|
+
case "type_len_list": ret = handle_type_list(dt, inp_queue, max_size=int(m.group().split("[", 1)[1][:-1]))
|
|
82
|
+
case "named_type_list": ret = handle_type_list(dt, inp_queue, max_size=-1)
|
|
83
|
+
case "type_list": ret = handle_type_list(dt, inp_queue, max_size=-1)
|
|
84
|
+
case "named_type": ret = handle_type(dt, inp_queue)
|
|
85
|
+
case "type": ret = handle_type(dt, inp_queue)
|
|
86
|
+
|
|
87
|
+
if "named" in m.lastgroup:
|
|
88
|
+
variables[var] = ret
|
|
89
|
+
ret = {var: ret}
|
|
90
|
+
|
|
91
|
+
if isinstance(ret, dict) and not named_variables:
|
|
92
|
+
output.extend([ v for v in ret.values() ])
|
|
93
|
+
elif isinstance(ret, dict) and named_variables:
|
|
94
|
+
output.update(ret)
|
|
95
|
+
elif not isinstance(ret, dict) and not named_variables and "times" in m.lastgroup:
|
|
96
|
+
output.extend(ret) # Unpack list to not nest lists when doing times group
|
|
97
|
+
elif not isinstance(ret, dict) and not named_variables:
|
|
98
|
+
output.append(ret)
|
|
99
|
+
else:
|
|
100
|
+
raise Exception("Unexpected error.")
|
|
101
|
+
|
|
102
|
+
# If there is a single line in the spec and the flag is set do not clear the queue
|
|
103
|
+
# Appped a ; to the end to manually clear the queue if you know there will be trailing data, otherwise do not
|
|
104
|
+
if not (_single_line_do_not_clear_queue and len(lines) == 1) and line.strip() != "": inp_queue.clear()
|
|
105
|
+
|
|
106
|
+
return output
|
|
@@ -1,6 +1,22 @@
|
|
|
1
|
+
import typing
|
|
1
2
|
from collections import deque
|
|
2
3
|
from .helpers import *
|
|
3
4
|
|
|
5
|
+
def handle_times(d: str, q: deque[str], read_stream: typing.Callable, var: dict[typing.Any]) -> int:
|
|
6
|
+
match (d):
|
|
7
|
+
case "int" | "N":
|
|
8
|
+
ret = read_queue_or_stream(q, read_stream)
|
|
9
|
+
if not is_int(ret): raise ValueError(f"Expected an integer, got {ret}")
|
|
10
|
+
return int(ret)
|
|
11
|
+
case _:
|
|
12
|
+
try:
|
|
13
|
+
val = var[d]
|
|
14
|
+
if not is_int(val): raise ValueError(f"Expected an integer, got {val}")
|
|
15
|
+
return int(val)
|
|
16
|
+
except IndexError as exc:
|
|
17
|
+
raise IndexError(f"No value found for variable '{d}'") from exc
|
|
18
|
+
|
|
19
|
+
|
|
4
20
|
def handle_extra(q: deque[str]) -> str:
|
|
5
21
|
output = []
|
|
6
22
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: format-input
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: A Python library for dynamically formatting and processing input
|
|
5
5
|
Author-email: GabbaTK <gabiteok+pypi@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -15,6 +15,7 @@ Requires-Dist: regex>=2026.1.15
|
|
|
15
15
|
Dynamic: license-file
|
|
16
16
|
|
|
17
17
|
# Format Input
|
|
18
|
+
 
|
|
18
19
|
|
|
19
20
|
`Format Input` is a small Python library for parsing console or text-stream input
|
|
20
21
|
according to a compact format specification. Instead of repeatedly calling
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import unittest
|
|
3
|
+
|
|
4
|
+
from format_input import format_input
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class TestHTimesGroup(unittest.TestCase):
|
|
8
|
+
def test_simple1(self):
|
|
9
|
+
source = io.StringIO("Hello Python World")
|
|
10
|
+
self.assertEqual(format_input("3 * [ str ]", source), [["Hello"], ["Python"], ["World"]])
|
|
11
|
+
|
|
12
|
+
def test_simple2(self):
|
|
13
|
+
source = io.StringIO("10 20")
|
|
14
|
+
self.assertEqual(format_input("2 * [ int ]", source), [[10], [20]])
|
|
15
|
+
|
|
16
|
+
def test_simple3(self):
|
|
17
|
+
source = io.StringIO("1 apple 2 banana")
|
|
18
|
+
self.assertEqual(format_input("2 * [ int str ]", source), [[1, "apple"], [2, "banana"]])
|
|
19
|
+
|
|
20
|
+
def test_simple4(self):
|
|
21
|
+
source = io.StringIO("1 red blue 2 green yellow")
|
|
22
|
+
self.assertEqual(format_input("2 * [ int [ str str ] ]", source), [[1, ["red", "blue"]], [2, ["green", "yellow"]]])
|
|
23
|
+
|
|
24
|
+
def test_simple5(self):
|
|
25
|
+
source = io.StringIO("1 2 3 4")
|
|
26
|
+
self.assertEqual(format_input("2 * [ int[2] ]", source), [[[1, 2]], [[3, 4]]])
|
|
27
|
+
|
|
28
|
+
def test_simple6(self):
|
|
29
|
+
source = io.StringIO("2 10 20")
|
|
30
|
+
self.assertEqual(format_input("int * [ int ]", source), [2, [10], [20]])
|
|
31
|
+
|
|
32
|
+
def test_simple7(self):
|
|
33
|
+
source = io.StringIO("2 hello world")
|
|
34
|
+
self.assertEqual(format_input("N * [ str ]", source), [["hello"], ["world"]])
|
|
35
|
+
|
|
36
|
+
def test_simple8(self):
|
|
37
|
+
source = io.StringIO("3 one 1 two 2 three 3")
|
|
38
|
+
self.assertEqual(format_input("int * [ str int ]", source), [3, ["one", 1], ["two", 2], ["three", 3]])
|
|
39
|
+
|
|
40
|
+
def test_simple9(self):
|
|
41
|
+
source = io.StringIO("3 red blue green")
|
|
42
|
+
self.assertEqual(format_input("N * [ str ]", source), [["red"], ["blue"], ["green"]])
|
|
43
|
+
|
|
44
|
+
def test_simple10(self):
|
|
45
|
+
source = io.StringIO("2 10 20 30 40")
|
|
46
|
+
self.assertEqual(format_input("int * [ int[2] ]", source), [2, [[10, 20]], [[30, 40]]])
|
|
47
|
+
|
|
48
|
+
def test_simple11(self):
|
|
49
|
+
source = io.StringIO("2 3 4")
|
|
50
|
+
self.assertEqual(format_input("1*[n:int] n*[int]", source), [[2], [3], [4]])
|
|
51
|
+
|
|
52
|
+
def test_shrink1(self):
|
|
53
|
+
source = io.StringIO("2 10 20")
|
|
54
|
+
self.assertEqual(format_input("int * [ int ]", source, shrink_output_in_times_group=True), [2, 10, 20])
|
|
55
|
+
|
|
56
|
+
def test_shrink2(self):
|
|
57
|
+
source = io.StringIO("2 hello world")
|
|
58
|
+
self.assertEqual(format_input("N * [ str ]", source, shrink_output_in_times_group=True), ["hello", "world"])
|
|
59
|
+
|
|
60
|
+
def test_shrink3(self):
|
|
61
|
+
source = io.StringIO("2 10 red 20 blue")
|
|
62
|
+
self.assertEqual(format_input("int * [ int str ]", source, shrink_output_in_times_group=True), [2, [10, "red"], [20, "blue"]])
|
|
63
|
+
|
|
64
|
+
def test_newline1(self):
|
|
65
|
+
source = io.StringIO("1\napple\n2\nbanana")
|
|
66
|
+
self.assertEqual(format_input("2 * [ int ; str ]", source), [[1, "apple"], [2, "banana"]])
|
|
67
|
+
|
|
68
|
+
def test_newline2(self):
|
|
69
|
+
source = io.StringIO("2\n1\napple\n2\nbanana")
|
|
70
|
+
self.assertEqual(format_input("int * [ int ; str ]", source), [2, [1, "apple"], [2, "banana"]])
|
|
71
|
+
|
|
72
|
+
def test_newline3(self):
|
|
73
|
+
source = io.StringIO("2\napple\n1\nbanana\n2")
|
|
74
|
+
self.assertEqual(format_input("N * [ str ; int ]", source), [["apple", 1], ["banana", 2]])
|
|
75
|
+
|
|
76
|
+
def test_newline4(self):
|
|
77
|
+
source = io.StringIO("2\nalpha\n1\nbeta\n2")
|
|
78
|
+
self.assertEqual(format_input("int * [ str ; int ]", source), [2, ["alpha", 1], ["beta", 2]])
|
|
79
|
+
|
|
80
|
+
def test_newline5(self):
|
|
81
|
+
source = io.StringIO("3\nred\nblue\ngreen")
|
|
82
|
+
self.assertEqual(format_input("N * [ str ]", source), [["red"], ["blue"], ["green"]])
|
|
83
|
+
|
|
84
|
+
def test_newline6(self):
|
|
85
|
+
source = io.StringIO("3 one two three\nfour five six\nseven eight nine")
|
|
86
|
+
self.assertEqual(format_input("int * [ str str ; ]", source), [3, ["one", "two"], ["four", "five"], ["seven", "eight"]])
|
|
87
|
+
|
|
88
|
+
def test_var1(self):
|
|
89
|
+
source = io.StringIO("2\n10 20")
|
|
90
|
+
self.assertEqual(format_input("n:int ; n * [ int ]", source), [2, [10], [20]])
|
|
91
|
+
|
|
92
|
+
def test_var2(self):
|
|
93
|
+
source = io.StringIO("2\n1\napple\n2\nbanana")
|
|
94
|
+
self.assertEqual(format_input("n:int ; n * [ int ; str ]", source), [2, [1, "apple"], [2, "banana"]])
|
|
95
|
+
|
|
96
|
+
def test_nested1(self):
|
|
97
|
+
source = io.StringIO("2\n2\n10\n11\n1\n20")
|
|
98
|
+
self.assertEqual(format_input("int * [ int * [ int ] ]", source), [2, [2, [10], [11]], [1, [20]]])
|
|
99
|
+
|
|
100
|
+
def test_nested2(self):
|
|
101
|
+
source = io.StringIO("2\n2\n10\n11\n1\n20")
|
|
102
|
+
self.assertEqual(format_input("int * [ N * [ int ] ]", source), [2, [[10], [11]], [[20]]])
|
|
103
|
+
|
|
104
|
+
def test_nested3(self):
|
|
105
|
+
source = io.StringIO("2\n10\n11\n20\n21")
|
|
106
|
+
self.assertEqual(format_input("n:int ; 2 * [ n * [ int ] ]", source), [2, [[10], [11]], [[20], [21]]])
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
if __name__ == "__main__":
|
|
110
|
+
unittest.main()
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import typing
|
|
2
|
-
import regex as re
|
|
3
|
-
import sys
|
|
4
|
-
from .helpers import *
|
|
5
|
-
from .handlers import *
|
|
6
|
-
from collections import deque
|
|
7
|
-
|
|
8
|
-
NAME_PREFIX = r"(?<![a-z_:])[a-z_]+:"
|
|
9
|
-
#RE_NEW_LINE = re.compile(r";(?![^[\]]*\])")
|
|
10
|
-
RE_NEW_LINE = re.compile(r";(?![^\[\]]*(?:\[[^\[\]]*\][^\[\]]*)*\])")
|
|
11
|
-
RE_LINE_MATCH = re.compile(
|
|
12
|
-
rf"{NAME_PREFIX}(?(DEFINE)(?P<named_inner>(?:[^\[\]]+|\[(?&named_inner)\])*))\[(?P<named_group>(?&named_inner))\]" # g:[ ... ]
|
|
13
|
-
r"|(?(DEFINE)(?P<inner>(?:[^\[\]]+|\[(?&inner)\])*))(?<=\[)(?P<group>(?&inner))(?=\])" # [ ... ]
|
|
14
|
-
rf"|{NAME_PREFIX}(?P<named_extra>\?)(?![\w?])" # e:?
|
|
15
|
-
rf"|{NAME_PREFIX}(?P<named_type_greedy>str\*)(?![\w*])" # t:str*
|
|
16
|
-
rf"|{NAME_PREFIX}(?P<named_type_len_list>(?:int|float|str)\[[1-9]\d*\])(?![\w\[])" # n:int[5] f:float[7] t:str[1]
|
|
17
|
-
rf"|{NAME_PREFIX}(?P<named_type_list>(?:int|str|float)\[\])(?![\w\[])" # n:int[] f:float[] t:str[]
|
|
18
|
-
rf"|{NAME_PREFIX}(?P<named_type>(?:int|str|float))\b" # n:int f:float t:str
|
|
19
|
-
r"|(?<![\w:])(?P<extra>\?)(?![\w?])" # ?
|
|
20
|
-
r"|\b(?P<type_greedy>str\*)(?![\w*])" # str*
|
|
21
|
-
r"|\b(?P<type_len_list>(?:int|float|str)\[[1-9]\d*\])(?![\w\[])" # int[5] float[7] str[1]
|
|
22
|
-
r"|\b(?P<type_list>(?:int|str|float)\[\])(?![\w\[])" # int[] float[] str[]
|
|
23
|
-
r"|\b(?P<type>int|str|float)\b" # int float str
|
|
24
|
-
)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
def readline(stream: typing.TextIO) -> str:
|
|
28
|
-
line = stream.readline()
|
|
29
|
-
#if line == "": raise EOFError()
|
|
30
|
-
return line.rstrip("\r\n")
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
def format_input(spec: str, stream: typing.TextIO | None = None, named_variables: bool = False, _input_queue: deque[str] | None = None) -> list[typing.Any] | dict[str, typing.Any]:
|
|
34
|
-
if stream is None: stream = sys.stdin
|
|
35
|
-
|
|
36
|
-
output = {} if named_variables else []
|
|
37
|
-
inp_queue: deque[str] = _input_queue if _input_queue is not None else deque()
|
|
38
|
-
lines = RE_NEW_LINE.split(spec)
|
|
39
|
-
|
|
40
|
-
for line in lines:
|
|
41
|
-
if len(inp_queue) == 0: # Always 0 unless _input_queue is provided then only during the first run is it not 0
|
|
42
|
-
inp = readline(stream)
|
|
43
|
-
inps = inp.split(" ")
|
|
44
|
-
inp_queue.extend(inps)
|
|
45
|
-
|
|
46
|
-
for m in RE_LINE_MATCH.finditer(line):
|
|
47
|
-
if "named" not in m.lastgroup and named_variables: raise TypeError("Cannot add an unnamed value when named_variables=True")
|
|
48
|
-
|
|
49
|
-
if "named" in m.lastgroup: var, dt = m.group().split(":", 1)
|
|
50
|
-
else: dt = m.group()
|
|
51
|
-
|
|
52
|
-
match (m.lastgroup):
|
|
53
|
-
case "named_extra": ret = handle_extra(inp_queue)
|
|
54
|
-
case "extra": ret = handle_extra(inp_queue)
|
|
55
|
-
case "named_type_greedy": ret = handle_type_greedy(inp_queue)
|
|
56
|
-
case "type_greedy": ret = handle_type_greedy(inp_queue)
|
|
57
|
-
case "named_type_len_list": ret = handle_type_list(dt, inp_queue, max_size=int(m.group().split("[", 1)[1][:-1]))
|
|
58
|
-
case "type_len_list": ret = handle_type_list(dt, inp_queue, max_size=int(m.group().split("[", 1)[1][:-1]))
|
|
59
|
-
case "named_type_list": ret = handle_type_list(dt, inp_queue, max_size=-1)
|
|
60
|
-
case "type_list": ret = handle_type_list(dt, inp_queue, max_size=-1)
|
|
61
|
-
case "named_type": ret = handle_type(dt, inp_queue)
|
|
62
|
-
case "type": ret = handle_type(dt, inp_queue)
|
|
63
|
-
case "named_group": ret = format_input(dt[1:-1], stream, named_variables=named_variables, _input_queue=inp_queue) # Regex currently also includes the brackets in the match which are excess, so remove them here or fix the regex
|
|
64
|
-
case "group": ret = format_input(dt, stream, named_variables=named_variables, _input_queue=inp_queue)
|
|
65
|
-
|
|
66
|
-
if "named" in m.lastgroup: ret = {var: ret}
|
|
67
|
-
|
|
68
|
-
if isinstance(ret, dict) and not named_variables:
|
|
69
|
-
output.extend([ v for v in ret.values() ])
|
|
70
|
-
elif isinstance(ret, dict) and named_variables:
|
|
71
|
-
output.update(ret)
|
|
72
|
-
elif not isinstance(ret, dict) and not named_variables:
|
|
73
|
-
output.append(ret)
|
|
74
|
-
else:
|
|
75
|
-
raise Exception("Unexpected error.")
|
|
76
|
-
|
|
77
|
-
inp_queue.clear()
|
|
78
|
-
|
|
79
|
-
return output
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|