format-input 0.1.0__tar.gz → 0.3.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.1.0/src/format_input.egg-info → format_input-0.3.0}/PKG-INFO +16 -4
- format_input-0.1.0/README.md → format_input-0.3.0/README.MD +13 -2
- {format_input-0.1.0 → format_input-0.3.0}/pyproject.toml +6 -3
- format_input-0.3.0/src/format_input/format_input.py +79 -0
- format_input-0.3.0/src/format_input/handlers.py +84 -0
- {format_input-0.1.0 → format_input-0.3.0}/src/format_input/helpers.py +4 -0
- {format_input-0.1.0 → format_input-0.3.0/src/format_input.egg-info}/PKG-INFO +16 -4
- {format_input-0.1.0 → format_input-0.3.0}/src/format_input.egg-info/SOURCES.txt +5 -1
- format_input-0.3.0/src/format_input.egg-info/requires.txt +1 -0
- format_input-0.3.0/tests/test_000_edgecase.py +127 -0
- {format_input-0.1.0 → format_input-0.3.0}/tests/test_a_basic_dtype.py +2 -1
- format_input-0.3.0/tests/test_f_extra.py +42 -0
- format_input-0.3.0/tests/test_g_groups.py +170 -0
- format_input-0.1.0/src/format_input/format_input.py +0 -62
- format_input-0.1.0/src/format_input/handlers.py +0 -146
- {format_input-0.1.0 → format_input-0.3.0}/LICENSE +0 -0
- {format_input-0.1.0 → format_input-0.3.0}/setup.cfg +0 -0
- {format_input-0.1.0 → format_input-0.3.0}/src/format_input/__init__.py +0 -0
- {format_input-0.1.0 → format_input-0.3.0}/src/format_input.egg-info/dependency_links.txt +0 -0
- {format_input-0.1.0 → format_input-0.3.0}/src/format_input.egg-info/top_level.txt +0 -0
- {format_input-0.1.0 → format_input-0.3.0}/tests/test_b_basic_dtype_list.py +0 -0
- {format_input-0.1.0 → format_input-0.3.0}/tests/test_c_basic_dtype_list_len.py +0 -0
- {format_input-0.1.0 → format_input-0.3.0}/tests/test_d_basic_dtype_greedy.py +0 -0
- {format_input-0.1.0 → format_input-0.3.0}/tests/test_e_named_dtype.py +0 -0
- {format_input-0.1.0 → format_input-0.3.0}/tests/test_mix_abcd.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
|
-
Name:
|
|
3
|
-
Version: 0.
|
|
2
|
+
Name: format-input
|
|
3
|
+
Version: 0.3.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
|
|
@@ -11,6 +11,7 @@ Classifier: Operating System :: OS Independent
|
|
|
11
11
|
Requires-Python: >=3.10
|
|
12
12
|
Description-Content-Type: text/markdown
|
|
13
13
|
License-File: LICENSE
|
|
14
|
+
Requires-Dist: regex>=2026.1.15
|
|
14
15
|
Dynamic: license-file
|
|
15
16
|
|
|
16
17
|
# Format Input
|
|
@@ -48,10 +49,18 @@ See [USAGE.MD](./USAGE.MD) for all supported types and format specifications.
|
|
|
48
49
|
## Requirements
|
|
49
50
|
|
|
50
51
|
- Python 3.10 or newer
|
|
51
|
-
-
|
|
52
|
+
- `regex` 2026.1.15 or newer
|
|
52
53
|
|
|
53
54
|
## Installation
|
|
54
55
|
|
|
56
|
+
Install the published package from PyPI:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pip install format-input
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Or clone the repository and install it manually:
|
|
63
|
+
|
|
55
64
|
Clone the repository:
|
|
56
65
|
|
|
57
66
|
```bash
|
|
@@ -59,7 +68,10 @@ git clone https://github.com/GabbaTK/Format-Input.git
|
|
|
59
68
|
cd Format-Input
|
|
60
69
|
```
|
|
61
70
|
|
|
62
|
-
The package is stored in `src/`.
|
|
71
|
+
The package is stored in `src/`.
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
To run the test suite from the repository root:
|
|
63
75
|
|
|
64
76
|
```bash
|
|
65
77
|
PYTHONPATH=src python -m unittest discover -s tests -v
|
|
@@ -33,10 +33,18 @@ See [USAGE.MD](./USAGE.MD) for all supported types and format specifications.
|
|
|
33
33
|
## Requirements
|
|
34
34
|
|
|
35
35
|
- Python 3.10 or newer
|
|
36
|
-
-
|
|
36
|
+
- `regex` 2026.1.15 or newer
|
|
37
37
|
|
|
38
38
|
## Installation
|
|
39
39
|
|
|
40
|
+
Install the published package from PyPI:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install format-input
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Or clone the repository and install it manually:
|
|
47
|
+
|
|
40
48
|
Clone the repository:
|
|
41
49
|
|
|
42
50
|
```bash
|
|
@@ -44,7 +52,10 @@ git clone https://github.com/GabbaTK/Format-Input.git
|
|
|
44
52
|
cd Format-Input
|
|
45
53
|
```
|
|
46
54
|
|
|
47
|
-
The package is stored in `src/`.
|
|
55
|
+
The package is stored in `src/`.
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
To run the test suite from the repository root:
|
|
48
59
|
|
|
49
60
|
```bash
|
|
50
61
|
PYTHONPATH=src python -m unittest discover -s tests -v
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
[project]
|
|
2
|
-
name = "
|
|
3
|
-
version = "0.
|
|
2
|
+
name = "format-input"
|
|
3
|
+
version = "0.3.0"
|
|
4
4
|
authors = [
|
|
5
5
|
{ name="GabbaTK", email="gabiteok+pypi@gmail.com" },
|
|
6
6
|
]
|
|
7
7
|
description = "A Python library for dynamically formatting and processing input"
|
|
8
|
-
readme = "README.
|
|
8
|
+
readme = "README.MD"
|
|
9
9
|
requires-python = ">=3.10"
|
|
10
|
+
dependencies = [
|
|
11
|
+
"regex>=2026.1.15",
|
|
12
|
+
]
|
|
10
13
|
classifiers = [
|
|
11
14
|
"Programming Language :: Python :: 3",
|
|
12
15
|
"Operating System :: OS Independent",
|
|
@@ -0,0 +1,79 @@
|
|
|
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
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
from collections import deque
|
|
2
|
+
from .helpers import *
|
|
3
|
+
|
|
4
|
+
def handle_extra(q: deque[str]) -> str:
|
|
5
|
+
output = []
|
|
6
|
+
|
|
7
|
+
while len(q) > 0:
|
|
8
|
+
output.append(q.popleft())
|
|
9
|
+
|
|
10
|
+
return " ".join(output)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def handle_type_greedy(q: deque[str]) -> str:
|
|
14
|
+
output = []
|
|
15
|
+
|
|
16
|
+
while len(q) > 0:
|
|
17
|
+
inp = q.popleft()
|
|
18
|
+
if is_str(inp):
|
|
19
|
+
output.append(inp)
|
|
20
|
+
else:
|
|
21
|
+
q.appendleft(inp) # Not a string, return it back to the queue and return
|
|
22
|
+
break
|
|
23
|
+
|
|
24
|
+
return " ".join(output)
|
|
25
|
+
|
|
26
|
+
def handle_type_list(dt: str, q: deque[str], max_size = -1) -> list[str] | list[int] | list[float]:
|
|
27
|
+
size_left = max_size
|
|
28
|
+
output = []
|
|
29
|
+
|
|
30
|
+
match (dt):
|
|
31
|
+
case dt if dt.startswith("str"):
|
|
32
|
+
while len(q) > 0 and size_left != 0:
|
|
33
|
+
inp = q.popleft()
|
|
34
|
+
if is_str(inp):
|
|
35
|
+
output.append(inp)
|
|
36
|
+
size_left -= 1
|
|
37
|
+
else:
|
|
38
|
+
q.appendleft(inp) # Not a string, return it back to the queue and return
|
|
39
|
+
break
|
|
40
|
+
|
|
41
|
+
return output
|
|
42
|
+
|
|
43
|
+
case dt if dt.startswith("int"):
|
|
44
|
+
while len(q) > 0 and size_left != 0:
|
|
45
|
+
inp = q.popleft()
|
|
46
|
+
if is_int(inp):
|
|
47
|
+
output.append(int(inp))
|
|
48
|
+
size_left -= 1
|
|
49
|
+
else:
|
|
50
|
+
q.appendleft(inp) # Not an int, return it back to the queue and return
|
|
51
|
+
break
|
|
52
|
+
|
|
53
|
+
return output
|
|
54
|
+
|
|
55
|
+
case dt if dt.startswith("float"):
|
|
56
|
+
while len(q) > 0 and size_left != 0:
|
|
57
|
+
inp = q.popleft()
|
|
58
|
+
if is_float(inp):
|
|
59
|
+
output.append(float(inp))
|
|
60
|
+
size_left -= 1
|
|
61
|
+
else:
|
|
62
|
+
q.appendleft(inp) # Not a float, return it back to the queue and return
|
|
63
|
+
break
|
|
64
|
+
|
|
65
|
+
return output
|
|
66
|
+
|
|
67
|
+
def handle_type(dt: str, q: deque[str]) -> str | int | float | None:
|
|
68
|
+
inp = q.popleft()
|
|
69
|
+
|
|
70
|
+
match (dt):
|
|
71
|
+
case "str": return str(inp)
|
|
72
|
+
case "int":
|
|
73
|
+
not_empty(inp)
|
|
74
|
+
try:
|
|
75
|
+
return int(inp)
|
|
76
|
+
except ValueError:
|
|
77
|
+
return None
|
|
78
|
+
case "float":
|
|
79
|
+
not_empty(inp)
|
|
80
|
+
try:
|
|
81
|
+
if not is_float(inp): return None # Nan or inf check
|
|
82
|
+
return float(inp)
|
|
83
|
+
except ValueError:
|
|
84
|
+
return None
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
|
-
Name:
|
|
3
|
-
Version: 0.
|
|
2
|
+
Name: format-input
|
|
3
|
+
Version: 0.3.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
|
|
@@ -11,6 +11,7 @@ Classifier: Operating System :: OS Independent
|
|
|
11
11
|
Requires-Python: >=3.10
|
|
12
12
|
Description-Content-Type: text/markdown
|
|
13
13
|
License-File: LICENSE
|
|
14
|
+
Requires-Dist: regex>=2026.1.15
|
|
14
15
|
Dynamic: license-file
|
|
15
16
|
|
|
16
17
|
# Format Input
|
|
@@ -48,10 +49,18 @@ See [USAGE.MD](./USAGE.MD) for all supported types and format specifications.
|
|
|
48
49
|
## Requirements
|
|
49
50
|
|
|
50
51
|
- Python 3.10 or newer
|
|
51
|
-
-
|
|
52
|
+
- `regex` 2026.1.15 or newer
|
|
52
53
|
|
|
53
54
|
## Installation
|
|
54
55
|
|
|
56
|
+
Install the published package from PyPI:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pip install format-input
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Or clone the repository and install it manually:
|
|
63
|
+
|
|
55
64
|
Clone the repository:
|
|
56
65
|
|
|
57
66
|
```bash
|
|
@@ -59,7 +68,10 @@ git clone https://github.com/GabbaTK/Format-Input.git
|
|
|
59
68
|
cd Format-Input
|
|
60
69
|
```
|
|
61
70
|
|
|
62
|
-
The package is stored in `src/`.
|
|
71
|
+
The package is stored in `src/`.
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
To run the test suite from the repository root:
|
|
63
75
|
|
|
64
76
|
```bash
|
|
65
77
|
PYTHONPATH=src python -m unittest discover -s tests -v
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
LICENSE
|
|
2
|
-
README.
|
|
2
|
+
README.MD
|
|
3
3
|
pyproject.toml
|
|
4
4
|
src/format_input/__init__.py
|
|
5
5
|
src/format_input/format_input.py
|
|
@@ -8,10 +8,14 @@ src/format_input/helpers.py
|
|
|
8
8
|
src/format_input.egg-info/PKG-INFO
|
|
9
9
|
src/format_input.egg-info/SOURCES.txt
|
|
10
10
|
src/format_input.egg-info/dependency_links.txt
|
|
11
|
+
src/format_input.egg-info/requires.txt
|
|
11
12
|
src/format_input.egg-info/top_level.txt
|
|
13
|
+
tests/test_000_edgecase.py
|
|
12
14
|
tests/test_a_basic_dtype.py
|
|
13
15
|
tests/test_b_basic_dtype_list.py
|
|
14
16
|
tests/test_c_basic_dtype_list_len.py
|
|
15
17
|
tests/test_d_basic_dtype_greedy.py
|
|
16
18
|
tests/test_e_named_dtype.py
|
|
19
|
+
tests/test_f_extra.py
|
|
20
|
+
tests/test_g_groups.py
|
|
17
21
|
tests/test_mix_abcd.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
regex>=2026.1.15
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import unittest
|
|
3
|
+
|
|
4
|
+
from format_input import format_input
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Test000Edgecase(unittest.TestCase):
|
|
8
|
+
def test1(self):
|
|
9
|
+
source = io.StringIO("")
|
|
10
|
+
with self.assertRaises(EOFError):
|
|
11
|
+
format_input("int ; float[] ; int", source)
|
|
12
|
+
|
|
13
|
+
def test2(self):
|
|
14
|
+
source = io.StringIO("")
|
|
15
|
+
with self.assertRaises(EOFError):
|
|
16
|
+
format_input("int", source)
|
|
17
|
+
|
|
18
|
+
def test3(self):
|
|
19
|
+
source = io.StringIO("42")
|
|
20
|
+
with self.assertRaises(IndexError):
|
|
21
|
+
format_input("int int", source)
|
|
22
|
+
|
|
23
|
+
def test4(self):
|
|
24
|
+
source = io.StringIO("\n\n")
|
|
25
|
+
with self.assertRaises(EOFError):
|
|
26
|
+
format_input("int ; float ; str", source)
|
|
27
|
+
|
|
28
|
+
def test5(self):
|
|
29
|
+
source = io.StringIO("not-an-int not-a-float")
|
|
30
|
+
self.assertEqual(format_input("int float", source), [None, None])
|
|
31
|
+
|
|
32
|
+
def test6(self):
|
|
33
|
+
source = io.StringIO("nan inf -inf")
|
|
34
|
+
self.assertEqual(format_input("float float float", source), [None, None, None])
|
|
35
|
+
|
|
36
|
+
def test7(self):
|
|
37
|
+
source = io.StringIO("1 2 word 3")
|
|
38
|
+
self.assertEqual(format_input("int[4] str[2]", source), [[1, 2], ["word"]])
|
|
39
|
+
|
|
40
|
+
def test8(self):
|
|
41
|
+
source = io.StringIO("42\n")
|
|
42
|
+
self.assertEqual(format_input("str* int", source), ["", 42])
|
|
43
|
+
|
|
44
|
+
def test9(self):
|
|
45
|
+
source = io.StringIO("7")
|
|
46
|
+
self.assertEqual(format_input("tail:str* count:int", source, named_variables=True), {"tail": "", "count": 7})
|
|
47
|
+
|
|
48
|
+
def test10(self):
|
|
49
|
+
source = io.StringIO("12 13.5")
|
|
50
|
+
with self.assertRaises(TypeError):
|
|
51
|
+
format_input("n:int float", source, named_variables=True)
|
|
52
|
+
|
|
53
|
+
def test11(self):
|
|
54
|
+
source = io.StringIO("1 unused tokens")
|
|
55
|
+
self.assertEqual(format_input("int", source), [1])
|
|
56
|
+
|
|
57
|
+
def test12(self):
|
|
58
|
+
source = io.StringIO("10 20\r\nred blue\r\n")
|
|
59
|
+
self.assertEqual(format_input("int[2] ; str[2]", source), [[10, 20], ["red", "blue"]])
|
|
60
|
+
|
|
61
|
+
def test13(self):
|
|
62
|
+
source = io.StringIO("42")
|
|
63
|
+
self.assertEqual(format_input("int ?", source), [42, ""])
|
|
64
|
+
|
|
65
|
+
def test14(self):
|
|
66
|
+
source = io.StringIO("\n")
|
|
67
|
+
self.assertEqual(format_input("?", source), [""])
|
|
68
|
+
|
|
69
|
+
def test15(self):
|
|
70
|
+
source = io.StringIO("42 left extra ")
|
|
71
|
+
self.assertEqual(format_input("int ?", source), [42, "left extra "])
|
|
72
|
+
|
|
73
|
+
def test16(self):
|
|
74
|
+
source = io.StringIO("42")
|
|
75
|
+
self.assertEqual(format_input("count:int extra:?", source, named_variables=True), {"count": 42, "extra": ""})
|
|
76
|
+
|
|
77
|
+
def test17(self):
|
|
78
|
+
source = io.StringIO("")
|
|
79
|
+
with self.assertRaises(EOFError):
|
|
80
|
+
format_input("[int]", source)
|
|
81
|
+
|
|
82
|
+
def test18(self):
|
|
83
|
+
source = io.StringIO("42")
|
|
84
|
+
with self.assertRaises(IndexError):
|
|
85
|
+
format_input("[int int]", source)
|
|
86
|
+
|
|
87
|
+
def test19(self):
|
|
88
|
+
source = io.StringIO("not-an-int")
|
|
89
|
+
self.assertEqual(format_input("[int]", source), [[None]])
|
|
90
|
+
|
|
91
|
+
def test20(self):
|
|
92
|
+
source = io.StringIO("nan")
|
|
93
|
+
self.assertEqual(format_input("[float]", source), [[None]])
|
|
94
|
+
|
|
95
|
+
def test21(self):
|
|
96
|
+
source = io.StringIO("1 2")
|
|
97
|
+
self.assertEqual(format_input("[int[3]]", source), [[[1, 2]]])
|
|
98
|
+
|
|
99
|
+
def test22(self):
|
|
100
|
+
source = io.StringIO("1 word 2")
|
|
101
|
+
self.assertEqual(format_input("[int[]]", source), [[[1]]])
|
|
102
|
+
|
|
103
|
+
def test23(self):
|
|
104
|
+
source = io.StringIO("42 extra tokens")
|
|
105
|
+
self.assertEqual(format_input("[int]", source), [[42]])
|
|
106
|
+
|
|
107
|
+
def test24(self):
|
|
108
|
+
source = io.StringIO("\n")
|
|
109
|
+
self.assertEqual(format_input("[str]", source), [[""]])
|
|
110
|
+
|
|
111
|
+
def test25(self):
|
|
112
|
+
source = io.StringIO("42\r\nhello\r\n")
|
|
113
|
+
self.assertEqual(format_input("[int ; str]", source), [[42, "hello"]])
|
|
114
|
+
|
|
115
|
+
def test26(self):
|
|
116
|
+
source = io.StringIO("42")
|
|
117
|
+
with self.assertRaises(TypeError):
|
|
118
|
+
format_input("[int]", source, named_variables=True)
|
|
119
|
+
|
|
120
|
+
def test27(self):
|
|
121
|
+
source = io.StringIO("42")
|
|
122
|
+
with self.assertRaises(TypeError):
|
|
123
|
+
format_input("[count:int]", source, named_variables=True)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
if __name__ == "__main__":
|
|
127
|
+
unittest.main()
|
|
@@ -155,7 +155,8 @@ class TestABasicDtype(unittest.TestCase):
|
|
|
155
155
|
|
|
156
156
|
def test_misc1(self):
|
|
157
157
|
source = io.StringIO("3\n\n7")
|
|
158
|
-
self.
|
|
158
|
+
with self.assertRaises(EOFError):
|
|
159
|
+
format_input("int ; int ; int", source)
|
|
159
160
|
|
|
160
161
|
|
|
161
162
|
if __name__ == "__main__":
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import unittest
|
|
3
|
+
|
|
4
|
+
from format_input import format_input
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class TestFExtra(unittest.TestCase):
|
|
8
|
+
def test1(self):
|
|
9
|
+
source = io.StringIO("hello")
|
|
10
|
+
self.assertEqual(format_input("?", source), ["hello"])
|
|
11
|
+
|
|
12
|
+
def test2(self):
|
|
13
|
+
source = io.StringIO("hello extra words")
|
|
14
|
+
self.assertEqual(format_input("?", source), ["hello extra words"])
|
|
15
|
+
|
|
16
|
+
def test3(self):
|
|
17
|
+
source = io.StringIO("42 extra words")
|
|
18
|
+
self.assertEqual(format_input("int ?", source), [42, "extra words"])
|
|
19
|
+
|
|
20
|
+
def test4(self):
|
|
21
|
+
source = io.StringIO("42 3.5 hello extra words")
|
|
22
|
+
self.assertEqual(format_input("int float str ?", source), [42, 3.5, "hello", "extra words"])
|
|
23
|
+
|
|
24
|
+
def test5(self):
|
|
25
|
+
source = io.StringIO("hello extra words")
|
|
26
|
+
self.assertEqual(format_input("extra:?", source), ["hello extra words"])
|
|
27
|
+
|
|
28
|
+
def test6(self):
|
|
29
|
+
source = io.StringIO("hello extra words")
|
|
30
|
+
self.assertEqual(format_input("extra:?", source, named_variables=True), {"extra": "hello extra words"})
|
|
31
|
+
|
|
32
|
+
def test7(self):
|
|
33
|
+
source = io.StringIO("42 hello extra words")
|
|
34
|
+
self.assertEqual(format_input("count:int extra:?", source, named_variables=True), {"count": 42, "extra": "hello extra words"})
|
|
35
|
+
|
|
36
|
+
def test8(self):
|
|
37
|
+
source = io.StringIO("42 3.5 hello extra words")
|
|
38
|
+
self.assertEqual(format_input("count:int ratio:float greeting:str extra:?", source, named_variables=True), {"count": 42, "ratio": 3.5, "greeting": "hello", "extra": "extra words"})
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
if __name__ == "__main__":
|
|
42
|
+
unittest.main()
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import unittest
|
|
3
|
+
|
|
4
|
+
from format_input import format_input
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class TestGGroups(unittest.TestCase):
|
|
8
|
+
def test_simple1(self):
|
|
9
|
+
source = io.StringIO("Hello World")
|
|
10
|
+
self.assertEqual(format_input("[ str str ]", source), [["Hello", "World"]])
|
|
11
|
+
|
|
12
|
+
def test_simple2(self):
|
|
13
|
+
source = io.StringIO("42")
|
|
14
|
+
self.assertEqual(format_input("[int]", source), [[42]])
|
|
15
|
+
|
|
16
|
+
def test_simple3(self):
|
|
17
|
+
source = io.StringIO("3.14")
|
|
18
|
+
self.assertEqual(format_input("[float]", source), [[3.14]])
|
|
19
|
+
|
|
20
|
+
def test_simple4(self):
|
|
21
|
+
source = io.StringIO("hello")
|
|
22
|
+
self.assertEqual(format_input("[str]", source), [["hello"]])
|
|
23
|
+
|
|
24
|
+
def test_simple5(self):
|
|
25
|
+
source = io.StringIO("42 3.14 hello")
|
|
26
|
+
self.assertEqual(format_input("[int float str]", source), [[42, 3.14, "hello"]])
|
|
27
|
+
|
|
28
|
+
def test_simple6(self):
|
|
29
|
+
source = io.StringIO("10 20")
|
|
30
|
+
self.assertEqual(format_input("[int int]", source), [[10, 20]])
|
|
31
|
+
|
|
32
|
+
def test_simple7(self):
|
|
33
|
+
source = io.StringIO("1 2 3")
|
|
34
|
+
self.assertEqual(format_input("[int[2]]", source), [[[1, 2]]])
|
|
35
|
+
|
|
36
|
+
def test_simple8(self):
|
|
37
|
+
source = io.StringIO("1.5 2.5")
|
|
38
|
+
self.assertEqual(format_input("[float[2]]", source), [[[1.5, 2.5]]])
|
|
39
|
+
|
|
40
|
+
def test_simple9(self):
|
|
41
|
+
source = io.StringIO("red blue")
|
|
42
|
+
self.assertEqual(format_input("[str[2]]", source), [[["red", "blue"]]])
|
|
43
|
+
|
|
44
|
+
def test_simple10(self):
|
|
45
|
+
source = io.StringIO("1 2 3")
|
|
46
|
+
self.assertEqual(format_input("[int[]]", source), [[[1, 2, 3]]])
|
|
47
|
+
|
|
48
|
+
def test_simple11(self):
|
|
49
|
+
source = io.StringIO("1.25 2.5")
|
|
50
|
+
self.assertEqual(format_input("[float[]]", source), [[[1.25, 2.5]]])
|
|
51
|
+
|
|
52
|
+
def test_simple12(self):
|
|
53
|
+
source = io.StringIO("1 2 1.5 2.5")
|
|
54
|
+
self.assertEqual(format_input("[int[2] float[2]]", source), [[[1, 2], [1.5, 2.5]]])
|
|
55
|
+
|
|
56
|
+
def test_simple13(self):
|
|
57
|
+
source = io.StringIO("1 2 3.5 4.5")
|
|
58
|
+
self.assertEqual(format_input("[int[] float[]]", source), [[[1, 2], [3.5, 4.5]]])
|
|
59
|
+
|
|
60
|
+
def test_simple14(self):
|
|
61
|
+
source = io.StringIO("hello 42")
|
|
62
|
+
self.assertEqual(format_input("[hello:str* int]", source), [["hello", 42]])
|
|
63
|
+
|
|
64
|
+
def test_simple15(self):
|
|
65
|
+
source = io.StringIO("42 hello")
|
|
66
|
+
self.assertEqual(format_input("[[int str]]", source), [[[42, "hello"]]])
|
|
67
|
+
|
|
68
|
+
def test_newline1(self):
|
|
69
|
+
source = io.StringIO("42\nhello")
|
|
70
|
+
self.assertEqual(format_input("[int ; str]", source), [[42, "hello"]])
|
|
71
|
+
|
|
72
|
+
def test_newline2(self):
|
|
73
|
+
source = io.StringIO("1 2\n1.5 2.5")
|
|
74
|
+
self.assertEqual(format_input("[int[2] ; float[2]]", source), [[[1, 2], [1.5, 2.5]]])
|
|
75
|
+
|
|
76
|
+
def test_newline3(self):
|
|
77
|
+
source = io.StringIO("hello\n42\n3.5")
|
|
78
|
+
self.assertEqual(format_input("[str ; int ; float]", source), [["hello", 42, 3.5]])
|
|
79
|
+
|
|
80
|
+
def test_newline4(self):
|
|
81
|
+
source = io.StringIO("42 3.5\nhello world")
|
|
82
|
+
self.assertEqual(format_input("[int float ; str str]", source), [[42, 3.5, "hello", "world"]])
|
|
83
|
+
|
|
84
|
+
def test_newline5(self):
|
|
85
|
+
source = io.StringIO("42 hello\nworld")
|
|
86
|
+
self.assertEqual(format_input("[int [str ; str]]", source), [[42, ["hello", "world"]]])
|
|
87
|
+
|
|
88
|
+
def test_mix1(self):
|
|
89
|
+
source = io.StringIO("42 hello world")
|
|
90
|
+
self.assertEqual(format_input("int [str str]", source), [42, ["hello", "world"]])
|
|
91
|
+
|
|
92
|
+
def test_mix2(self):
|
|
93
|
+
source = io.StringIO("3.5 10 20")
|
|
94
|
+
self.assertEqual(format_input("float [int int]", source), [3.5, [10, 20]])
|
|
95
|
+
|
|
96
|
+
def test_mix3(self):
|
|
97
|
+
source = io.StringIO("alpha 1.25 42")
|
|
98
|
+
self.assertEqual(format_input("str [float int]", source), ["alpha", [1.25, 42]])
|
|
99
|
+
|
|
100
|
+
def test_mix4(self):
|
|
101
|
+
source = io.StringIO("42 3.5 hello world")
|
|
102
|
+
self.assertEqual(format_input("int float [str str]", source), [42, 3.5, ["hello", "world"]])
|
|
103
|
+
|
|
104
|
+
def test_mix5(self):
|
|
105
|
+
source = io.StringIO("1 2 3.5 hello")
|
|
106
|
+
self.assertEqual(format_input("int[2] [float str]", source), [[1, 2], [3.5, "hello"]])
|
|
107
|
+
|
|
108
|
+
def test_mix6(self):
|
|
109
|
+
source = io.StringIO("note words 42 3.5")
|
|
110
|
+
self.assertEqual(format_input("str* int [float]", source), ["note words", 42, [3.5]])
|
|
111
|
+
|
|
112
|
+
def test_mix7(self):
|
|
113
|
+
source = io.StringIO("42 hello extra words")
|
|
114
|
+
self.assertEqual(format_input("int [str*]", source), [42, ["hello extra words"]])
|
|
115
|
+
|
|
116
|
+
def test_mix8(self):
|
|
117
|
+
source = io.StringIO("0.5 1 2 3")
|
|
118
|
+
self.assertEqual(format_input("float [int[2]]", source), [0.5, [[1, 2]]])
|
|
119
|
+
|
|
120
|
+
def test_mix9(self):
|
|
121
|
+
source = io.StringIO("42 hello\n3.5")
|
|
122
|
+
self.assertEqual(format_input("int [str ; float]", source), [42, ["hello", 3.5]])
|
|
123
|
+
|
|
124
|
+
def test_mix10(self):
|
|
125
|
+
source = io.StringIO("1 2 hello\n3.5")
|
|
126
|
+
self.assertEqual(format_input("int [int [str ; float]]", source), [1, [2, ["hello", 3.5]]])
|
|
127
|
+
|
|
128
|
+
def test_named1(self):
|
|
129
|
+
source = io.StringIO("42")
|
|
130
|
+
self.assertEqual(format_input("record:[count:int]", source, named_variables=True), {"record": {"count": 42}})
|
|
131
|
+
|
|
132
|
+
def test_named2(self):
|
|
133
|
+
source = io.StringIO("Ada 36")
|
|
134
|
+
self.assertEqual(format_input("person:[name:str age:int]", source, named_variables=True), {"person": {"name": "Ada", "age": 36}})
|
|
135
|
+
|
|
136
|
+
def test_named3(self):
|
|
137
|
+
source = io.StringIO("3.5")
|
|
138
|
+
self.assertEqual(format_input("measurement:[ratio:float]", source, named_variables=True), {"measurement": {"ratio": 3.5}})
|
|
139
|
+
|
|
140
|
+
def test_named4(self):
|
|
141
|
+
source = io.StringIO("1 2 3")
|
|
142
|
+
self.assertEqual(format_input("record:[values:int[2]]", source, named_variables=True), {"record": {"values": [1, 2]}})
|
|
143
|
+
|
|
144
|
+
def test_named5(self):
|
|
145
|
+
source = io.StringIO("red blue")
|
|
146
|
+
self.assertEqual(format_input("record:[colors:str[2]]", source, named_variables=True), {"record": {"colors": ["red", "blue"]}})
|
|
147
|
+
|
|
148
|
+
def test_named6(self):
|
|
149
|
+
source = io.StringIO("1.25 2.5")
|
|
150
|
+
self.assertEqual(format_input("record:[values:float[]]", source, named_variables=True), {"record": {"values": [1.25, 2.5]}})
|
|
151
|
+
|
|
152
|
+
def test_named7(self):
|
|
153
|
+
source = io.StringIO("hello extra words")
|
|
154
|
+
self.assertEqual(format_input("record:[text:str*]", source, named_variables=True), {"record": {"text": "hello extra words"}})
|
|
155
|
+
|
|
156
|
+
def test_named8(self):
|
|
157
|
+
source = io.StringIO("extra words")
|
|
158
|
+
self.assertEqual(format_input("record:[extra:?]", source, named_variables=True), {"record": {"extra": "extra words"}})
|
|
159
|
+
|
|
160
|
+
def test_named9(self):
|
|
161
|
+
source = io.StringIO("Ada\n36")
|
|
162
|
+
self.assertEqual(format_input("person:[name:str ; age:int]", source, named_variables=True), {"person": {"name": "Ada", "age": 36}})
|
|
163
|
+
|
|
164
|
+
def test_named10(self):
|
|
165
|
+
source = io.StringIO("profile 42 3.5")
|
|
166
|
+
self.assertEqual(format_input("kind:str record:[count:int ratio:float]", source, named_variables=True), {"kind": "profile", "record": {"count": 42, "ratio": 3.5}})
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
if __name__ == "__main__":
|
|
170
|
+
unittest.main()
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import typing
|
|
2
|
-
import 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_LINE_MATCH = re.compile(
|
|
11
|
-
rf"{NAME_PREFIX}(?P<named_type_greedy>str\*)(?![\w*])" # t:str*
|
|
12
|
-
rf"|{NAME_PREFIX}(?P<named_type_len_list>(?:int|float|str)\[[1-9]\d*\])(?![\w\[])" # n:int[5] f:float[7] t:str[1]
|
|
13
|
-
rf"|{NAME_PREFIX}(?P<named_type_list>(?:int|str|float)\[\])(?![\w\[])" # n:int[] f:float[] t:str[]
|
|
14
|
-
rf"|{NAME_PREFIX}(?P<named_type>(?:int|str|float))\b" # n:int f:float t:str
|
|
15
|
-
r"|\b(?P<type_greedy>str\*)(?![\w*])" # str*
|
|
16
|
-
r"|\b(?P<type_len_list>(?:int|float|str)\[[1-9]\d*\])(?![\w\[])" # int[5] float[7] str[1]
|
|
17
|
-
r"|\b(?P<type_list>(?:int|str|float)\[\])(?![\w\[])" # int[] float[] str[]
|
|
18
|
-
r"|\b(?P<type>int|str|float)\b" # int float str
|
|
19
|
-
)
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
def readline(stream: typing.TextIO) -> str:
|
|
23
|
-
line = stream.readline()
|
|
24
|
-
if line == "": return EOFError
|
|
25
|
-
return line.rstrip("\r\n")
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
def format_input(spec: str, stream: typing.TextIO | None = None, named_variables: bool = False) -> list[typing.Any] | dict[str, typing.Any]:
|
|
29
|
-
if stream is None: stream = sys.stdin
|
|
30
|
-
|
|
31
|
-
output = {} if named_variables else []
|
|
32
|
-
inp_queue: deque[str] = deque()
|
|
33
|
-
lines = RE_NEW_LINE.split(spec)
|
|
34
|
-
|
|
35
|
-
for line in lines:
|
|
36
|
-
inp = readline(stream)
|
|
37
|
-
inps = inp.split(" ")
|
|
38
|
-
inp_queue.clear()
|
|
39
|
-
inp_queue.extend(inps)
|
|
40
|
-
|
|
41
|
-
for m in RE_LINE_MATCH.finditer(line):
|
|
42
|
-
|
|
43
|
-
match (m.lastgroup):
|
|
44
|
-
case "named_type_greedy": ret = handle_named_type_greedy(m.group(), inp_queue)
|
|
45
|
-
case "type_greedy": ret = handle_type_greedy(inp_queue)
|
|
46
|
-
case "named_type_len_list": ret = handle_named_type_list(m.group(), inp_queue, max_size=int(m.group().split("[", 1)[1][:-1]))
|
|
47
|
-
case "type_len_list": ret = handle_type_list(m.group(), inp_queue, max_size=int(m.group().split("[", 1)[1][:-1]))
|
|
48
|
-
case "named_type_list": ret = handle_named_type_list(m.group(), inp_queue, max_size=-1)
|
|
49
|
-
case "type_list": ret = handle_type_list(m.group(), inp_queue, max_size=-1)
|
|
50
|
-
case "named_type": ret = handle_named_type(m.group(), inp_queue)
|
|
51
|
-
case "type": ret = handle_type(m.group(), inp_queue)
|
|
52
|
-
|
|
53
|
-
if isinstance(ret, dict) and not named_variables:
|
|
54
|
-
output.extend([ v for v in ret.values() ])
|
|
55
|
-
elif isinstance(ret, dict) and named_variables:
|
|
56
|
-
output.update(ret)
|
|
57
|
-
elif not isinstance(ret, dict) and not named_variables:
|
|
58
|
-
output.append(ret)
|
|
59
|
-
else:
|
|
60
|
-
raise TypeError("Cannot add an unnamed value to named_variables=True")
|
|
61
|
-
|
|
62
|
-
return output
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
from collections import deque
|
|
2
|
-
from .helpers import *
|
|
3
|
-
|
|
4
|
-
def handle_named_type_greedy(dt: str, q: deque[str]) -> str:
|
|
5
|
-
var, _ = dt.split(":")
|
|
6
|
-
output = {var: []}
|
|
7
|
-
|
|
8
|
-
while len(q) > 0:
|
|
9
|
-
inp = q.popleft()
|
|
10
|
-
if is_str(inp):
|
|
11
|
-
output[var].append(inp)
|
|
12
|
-
else:
|
|
13
|
-
q.appendleft(inp) # Not a string, return it back to the queue and return
|
|
14
|
-
break
|
|
15
|
-
|
|
16
|
-
output[var] = " ".join(output[var])
|
|
17
|
-
return output
|
|
18
|
-
|
|
19
|
-
def handle_type_greedy(q: deque[str]) -> str:
|
|
20
|
-
output = []
|
|
21
|
-
|
|
22
|
-
while len(q) > 0:
|
|
23
|
-
inp = q.popleft()
|
|
24
|
-
if is_str(inp):
|
|
25
|
-
output.append(inp)
|
|
26
|
-
else:
|
|
27
|
-
q.appendleft(inp) # Not a string, return it back to the queue and return
|
|
28
|
-
break
|
|
29
|
-
|
|
30
|
-
return " ".join(output)
|
|
31
|
-
|
|
32
|
-
def handle_named_type_list(dt: str, q: deque[str], max_size = -1) -> dict[str, list[str]] | dict[str, list[int]] | dict[str, list[float]]:
|
|
33
|
-
size_left = max_size
|
|
34
|
-
var, dt = dt.split(":")
|
|
35
|
-
output = {var: []}
|
|
36
|
-
|
|
37
|
-
match (dt):
|
|
38
|
-
case dt if dt.startswith("str"):
|
|
39
|
-
while len(q) > 0 and size_left != 0:
|
|
40
|
-
inp = q.popleft()
|
|
41
|
-
if is_str(inp):
|
|
42
|
-
output[var].append(inp)
|
|
43
|
-
size_left -= 1
|
|
44
|
-
else:
|
|
45
|
-
q.appendleft(inp) # Not a string, return it back to the queue and return
|
|
46
|
-
break
|
|
47
|
-
|
|
48
|
-
return output
|
|
49
|
-
|
|
50
|
-
case dt if dt.startswith("int"):
|
|
51
|
-
while len(q) > 0 and size_left != 0:
|
|
52
|
-
inp = q.popleft()
|
|
53
|
-
if is_int(inp):
|
|
54
|
-
output[var].append(int(inp))
|
|
55
|
-
size_left -= 1
|
|
56
|
-
else:
|
|
57
|
-
q.appendleft(inp) # Not an int, return it back to the queue and return
|
|
58
|
-
break
|
|
59
|
-
|
|
60
|
-
return output
|
|
61
|
-
|
|
62
|
-
case dt if dt.startswith("float"):
|
|
63
|
-
while len(q) > 0 and size_left != 0:
|
|
64
|
-
inp = q.popleft()
|
|
65
|
-
if is_float(inp):
|
|
66
|
-
output[var].append(float(inp))
|
|
67
|
-
size_left -= 1
|
|
68
|
-
else:
|
|
69
|
-
q.appendleft(inp) # Not a float, return it back to the queue and return
|
|
70
|
-
break
|
|
71
|
-
|
|
72
|
-
return output
|
|
73
|
-
|
|
74
|
-
def handle_type_list(dt: str, q: deque[str], max_size = -1) -> list[str] | list[int] | list[float]:
|
|
75
|
-
size_left = max_size
|
|
76
|
-
output = []
|
|
77
|
-
|
|
78
|
-
match (dt):
|
|
79
|
-
case dt if dt.startswith("str"):
|
|
80
|
-
while len(q) > 0 and size_left != 0:
|
|
81
|
-
inp = q.popleft()
|
|
82
|
-
if is_str(inp):
|
|
83
|
-
output.append(inp)
|
|
84
|
-
size_left -= 1
|
|
85
|
-
else:
|
|
86
|
-
q.appendleft(inp) # Not a string, return it back to the queue and return
|
|
87
|
-
break
|
|
88
|
-
|
|
89
|
-
return output
|
|
90
|
-
|
|
91
|
-
case dt if dt.startswith("int"):
|
|
92
|
-
while len(q) > 0 and size_left != 0:
|
|
93
|
-
inp = q.popleft()
|
|
94
|
-
if is_int(inp):
|
|
95
|
-
output.append(int(inp))
|
|
96
|
-
size_left -= 1
|
|
97
|
-
else:
|
|
98
|
-
q.appendleft(inp) # Not an int, return it back to the queue and return
|
|
99
|
-
break
|
|
100
|
-
|
|
101
|
-
return output
|
|
102
|
-
|
|
103
|
-
case dt if dt.startswith("float"):
|
|
104
|
-
while len(q) > 0 and size_left != 0:
|
|
105
|
-
inp = q.popleft()
|
|
106
|
-
if is_float(inp):
|
|
107
|
-
output.append(float(inp))
|
|
108
|
-
size_left -= 1
|
|
109
|
-
else:
|
|
110
|
-
q.appendleft(inp) # Not a float, return it back to the queue and return
|
|
111
|
-
break
|
|
112
|
-
|
|
113
|
-
return output
|
|
114
|
-
|
|
115
|
-
def handle_named_type(dt: str, q: deque[str]) -> dict[str, str] | dict[str, int] | dict[str, float] | dict[str, None]:
|
|
116
|
-
inp = q.popleft()
|
|
117
|
-
var, dt = dt.split(":")
|
|
118
|
-
|
|
119
|
-
match (dt):
|
|
120
|
-
case "str": return {var: str(inp)}
|
|
121
|
-
case "int":
|
|
122
|
-
try:
|
|
123
|
-
return {var: int(inp)}
|
|
124
|
-
except ValueError:
|
|
125
|
-
return {var: None}
|
|
126
|
-
case "float":
|
|
127
|
-
try:
|
|
128
|
-
return {var: float(inp)}
|
|
129
|
-
except ValueError:
|
|
130
|
-
return {var: None}
|
|
131
|
-
|
|
132
|
-
def handle_type(dt: str, q: deque[str]) -> str | int | float | None:
|
|
133
|
-
inp = q.popleft()
|
|
134
|
-
|
|
135
|
-
match (dt):
|
|
136
|
-
case "str": return str(inp)
|
|
137
|
-
case "int":
|
|
138
|
-
try:
|
|
139
|
-
return int(inp)
|
|
140
|
-
except ValueError:
|
|
141
|
-
return None
|
|
142
|
-
case "float":
|
|
143
|
-
try:
|
|
144
|
-
return float(inp)
|
|
145
|
-
except ValueError:
|
|
146
|
-
return None
|
|
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
|