format-input 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.
- format_input-0.1.0/LICENSE +21 -0
- format_input-0.1.0/PKG-INFO +74 -0
- format_input-0.1.0/README.md +59 -0
- format_input-0.1.0/pyproject.toml +19 -0
- format_input-0.1.0/setup.cfg +4 -0
- format_input-0.1.0/src/format_input/__init__.py +3 -0
- format_input-0.1.0/src/format_input/format_input.py +62 -0
- format_input-0.1.0/src/format_input/handlers.py +146 -0
- format_input-0.1.0/src/format_input/helpers.py +19 -0
- format_input-0.1.0/src/format_input.egg-info/PKG-INFO +74 -0
- format_input-0.1.0/src/format_input.egg-info/SOURCES.txt +17 -0
- format_input-0.1.0/src/format_input.egg-info/dependency_links.txt +1 -0
- format_input-0.1.0/src/format_input.egg-info/top_level.txt +1 -0
- format_input-0.1.0/tests/test_a_basic_dtype.py +162 -0
- format_input-0.1.0/tests/test_b_basic_dtype_list.py +54 -0
- format_input-0.1.0/tests/test_c_basic_dtype_list_len.py +98 -0
- format_input-0.1.0/tests/test_d_basic_dtype_greedy.py +34 -0
- format_input-0.1.0/tests/test_e_named_dtype.py +130 -0
- format_input-0.1.0/tests/test_mix_abcd.py +159 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Gabba
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: format_input
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Python library for dynamically formatting and processing input
|
|
5
|
+
Author-email: GabbaTK <gabiteok+pypi@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/GabbaTK/Format-Input/
|
|
8
|
+
Project-URL: Issues, https://github.com/GabbaTK/Format-Input/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# Format Input
|
|
17
|
+
|
|
18
|
+
`Format Input` is a small Python library for parsing console or text-stream input
|
|
19
|
+
according to a compact format specification. Instead of repeatedly calling
|
|
20
|
+
`input()`, splitting strings, and converting values by hand, describe the shape
|
|
21
|
+
of each input line once and receive typed Python values.
|
|
22
|
+
|
|
23
|
+
It is useful for:
|
|
24
|
+
|
|
25
|
+
- Competitive programming problems with predictable, line-based input.
|
|
26
|
+
- Command-line tools that read structured text.
|
|
27
|
+
- Small scripts that need to convert input into integers, floats, and strings.
|
|
28
|
+
- Situations where input may contain a mixture of scalar values, lists, and
|
|
29
|
+
values followed by free-form text.
|
|
30
|
+
|
|
31
|
+
For example, instead of manually splitting and converting a line:
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
parts = input().split()
|
|
35
|
+
count = int(parts[0])
|
|
36
|
+
ratio = float(parts[1])
|
|
37
|
+
name = parts[2]
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
You can describe the input once and receive typed values directly:
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
count, ratio, name = format_input("int float str")
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
See [USAGE.MD](./USAGE.MD) for all supported types and format specifications.
|
|
47
|
+
|
|
48
|
+
## Requirements
|
|
49
|
+
|
|
50
|
+
- Python 3.10 or newer
|
|
51
|
+
- No third-party dependencies
|
|
52
|
+
|
|
53
|
+
## Installation
|
|
54
|
+
|
|
55
|
+
Clone the repository:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
git clone https://github.com/GabbaTK/Format-Input.git
|
|
59
|
+
cd Format-Input
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The package is stored in `src/`. To run the test suite from the repository root:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
PYTHONPATH=src python -m unittest discover -s tests -v
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## License
|
|
69
|
+
|
|
70
|
+
This project is available under the [MIT License](./LICENSE).
|
|
71
|
+
|
|
72
|
+
## AI disclosure
|
|
73
|
+
|
|
74
|
+
No AI was used in the making of the code.
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Format Input
|
|
2
|
+
|
|
3
|
+
`Format Input` is a small Python library for parsing console or text-stream input
|
|
4
|
+
according to a compact format specification. Instead of repeatedly calling
|
|
5
|
+
`input()`, splitting strings, and converting values by hand, describe the shape
|
|
6
|
+
of each input line once and receive typed Python values.
|
|
7
|
+
|
|
8
|
+
It is useful for:
|
|
9
|
+
|
|
10
|
+
- Competitive programming problems with predictable, line-based input.
|
|
11
|
+
- Command-line tools that read structured text.
|
|
12
|
+
- Small scripts that need to convert input into integers, floats, and strings.
|
|
13
|
+
- Situations where input may contain a mixture of scalar values, lists, and
|
|
14
|
+
values followed by free-form text.
|
|
15
|
+
|
|
16
|
+
For example, instead of manually splitting and converting a line:
|
|
17
|
+
|
|
18
|
+
```python
|
|
19
|
+
parts = input().split()
|
|
20
|
+
count = int(parts[0])
|
|
21
|
+
ratio = float(parts[1])
|
|
22
|
+
name = parts[2]
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
You can describe the input once and receive typed values directly:
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
count, ratio, name = format_input("int float str")
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
See [USAGE.MD](./USAGE.MD) for all supported types and format specifications.
|
|
32
|
+
|
|
33
|
+
## Requirements
|
|
34
|
+
|
|
35
|
+
- Python 3.10 or newer
|
|
36
|
+
- No third-party dependencies
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
Clone the repository:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
git clone https://github.com/GabbaTK/Format-Input.git
|
|
44
|
+
cd Format-Input
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
The package is stored in `src/`. To run the test suite from the repository root:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
PYTHONPATH=src python -m unittest discover -s tests -v
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## License
|
|
54
|
+
|
|
55
|
+
This project is available under the [MIT License](./LICENSE).
|
|
56
|
+
|
|
57
|
+
## AI disclosure
|
|
58
|
+
|
|
59
|
+
No AI was used in the making of the code.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "format_input"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
authors = [
|
|
5
|
+
{ name="GabbaTK", email="gabiteok+pypi@gmail.com" },
|
|
6
|
+
]
|
|
7
|
+
description = "A Python library for dynamically formatting and processing input"
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">=3.10"
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Programming Language :: Python :: 3",
|
|
12
|
+
"Operating System :: OS Independent",
|
|
13
|
+
]
|
|
14
|
+
license = "MIT"
|
|
15
|
+
license-files = ["LICEN[CS]E*"]
|
|
16
|
+
|
|
17
|
+
[project.urls]
|
|
18
|
+
Homepage = "https://github.com/GabbaTK/Format-Input/"
|
|
19
|
+
Issues = "https://github.com/GabbaTK/Format-Input/issues"
|
|
@@ -0,0 +1,62 @@
|
|
|
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
|
|
@@ -0,0 +1,146 @@
|
|
|
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
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import math
|
|
2
|
+
|
|
3
|
+
def is_str(val: str) -> bool:
|
|
4
|
+
return not (is_int(val) or is_float(val))
|
|
5
|
+
|
|
6
|
+
def is_int(val: str) -> bool:
|
|
7
|
+
try:
|
|
8
|
+
int(val)
|
|
9
|
+
return True
|
|
10
|
+
except ValueError:
|
|
11
|
+
return False
|
|
12
|
+
|
|
13
|
+
def is_float(val: str) -> bool:
|
|
14
|
+
try:
|
|
15
|
+
num = float(val)
|
|
16
|
+
if not math.isnan(num) and not math.isinf(num):
|
|
17
|
+
return True
|
|
18
|
+
except ValueError:
|
|
19
|
+
return False
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: format_input
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Python library for dynamically formatting and processing input
|
|
5
|
+
Author-email: GabbaTK <gabiteok+pypi@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/GabbaTK/Format-Input/
|
|
8
|
+
Project-URL: Issues, https://github.com/GabbaTK/Format-Input/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# Format Input
|
|
17
|
+
|
|
18
|
+
`Format Input` is a small Python library for parsing console or text-stream input
|
|
19
|
+
according to a compact format specification. Instead of repeatedly calling
|
|
20
|
+
`input()`, splitting strings, and converting values by hand, describe the shape
|
|
21
|
+
of each input line once and receive typed Python values.
|
|
22
|
+
|
|
23
|
+
It is useful for:
|
|
24
|
+
|
|
25
|
+
- Competitive programming problems with predictable, line-based input.
|
|
26
|
+
- Command-line tools that read structured text.
|
|
27
|
+
- Small scripts that need to convert input into integers, floats, and strings.
|
|
28
|
+
- Situations where input may contain a mixture of scalar values, lists, and
|
|
29
|
+
values followed by free-form text.
|
|
30
|
+
|
|
31
|
+
For example, instead of manually splitting and converting a line:
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
parts = input().split()
|
|
35
|
+
count = int(parts[0])
|
|
36
|
+
ratio = float(parts[1])
|
|
37
|
+
name = parts[2]
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
You can describe the input once and receive typed values directly:
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
count, ratio, name = format_input("int float str")
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
See [USAGE.MD](./USAGE.MD) for all supported types and format specifications.
|
|
47
|
+
|
|
48
|
+
## Requirements
|
|
49
|
+
|
|
50
|
+
- Python 3.10 or newer
|
|
51
|
+
- No third-party dependencies
|
|
52
|
+
|
|
53
|
+
## Installation
|
|
54
|
+
|
|
55
|
+
Clone the repository:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
git clone https://github.com/GabbaTK/Format-Input.git
|
|
59
|
+
cd Format-Input
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The package is stored in `src/`. To run the test suite from the repository root:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
PYTHONPATH=src python -m unittest discover -s tests -v
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## License
|
|
69
|
+
|
|
70
|
+
This project is available under the [MIT License](./LICENSE).
|
|
71
|
+
|
|
72
|
+
## AI disclosure
|
|
73
|
+
|
|
74
|
+
No AI was used in the making of the code.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/format_input/__init__.py
|
|
5
|
+
src/format_input/format_input.py
|
|
6
|
+
src/format_input/handlers.py
|
|
7
|
+
src/format_input/helpers.py
|
|
8
|
+
src/format_input.egg-info/PKG-INFO
|
|
9
|
+
src/format_input.egg-info/SOURCES.txt
|
|
10
|
+
src/format_input.egg-info/dependency_links.txt
|
|
11
|
+
src/format_input.egg-info/top_level.txt
|
|
12
|
+
tests/test_a_basic_dtype.py
|
|
13
|
+
tests/test_b_basic_dtype_list.py
|
|
14
|
+
tests/test_c_basic_dtype_list_len.py
|
|
15
|
+
tests/test_d_basic_dtype_greedy.py
|
|
16
|
+
tests/test_e_named_dtype.py
|
|
17
|
+
tests/test_mix_abcd.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
format_input
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import unittest
|
|
3
|
+
|
|
4
|
+
from format_input import format_input
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class TestABasicDtype(unittest.TestCase):
|
|
8
|
+
def test_single_int(self):
|
|
9
|
+
source = io.StringIO("42")
|
|
10
|
+
self.assertEqual(format_input("int", source), [42])
|
|
11
|
+
|
|
12
|
+
def test_single_int1(self):
|
|
13
|
+
source = io.StringIO("-17")
|
|
14
|
+
self.assertEqual(format_input("int", source), [-17])
|
|
15
|
+
|
|
16
|
+
def test_single_int2(self):
|
|
17
|
+
source = io.StringIO("0")
|
|
18
|
+
self.assertEqual(format_input("int", source), [0])
|
|
19
|
+
|
|
20
|
+
def test_single_str(self):
|
|
21
|
+
source = io.StringIO("Hello")
|
|
22
|
+
self.assertEqual(format_input("str", source), ["Hello"])
|
|
23
|
+
|
|
24
|
+
def test_single_str1(self):
|
|
25
|
+
source = io.StringIO("D y n")
|
|
26
|
+
self.assertEqual(format_input("str", source), ["D"])
|
|
27
|
+
|
|
28
|
+
def test_single_str2(self):
|
|
29
|
+
source = io.StringIO("Python")
|
|
30
|
+
self.assertEqual(format_input("str", source), ["Python"])
|
|
31
|
+
|
|
32
|
+
def test_single_float(self):
|
|
33
|
+
source = io.StringIO("3.141")
|
|
34
|
+
self.assertEqual(format_input("float", source), [3.141])
|
|
35
|
+
|
|
36
|
+
def test_single_float1(self):
|
|
37
|
+
source = io.StringIO("-0.25")
|
|
38
|
+
self.assertEqual(format_input("float", source), [-0.25])
|
|
39
|
+
|
|
40
|
+
def test_single_float2(self):
|
|
41
|
+
source = io.StringIO("1e3")
|
|
42
|
+
self.assertEqual(format_input("float", source), [1000.0])
|
|
43
|
+
|
|
44
|
+
def test_multiline_int(self):
|
|
45
|
+
source = io.StringIO("420\n69\n80085")
|
|
46
|
+
self.assertEqual(format_input("int ; int ; int", source), [420, 69, 80085])
|
|
47
|
+
|
|
48
|
+
def test_multiline_int1(self):
|
|
49
|
+
source = io.StringIO("-5\n12\n999")
|
|
50
|
+
self.assertEqual(format_input("int ; int ; int", source), [-5, 12, 999])
|
|
51
|
+
|
|
52
|
+
def test_multiline_int2(self):
|
|
53
|
+
source = io.StringIO("7\n0\n-100")
|
|
54
|
+
self.assertEqual(format_input("int;int;int", source), [7, 0, -100])
|
|
55
|
+
|
|
56
|
+
def test_multiline_int3(self):
|
|
57
|
+
source = io.StringIO("123\n4\n465456654645")
|
|
58
|
+
self.assertEqual(format_input("int ;int; int", source), [123, 4, 465456654645])
|
|
59
|
+
|
|
60
|
+
def test_multiline_int4(self):
|
|
61
|
+
source = io.StringIO("8\n16\n32")
|
|
62
|
+
self.assertEqual(format_input("int ; int ;int", source), [8, 16, 32])
|
|
63
|
+
|
|
64
|
+
def test_multiline_int5(self):
|
|
65
|
+
source = io.StringIO("1001\n2002\n3003")
|
|
66
|
+
self.assertEqual(format_input("int; int; int", source), [1001, 2002, 3003])
|
|
67
|
+
|
|
68
|
+
def test_multiline_str(self):
|
|
69
|
+
source = io.StringIO("Hello\nWorld\nTest")
|
|
70
|
+
self.assertEqual(format_input("str ; str ; str", source), ["Hello", "World", "Test"])
|
|
71
|
+
|
|
72
|
+
def test_multiline_str1(self):
|
|
73
|
+
source = io.StringIO("Left\nMiddle\nRight")
|
|
74
|
+
self.assertEqual(format_input("str ; str ; str", source), ["Left", "Middle", "Right"])
|
|
75
|
+
|
|
76
|
+
def test_multiline_str2(self):
|
|
77
|
+
source = io.StringIO("Red\nGreen\nBlue")
|
|
78
|
+
self.assertEqual(format_input("str;str;str", source), ["Red", "Green", "Blue"])
|
|
79
|
+
|
|
80
|
+
def test_multiline_str3(self):
|
|
81
|
+
source = io.StringIO("Alpha\nBeta\nGamma")
|
|
82
|
+
self.assertEqual(format_input("str ;str; str", source), ["Alpha", "Beta", "Gamma"])
|
|
83
|
+
|
|
84
|
+
def test_multiline_str4(self):
|
|
85
|
+
source = io.StringIO("One\nTwo\nThree")
|
|
86
|
+
self.assertEqual(format_input("str ; str;str", source), ["One", "Two", "Three"])
|
|
87
|
+
|
|
88
|
+
def test_multiline_str5(self):
|
|
89
|
+
source = io.StringIO("North\nSouth\nWest")
|
|
90
|
+
self.assertEqual(format_input("str; str ; str", source), ["North", "South", "West"])
|
|
91
|
+
|
|
92
|
+
def test_multiline_float(self):
|
|
93
|
+
source = io.StringIO("1.5\n2.25\n3.75")
|
|
94
|
+
self.assertEqual(format_input("float ; float ; float", source), [1.5, 2.25, 3.75])
|
|
95
|
+
|
|
96
|
+
def test_multiline_float1(self):
|
|
97
|
+
source = io.StringIO("-1.5\n0.0\n2.75")
|
|
98
|
+
self.assertEqual(format_input("float ; float ; float", source), [-1.5, 0.0, 2.75])
|
|
99
|
+
|
|
100
|
+
def test_multiline_float2(self):
|
|
101
|
+
source = io.StringIO("4.25\n5.5\n6.75")
|
|
102
|
+
self.assertEqual(format_input("float;float;float", source), [4.25, 5.5, 6.75])
|
|
103
|
+
|
|
104
|
+
def test_multiline_float3(self):
|
|
105
|
+
source = io.StringIO("10.1\n20.2\n30.3")
|
|
106
|
+
self.assertEqual(format_input("float ;float; float", source), [10.1, 20.2, 30.3])
|
|
107
|
+
|
|
108
|
+
def test_multiline_float4(self):
|
|
109
|
+
source = io.StringIO("0.01\n0.02\n0.03")
|
|
110
|
+
self.assertEqual(format_input("float ; float;float", source), [0.01, 0.02, 0.03])
|
|
111
|
+
|
|
112
|
+
def test_multiline_float5(self):
|
|
113
|
+
source = io.StringIO("100.5\n200.5\n300.5")
|
|
114
|
+
self.assertEqual(format_input("float; float ; float", source), [100.5, 200.5, 300.5])
|
|
115
|
+
|
|
116
|
+
def test_singleline_int(self):
|
|
117
|
+
source = io.StringIO("1 2 3")
|
|
118
|
+
self.assertEqual(format_input("int int int", source), [1, 2, 3])
|
|
119
|
+
|
|
120
|
+
def test_singleline_int1(self):
|
|
121
|
+
source = io.StringIO("-1 0 1")
|
|
122
|
+
self.assertEqual(format_input("int int int", source), [-1, 0, 1])
|
|
123
|
+
|
|
124
|
+
def test_singleline_int2(self):
|
|
125
|
+
source = io.StringIO("10 20 30")
|
|
126
|
+
self.assertEqual(format_input("int int int", source), [10, 20, 30])
|
|
127
|
+
|
|
128
|
+
def test_singleline_str(self):
|
|
129
|
+
source = io.StringIO("Hello World Test")
|
|
130
|
+
self.assertEqual(format_input("str str str", source), ["Hello", "World", "Test"])
|
|
131
|
+
|
|
132
|
+
def test_singleline_str1(self):
|
|
133
|
+
source = io.StringIO("Cat Dog Bird")
|
|
134
|
+
self.assertEqual(format_input("str str str", source), ["Cat", "Dog", "Bird"])
|
|
135
|
+
|
|
136
|
+
def test_singleline_str2(self):
|
|
137
|
+
source = io.StringIO("Rock Paper Scissors")
|
|
138
|
+
self.assertEqual(format_input("str str str", source), ["Rock", "Paper", "Scissors"])
|
|
139
|
+
|
|
140
|
+
def test_singleline_float(self):
|
|
141
|
+
source = io.StringIO("1.1 2.2 3.3")
|
|
142
|
+
self.assertEqual(format_input("float float float", source), [1.1, 2.2, 3.3])
|
|
143
|
+
|
|
144
|
+
def test_singleline_float1(self):
|
|
145
|
+
source = io.StringIO("-2.5 0.5 7.75")
|
|
146
|
+
self.assertEqual(format_input("float float float", source), [-2.5, 0.5, 7.75])
|
|
147
|
+
|
|
148
|
+
def test_singleline_float2(self):
|
|
149
|
+
source = io.StringIO("10.25 20.5 30.75")
|
|
150
|
+
self.assertEqual(format_input("float float float", source), [10.25, 20.5, 30.75])
|
|
151
|
+
|
|
152
|
+
def test_misc(self):
|
|
153
|
+
source = io.StringIO("Hi\n\nHello")
|
|
154
|
+
self.assertEqual(format_input("str ; str ; str", source), ["Hi", "", "Hello"])
|
|
155
|
+
|
|
156
|
+
def test_misc1(self):
|
|
157
|
+
source = io.StringIO("3\n\n7")
|
|
158
|
+
self.assertEqual(format_input("int ; int ; int", source), [3, None, 7])
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
if __name__ == "__main__":
|
|
162
|
+
unittest.main()
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import unittest
|
|
3
|
+
|
|
4
|
+
from format_input import format_input
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class TestBBasicDtypeList(unittest.TestCase):
|
|
8
|
+
def test_int(self):
|
|
9
|
+
source = io.StringIO("42 69 80085")
|
|
10
|
+
self.assertEqual(format_input("int[]", source), [[42, 69, 80085]])
|
|
11
|
+
|
|
12
|
+
def test_int1(self):
|
|
13
|
+
source = io.StringIO("-4 0 12")
|
|
14
|
+
self.assertEqual(format_input("int[]", source), [[-4, 0, 12]])
|
|
15
|
+
|
|
16
|
+
def test_int2(self):
|
|
17
|
+
source = io.StringIO("1 2 3\n4 5")
|
|
18
|
+
self.assertEqual(format_input("int[] ; int[]", source), [[1, 2, 3], [4, 5]])
|
|
19
|
+
|
|
20
|
+
def test_float(self):
|
|
21
|
+
source = io.StringIO("3.141 5.926 5.358")
|
|
22
|
+
self.assertEqual(format_input("float[]", source), [[3.141, 5.926, 5.358]])
|
|
23
|
+
|
|
24
|
+
def test_float1(self):
|
|
25
|
+
source = io.StringIO("-1.25 0.5 2.75")
|
|
26
|
+
self.assertEqual(format_input("float[]", source), [[-1.25, 0.5, 2.75]])
|
|
27
|
+
|
|
28
|
+
def test_float2(self):
|
|
29
|
+
source = io.StringIO("1.1 2.2\n3.3 4.4 5.5")
|
|
30
|
+
self.assertEqual(format_input("float[] ; float[]", source), [[1.1, 2.2], [3.3, 4.4, 5.5]])
|
|
31
|
+
|
|
32
|
+
def test_float3(self):
|
|
33
|
+
source = io.StringIO("1e3 2.5e-2 -3E+1")
|
|
34
|
+
self.assertEqual(format_input("float[]", source), [[1000.0, 0.025, -30.0]])
|
|
35
|
+
|
|
36
|
+
def test_float4(self):
|
|
37
|
+
source = io.StringIO("4e-3 5E2\n6.25e1")
|
|
38
|
+
self.assertEqual(format_input("float[] ; float[]", source), [[0.004, 500.0], [62.5]])
|
|
39
|
+
|
|
40
|
+
def test_str(self):
|
|
41
|
+
source = io.StringIO("Hello, World!")
|
|
42
|
+
self.assertEqual(format_input("str[]", source), [["Hello,", "World!"]])
|
|
43
|
+
|
|
44
|
+
def test_str1(self):
|
|
45
|
+
source = io.StringIO("one two three four")
|
|
46
|
+
self.assertEqual(format_input("str[]", source), [["one", "two", "three", "four"]])
|
|
47
|
+
|
|
48
|
+
def test_str2(self):
|
|
49
|
+
source = io.StringIO("red green\nblue yellow")
|
|
50
|
+
self.assertEqual(format_input("str[] ; str[]", source), [["red", "green"], ["blue", "yellow"]])
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
if __name__ == "__main__":
|
|
54
|
+
unittest.main()
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import unittest
|
|
3
|
+
|
|
4
|
+
from format_input import format_input
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class TestCBasicDtypeList(unittest.TestCase):
|
|
8
|
+
def test_int(self):
|
|
9
|
+
source = io.StringIO("42 69 80085")
|
|
10
|
+
self.assertEqual(format_input("int[2]", source), [[42, 69]])
|
|
11
|
+
|
|
12
|
+
def test_int1(self):
|
|
13
|
+
source = io.StringIO("-7 12")
|
|
14
|
+
self.assertEqual(format_input("int[1]", source), [[-7]])
|
|
15
|
+
|
|
16
|
+
def test_int2(self):
|
|
17
|
+
source = io.StringIO("1 2 3 4")
|
|
18
|
+
self.assertEqual(format_input("int[3]", source), [[1, 2, 3]])
|
|
19
|
+
|
|
20
|
+
def test_int3(self):
|
|
21
|
+
source = io.StringIO("1 2 3 4")
|
|
22
|
+
self.assertEqual(format_input("int[2] int[2]", source), [[1, 2], [3, 4]])
|
|
23
|
+
|
|
24
|
+
def test_float(self):
|
|
25
|
+
source = io.StringIO("3.141 5.926 5.358")
|
|
26
|
+
self.assertEqual(format_input("float[2]", source), [[3.141, 5.926]])
|
|
27
|
+
|
|
28
|
+
def test_float1(self):
|
|
29
|
+
source = io.StringIO("-0.5 2.5")
|
|
30
|
+
self.assertEqual(format_input("float[1]", source), [[-0.5]])
|
|
31
|
+
|
|
32
|
+
def test_float2(self):
|
|
33
|
+
source = io.StringIO("1e-2 2.5 3E1 40")
|
|
34
|
+
self.assertEqual(format_input("float[3]", source), [[0.01, 2.5, 30.0]])
|
|
35
|
+
|
|
36
|
+
def test_str(self):
|
|
37
|
+
source = io.StringIO("Hello, World!")
|
|
38
|
+
self.assertEqual(format_input("str[1]", source), [["Hello,"]])
|
|
39
|
+
|
|
40
|
+
def test_str1(self):
|
|
41
|
+
source = io.StringIO("Hello, World! Extra")
|
|
42
|
+
self.assertEqual(format_input("str[2]", source), [["Hello,", "World!"]])
|
|
43
|
+
|
|
44
|
+
def test_mix(self):
|
|
45
|
+
source = io.StringIO("10 20\n1.5 2.5\nred blue")
|
|
46
|
+
self.assertEqual(format_input("int[2] ; float[2] ; str[2]", source), [[10, 20], [1.5, 2.5], ["red", "blue"]])
|
|
47
|
+
|
|
48
|
+
def test_mix1(self):
|
|
49
|
+
source = io.StringIO("1 2 1.5 2.5")
|
|
50
|
+
self.assertEqual(format_input("int[2] float[2]", source), [[1, 2], [1.5, 2.5]])
|
|
51
|
+
|
|
52
|
+
def test_mix2(self):
|
|
53
|
+
source = io.StringIO("red blue 3 4")
|
|
54
|
+
self.assertEqual(format_input("str[2] int[2]", source), [["red", "blue"], [3, 4]])
|
|
55
|
+
|
|
56
|
+
def test_mix3(self):
|
|
57
|
+
source = io.StringIO("1.25 2.5 hello world")
|
|
58
|
+
self.assertEqual(format_input("float[2] str[2]", source), [[1.25, 2.5], ["hello", "world"]])
|
|
59
|
+
|
|
60
|
+
def test_mix4(self):
|
|
61
|
+
source = io.StringIO("5 6\n7.5 8.5")
|
|
62
|
+
self.assertEqual(format_input("int[2] ; float[2]", source), [[5, 6], [7.5, 8.5]])
|
|
63
|
+
|
|
64
|
+
def test_mix5(self):
|
|
65
|
+
source = io.StringIO("north south\neast west")
|
|
66
|
+
self.assertEqual(format_input("str[2] ; str[2]", source), [["north", "south"], ["east", "west"]])
|
|
67
|
+
|
|
68
|
+
def test_mix6(self):
|
|
69
|
+
source = io.StringIO("1.5 2.5\n3 4")
|
|
70
|
+
self.assertEqual(format_input("float[2] ; int[2]", source), [[1.5, 2.5], [3, 4]])
|
|
71
|
+
|
|
72
|
+
def test_mix7(self):
|
|
73
|
+
source = io.StringIO("1 2 3.5 4.5 alpha beta")
|
|
74
|
+
self.assertEqual(format_input("int[2] float[2] str[2]", source), [[1, 2], [3.5, 4.5], ["alpha", "beta"]])
|
|
75
|
+
|
|
76
|
+
def test_mix8(self):
|
|
77
|
+
source = io.StringIO("alpha beta 1 2 3.5 4.5")
|
|
78
|
+
self.assertEqual(format_input("str[2] int[2] float[2]", source), [["alpha", "beta"], [1, 2], [3.5, 4.5]])
|
|
79
|
+
|
|
80
|
+
def test_mix9(self):
|
|
81
|
+
source = io.StringIO("1 2\nred blue\n3.5 4.5")
|
|
82
|
+
self.assertEqual(format_input("int[2] ; str[2] ; float[2]", source), [[1, 2], ["red", "blue"], [3.5, 4.5]])
|
|
83
|
+
|
|
84
|
+
def test_mix10(self):
|
|
85
|
+
source = io.StringIO("3.5 4.5\n5 6\none two")
|
|
86
|
+
self.assertEqual(format_input("float[2] ; int[2] ; str[2]", source), [[3.5, 4.5], [5, 6], ["one", "two"]])
|
|
87
|
+
|
|
88
|
+
def test_mix11(self):
|
|
89
|
+
source = io.StringIO("1 2 3.5 4.5\nx y 9 10")
|
|
90
|
+
self.assertEqual(format_input("int[2] float[2] ; str[2] int[2]", source), [[1, 2], [3.5, 4.5], ["x", "y"], [9, 10]])
|
|
91
|
+
|
|
92
|
+
def test_mix12(self):
|
|
93
|
+
source = io.StringIO("a b 1.5 2.5\n3 4 c d")
|
|
94
|
+
self.assertEqual(format_input("str[2] float[2] ; int[2] str[2]", source), [["a", "b"], [1.5, 2.5], [3, 4], ["c", "d"]])
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
if __name__ == "__main__":
|
|
98
|
+
unittest.main()
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import unittest
|
|
3
|
+
|
|
4
|
+
from format_input import format_input
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class TestDBasicDtypeList(unittest.TestCase):
|
|
8
|
+
def test1(self):
|
|
9
|
+
source = io.StringIO("Hello python world !")
|
|
10
|
+
self.assertEqual(format_input("str[1] str*", source), [["Hello"], "python world !"])
|
|
11
|
+
|
|
12
|
+
def test2(self):
|
|
13
|
+
source = io.StringIO("alpha beta 17")
|
|
14
|
+
self.assertEqual(format_input("str* int", source), ["alpha beta", 17])
|
|
15
|
+
|
|
16
|
+
def test3(self):
|
|
17
|
+
source = io.StringIO("start middle 2.5")
|
|
18
|
+
self.assertEqual(format_input("str* float", source), ["start middle", 2.5])
|
|
19
|
+
|
|
20
|
+
def test4(self):
|
|
21
|
+
source = io.StringIO("red green\nblue 12")
|
|
22
|
+
self.assertEqual(format_input("str* ; str* int", source), ["red green", "blue", 12])
|
|
23
|
+
|
|
24
|
+
def test5(self):
|
|
25
|
+
source = io.StringIO("all words are consumed")
|
|
26
|
+
self.assertEqual(format_input("str*", source), ["all words are consumed"])
|
|
27
|
+
|
|
28
|
+
def test6(self):
|
|
29
|
+
source = io.StringIO("42")
|
|
30
|
+
self.assertEqual(format_input("str* int", source), ["", 42])
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
if __name__ == "__main__":
|
|
34
|
+
unittest.main()
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import unittest
|
|
3
|
+
|
|
4
|
+
from format_input import format_input
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class TestENamedDtype(unittest.TestCase):
|
|
8
|
+
def test_single_int(self):
|
|
9
|
+
source = io.StringIO("42")
|
|
10
|
+
self.assertEqual(format_input("n:int", source), [42])
|
|
11
|
+
|
|
12
|
+
def test_single_int_named(self):
|
|
13
|
+
source = io.StringIO("42")
|
|
14
|
+
self.assertEqual(format_input("n:int", source, named_variables=True), {"n": 42})
|
|
15
|
+
|
|
16
|
+
def test_single_float(self):
|
|
17
|
+
source = io.StringIO("3.141")
|
|
18
|
+
self.assertEqual(format_input("f:float", source), [3.141])
|
|
19
|
+
|
|
20
|
+
def test_single_float_named(self):
|
|
21
|
+
source = io.StringIO("-0.25")
|
|
22
|
+
self.assertEqual(format_input("f:float", source, named_variables=True), {"f": -0.25})
|
|
23
|
+
|
|
24
|
+
def test_single_str(self):
|
|
25
|
+
source = io.StringIO("Hello")
|
|
26
|
+
self.assertEqual(format_input("s:str", source), ["Hello"])
|
|
27
|
+
|
|
28
|
+
def test_single_str_named(self):
|
|
29
|
+
source = io.StringIO("Python")
|
|
30
|
+
self.assertEqual(format_input("s:str", source, named_variables=True), {"s": "Python"})
|
|
31
|
+
|
|
32
|
+
def test_single_int_list(self):
|
|
33
|
+
source = io.StringIO("42 43 44 45")
|
|
34
|
+
self.assertEqual(format_input("hai:int[]", source), [[42, 43, 44, 45]])
|
|
35
|
+
|
|
36
|
+
def test_single_int_list_named(self):
|
|
37
|
+
source = io.StringIO("89 100 ")
|
|
38
|
+
self.assertEqual(format_input("hai:int[]", source, named_variables=True), {"hai": [89, 100]})
|
|
39
|
+
|
|
40
|
+
def test_single_float_list(self):
|
|
41
|
+
source = io.StringIO("-1.25 0.5 2.75")
|
|
42
|
+
self.assertEqual(format_input("values:float[]", source), [[-1.25, 0.5, 2.75]])
|
|
43
|
+
|
|
44
|
+
def test_single_float_list_named(self):
|
|
45
|
+
source = io.StringIO("1e3 2.5e-2 -3E+1")
|
|
46
|
+
self.assertEqual(format_input("values:float[]", source, named_variables=True), {"values": [1000.0, 0.025, -30.0]})
|
|
47
|
+
|
|
48
|
+
def test_single_str_list(self):
|
|
49
|
+
source = io.StringIO("one two three four")
|
|
50
|
+
self.assertEqual(format_input("words:str[]", source), [["one", "two", "three", "four"]])
|
|
51
|
+
|
|
52
|
+
def test_single_str_list_named(self):
|
|
53
|
+
source = io.StringIO("red green blue")
|
|
54
|
+
self.assertEqual(format_input("words:str[]", source, named_variables=True), {"words": ["red", "green", "blue"]})
|
|
55
|
+
|
|
56
|
+
def test_single_int_len_list(self):
|
|
57
|
+
source = io.StringIO("1 2 3 4")
|
|
58
|
+
self.assertEqual(format_input("numbers:int[2]", source), [[1, 2]])
|
|
59
|
+
|
|
60
|
+
def test_single_int_len_list_named(self):
|
|
61
|
+
source = io.StringIO("5 6 7")
|
|
62
|
+
self.assertEqual(format_input("numbers:int[3]", source, named_variables=True), {"numbers": [5, 6, 7]})
|
|
63
|
+
|
|
64
|
+
def test_single_float_len_list(self):
|
|
65
|
+
source = io.StringIO("1.25 2.5 3.75")
|
|
66
|
+
self.assertEqual(format_input("values:float[2]", source), [[1.25, 2.5]])
|
|
67
|
+
|
|
68
|
+
def test_single_float_len_list_named(self):
|
|
69
|
+
source = io.StringIO("-0.5 2.5 40")
|
|
70
|
+
self.assertEqual(format_input("values:float[2]", source, named_variables=True), {"values": [-0.5, 2.5]})
|
|
71
|
+
|
|
72
|
+
def test_single_str_len_list(self):
|
|
73
|
+
source = io.StringIO("Hello World Extra")
|
|
74
|
+
self.assertEqual(format_input("words:str[2]", source), [["Hello", "World"]])
|
|
75
|
+
|
|
76
|
+
def test_single_str_len_list_named(self):
|
|
77
|
+
source = io.StringIO("red green blue")
|
|
78
|
+
self.assertEqual(format_input("words:str[1]", source, named_variables=True), {"words": ["red"]})
|
|
79
|
+
|
|
80
|
+
def test_single_greedy(self):
|
|
81
|
+
source = io.StringIO("alpha beta gamma")
|
|
82
|
+
self.assertEqual(format_input("text:str*", source), ["alpha beta gamma"])
|
|
83
|
+
|
|
84
|
+
def test_single_greedy_named(self):
|
|
85
|
+
source = io.StringIO("red green blue")
|
|
86
|
+
self.assertEqual(format_input("text:str*", source, named_variables=True), {"text": "red green blue"})
|
|
87
|
+
|
|
88
|
+
def test_mix1(self):
|
|
89
|
+
source = io.StringIO("42 3.25 Hello")
|
|
90
|
+
self.assertEqual(format_input("n:int f:float s:str", source), [42, 3.25, "Hello"])
|
|
91
|
+
|
|
92
|
+
def test_mix2(self):
|
|
93
|
+
source = io.StringIO("42 3.25 Hello")
|
|
94
|
+
self.assertEqual(format_input("n:int f:float s:str", source, named_variables=True), {"n": 42, "f": 3.25, "s": "Hello"})
|
|
95
|
+
|
|
96
|
+
def test_mix3(self):
|
|
97
|
+
source = io.StringIO("1 2 3.5 4.5 red blue")
|
|
98
|
+
self.assertEqual(format_input("ints:int[2] floats:float[2] words:str[2]", source), [[1, 2], [3.5, 4.5], ["red", "blue"]])
|
|
99
|
+
|
|
100
|
+
def test_mix4(self):
|
|
101
|
+
source = io.StringIO("1 2 3.5 4.5 red blue")
|
|
102
|
+
self.assertEqual(format_input("ints:int[2] floats:float[2] words:str[2]", source, named_variables=True), {"ints": [1, 2], "floats": [3.5, 4.5], "words": ["red", "blue"]})
|
|
103
|
+
|
|
104
|
+
def test_mix5(self):
|
|
105
|
+
source = io.StringIO("10 20 one two 1.5 2.5")
|
|
106
|
+
self.assertEqual(format_input("values:int[] words:str[] floats:float[]", source), [[10, 20], ["one", "two"], [1.5, 2.5]])
|
|
107
|
+
|
|
108
|
+
def test_mix6(self):
|
|
109
|
+
source = io.StringIO("10 20 one two 1.5 2.5")
|
|
110
|
+
self.assertEqual(format_input("values:int[] words:str[] floats:float[]", source, named_variables=True), {"values": [10, 20], "words": ["one", "two"], "floats": [1.5, 2.5]})
|
|
111
|
+
|
|
112
|
+
def test_mix7(self):
|
|
113
|
+
source = io.StringIO("alpha beta 42 3.75 gamma delta")
|
|
114
|
+
self.assertEqual(format_input("prefix:str[2] count:int ratio:float suffix:str*", source), [["alpha", "beta"], 42, 3.75, "gamma delta"])
|
|
115
|
+
|
|
116
|
+
def test_mix8(self):
|
|
117
|
+
source = io.StringIO("alpha beta 42 3.75 gamma delta")
|
|
118
|
+
self.assertEqual(format_input("prefix:str[2] count:int ratio:float suffix:str*", source, named_variables=True), {"prefix": ["alpha", "beta"], "count": 42, "ratio": 3.75, "suffix": "gamma delta"})
|
|
119
|
+
|
|
120
|
+
def test_mix9(self):
|
|
121
|
+
source = io.StringIO("7 8\nred blue\n1.25 2.5")
|
|
122
|
+
self.assertEqual(format_input("numbers:int[2] ; words:str[2] ; values:float[2]", source), [[7, 8], ["red", "blue"], [1.25, 2.5]])
|
|
123
|
+
|
|
124
|
+
def test_mix10(self):
|
|
125
|
+
source = io.StringIO("7 8\nred blue\n1.25 2.5")
|
|
126
|
+
self.assertEqual(format_input("numbers:int[2] ; words:str[2] ; values:float[2]", source, named_variables=True), {"numbers": [7, 8], "words": ["red", "blue"], "values": [1.25, 2.5]})
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
if __name__ == "__main__":
|
|
130
|
+
unittest.main()
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import unittest
|
|
3
|
+
|
|
4
|
+
from format_input import format_input
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class TestMixABCD(unittest.TestCase):
|
|
8
|
+
def test_mix1(self):
|
|
9
|
+
source = io.StringIO(
|
|
10
|
+
"opening words for greedy input\n"
|
|
11
|
+
"1.25 2.5 3.75 4.5 5.25 6 7\n"
|
|
12
|
+
"10 11 middle words 12\n"
|
|
13
|
+
"one two three four five 0.1 0.2 0.3\n"
|
|
14
|
+
"99\n"
|
|
15
|
+
"more greedy words here\n"
|
|
16
|
+
"last scalar ending words\n"
|
|
17
|
+
"final ending words"
|
|
18
|
+
)
|
|
19
|
+
spec = (
|
|
20
|
+
"str* ; float float float[3] int[2] ; "
|
|
21
|
+
"int[2] str* int ; str[5] float[3] ; "
|
|
22
|
+
"int ; str* ; str ; str[3]"
|
|
23
|
+
)
|
|
24
|
+
expected = [
|
|
25
|
+
"opening words for greedy input",
|
|
26
|
+
1.25,
|
|
27
|
+
2.5,
|
|
28
|
+
[3.75, 4.5, 5.25],
|
|
29
|
+
[6, 7],
|
|
30
|
+
[10, 11],
|
|
31
|
+
"middle words",
|
|
32
|
+
12,
|
|
33
|
+
["one", "two", "three", "four", "five"],
|
|
34
|
+
[0.1, 0.2, 0.3],
|
|
35
|
+
99,
|
|
36
|
+
"more greedy words here",
|
|
37
|
+
"last",
|
|
38
|
+
["final", "ending", "words"],
|
|
39
|
+
]
|
|
40
|
+
self.assertEqual(format_input(spec, source), expected)
|
|
41
|
+
|
|
42
|
+
def test_mix2(self):
|
|
43
|
+
source = io.StringIO(
|
|
44
|
+
"2 4 6 8 10\n"
|
|
45
|
+
"1e-2 2.5E1 -3.75 4.125 stop 42\n"
|
|
46
|
+
"alpha beta gamma 100 200\n"
|
|
47
|
+
"301 302 303 5.5 6.5 7.5\n"
|
|
48
|
+
"tail words before 8"
|
|
49
|
+
)
|
|
50
|
+
spec = (
|
|
51
|
+
"int[] ; float[4] str int ; "
|
|
52
|
+
"str[3] int[2] ; int[3] float[] ; str* int"
|
|
53
|
+
)
|
|
54
|
+
expected = [
|
|
55
|
+
[2, 4, 6, 8, 10],
|
|
56
|
+
[0.01, 25.0, -3.75, 4.125],
|
|
57
|
+
"stop",
|
|
58
|
+
42,
|
|
59
|
+
["alpha", "beta", "gamma"],
|
|
60
|
+
[100, 200],
|
|
61
|
+
[301, 302, 303],
|
|
62
|
+
[5.5, 6.5, 7.5],
|
|
63
|
+
"tail words before",
|
|
64
|
+
8,
|
|
65
|
+
]
|
|
66
|
+
self.assertEqual(format_input(spec, source), expected)
|
|
67
|
+
|
|
68
|
+
def test_mix3(self):
|
|
69
|
+
source = io.StringIO(
|
|
70
|
+
"discard these words 11 12.5\n"
|
|
71
|
+
"13 14 15 16 17\n"
|
|
72
|
+
"red green blue 0.5 1.5\n"
|
|
73
|
+
"2.5 3.5 4.5 21 22\n"
|
|
74
|
+
"final row text 23 24 25"
|
|
75
|
+
)
|
|
76
|
+
spec = (
|
|
77
|
+
"str* int float ; int[2] int[3] ; "
|
|
78
|
+
"str[3] float[2] ; float[3] int[2] ; "
|
|
79
|
+
"str* int[3]"
|
|
80
|
+
)
|
|
81
|
+
expected = [
|
|
82
|
+
"discard these words",
|
|
83
|
+
11,
|
|
84
|
+
12.5,
|
|
85
|
+
[13, 14],
|
|
86
|
+
[15, 16, 17],
|
|
87
|
+
["red", "green", "blue"],
|
|
88
|
+
[0.5, 1.5],
|
|
89
|
+
[2.5, 3.5, 4.5],
|
|
90
|
+
[21, 22],
|
|
91
|
+
"final row text",
|
|
92
|
+
[23, 24, 25],
|
|
93
|
+
]
|
|
94
|
+
self.assertEqual(format_input(spec, source), expected)
|
|
95
|
+
|
|
96
|
+
def test_mix4(self):
|
|
97
|
+
source = io.StringIO(
|
|
98
|
+
"1.5 2.5 3.5 words between 7 8\n"
|
|
99
|
+
"first second third fourth fifth 9.5 10.5\n"
|
|
100
|
+
"11 12.25 13 14.25 15 16\n"
|
|
101
|
+
"left right 17 18 19.5 20.5 21.5 22.5\n"
|
|
102
|
+
"last line has several words 23"
|
|
103
|
+
)
|
|
104
|
+
spec = (
|
|
105
|
+
"float[3] str* int[2] ; str[5] float[2] ; "
|
|
106
|
+
"int float int float int[2] ; "
|
|
107
|
+
"str[2] int[2] float[4] ; str* int"
|
|
108
|
+
)
|
|
109
|
+
expected = [
|
|
110
|
+
[1.5, 2.5, 3.5],
|
|
111
|
+
"words between",
|
|
112
|
+
[7, 8],
|
|
113
|
+
["first", "second", "third", "fourth", "fifth"],
|
|
114
|
+
[9.5, 10.5],
|
|
115
|
+
11,
|
|
116
|
+
12.25,
|
|
117
|
+
13,
|
|
118
|
+
14.25,
|
|
119
|
+
[15, 16],
|
|
120
|
+
["left", "right"],
|
|
121
|
+
[17, 18],
|
|
122
|
+
[19.5, 20.5, 21.5, 22.5],
|
|
123
|
+
"last line has several words",
|
|
124
|
+
23,
|
|
125
|
+
]
|
|
126
|
+
self.assertEqual(format_input(spec, source), expected)
|
|
127
|
+
|
|
128
|
+
def test_mix5(self):
|
|
129
|
+
source = io.StringIO(
|
|
130
|
+
"north south east west 31 32\n"
|
|
131
|
+
"33 34.5 35 36 37 38 39.5 40.5\n"
|
|
132
|
+
"41.5 42.5 43.5 note after floats\n"
|
|
133
|
+
"red orange yellow green blue 44 45 46\n"
|
|
134
|
+
"47 48 49 words at the end"
|
|
135
|
+
)
|
|
136
|
+
spec = (
|
|
137
|
+
"str[4] int[2] ; int float int[3] float[2] ; "
|
|
138
|
+
"float[3] str* ; str[5] int[3] ; "
|
|
139
|
+
"int[3] str*"
|
|
140
|
+
)
|
|
141
|
+
expected = [
|
|
142
|
+
["north", "south", "east", "west"],
|
|
143
|
+
[31, 32],
|
|
144
|
+
33,
|
|
145
|
+
34.5,
|
|
146
|
+
[35, 36, 37],
|
|
147
|
+
[38.0, 39.5],
|
|
148
|
+
[41.5, 42.5, 43.5],
|
|
149
|
+
"note after floats",
|
|
150
|
+
["red", "orange", "yellow", "green", "blue"],
|
|
151
|
+
[44, 45, 46],
|
|
152
|
+
[47, 48, 49],
|
|
153
|
+
"words at the end",
|
|
154
|
+
]
|
|
155
|
+
self.assertEqual(format_input(spec, source), expected)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
if __name__ == "__main__":
|
|
159
|
+
unittest.main()
|