format-input 0.2.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.3.0/PKG-INFO +86 -0
- format_input-0.3.0/README.MD +70 -0
- {format_input-0.2.0 → format_input-0.3.0}/pyproject.toml +5 -2
- {format_input-0.2.0 → format_input-0.3.0}/src/format_input/format_input.py +29 -23
- format_input-0.3.0/src/format_input.egg-info/PKG-INFO +86 -0
- {format_input-0.2.0 → format_input-0.3.0}/src/format_input.egg-info/SOURCES.txt +3 -0
- format_input-0.3.0/src/format_input.egg-info/requires.txt +1 -0
- {format_input-0.2.0 → format_input-0.3.0}/tests/test_000_edgecase.py +48 -0
- format_input-0.3.0/tests/test_g_groups.py +170 -0
- format_input-0.2.0/PKG-INFO +0 -14
- format_input-0.2.0/src/format_input.egg-info/PKG-INFO +0 -14
- {format_input-0.2.0 → format_input-0.3.0}/LICENSE +0 -0
- {format_input-0.2.0 → format_input-0.3.0}/setup.cfg +0 -0
- {format_input-0.2.0 → format_input-0.3.0}/src/format_input/__init__.py +0 -0
- {format_input-0.2.0 → format_input-0.3.0}/src/format_input/handlers.py +0 -0
- {format_input-0.2.0 → format_input-0.3.0}/src/format_input/helpers.py +0 -0
- {format_input-0.2.0 → format_input-0.3.0}/src/format_input.egg-info/dependency_links.txt +0 -0
- {format_input-0.2.0 → format_input-0.3.0}/src/format_input.egg-info/top_level.txt +0 -0
- {format_input-0.2.0 → format_input-0.3.0}/tests/test_a_basic_dtype.py +0 -0
- {format_input-0.2.0 → format_input-0.3.0}/tests/test_b_basic_dtype_list.py +0 -0
- {format_input-0.2.0 → format_input-0.3.0}/tests/test_c_basic_dtype_list_len.py +0 -0
- {format_input-0.2.0 → format_input-0.3.0}/tests/test_d_basic_dtype_greedy.py +0 -0
- {format_input-0.2.0 → format_input-0.3.0}/tests/test_e_named_dtype.py +0 -0
- {format_input-0.2.0 → format_input-0.3.0}/tests/test_f_extra.py +0 -0
- {format_input-0.2.0 → format_input-0.3.0}/tests/test_mix_abcd.py +0 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: format-input
|
|
3
|
+
Version: 0.3.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
|
+
Requires-Dist: regex>=2026.1.15
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
|
|
17
|
+
# Format Input
|
|
18
|
+
|
|
19
|
+
`Format Input` is a small Python library for parsing console or text-stream input
|
|
20
|
+
according to a compact format specification. Instead of repeatedly calling
|
|
21
|
+
`input()`, splitting strings, and converting values by hand, describe the shape
|
|
22
|
+
of each input line once and receive typed Python values.
|
|
23
|
+
|
|
24
|
+
It is useful for:
|
|
25
|
+
|
|
26
|
+
- Competitive programming problems with predictable, line-based input.
|
|
27
|
+
- Command-line tools that read structured text.
|
|
28
|
+
- Small scripts that need to convert input into integers, floats, and strings.
|
|
29
|
+
- Situations where input may contain a mixture of scalar values, lists, and
|
|
30
|
+
values followed by free-form text.
|
|
31
|
+
|
|
32
|
+
For example, instead of manually splitting and converting a line:
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
parts = input().split()
|
|
36
|
+
count = int(parts[0])
|
|
37
|
+
ratio = float(parts[1])
|
|
38
|
+
name = parts[2]
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
You can describe the input once and receive typed values directly:
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
count, ratio, name = format_input("int float str")
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
See [USAGE.MD](./USAGE.MD) for all supported types and format specifications.
|
|
48
|
+
|
|
49
|
+
## Requirements
|
|
50
|
+
|
|
51
|
+
- Python 3.10 or newer
|
|
52
|
+
- `regex` 2026.1.15 or newer
|
|
53
|
+
|
|
54
|
+
## Installation
|
|
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
|
+
|
|
64
|
+
Clone the repository:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
git clone https://github.com/GabbaTK/Format-Input.git
|
|
68
|
+
cd Format-Input
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The package is stored in `src/`.
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
To run the test suite from the repository root:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
PYTHONPATH=src python -m unittest discover -s tests -v
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## License
|
|
81
|
+
|
|
82
|
+
This project is available under the [MIT License](./LICENSE).
|
|
83
|
+
|
|
84
|
+
## AI disclosure
|
|
85
|
+
|
|
86
|
+
No AI was used in the making of the code.
|
|
@@ -0,0 +1,70 @@
|
|
|
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
|
+
- `regex` 2026.1.15 or newer
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
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
|
+
|
|
48
|
+
Clone the repository:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
git clone https://github.com/GabbaTK/Format-Input.git
|
|
52
|
+
cd Format-Input
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The package is stored in `src/`.
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
To run the test suite from the repository root:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
PYTHONPATH=src python -m unittest discover -s tests -v
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## License
|
|
65
|
+
|
|
66
|
+
This project is available under the [MIT License](./LICENSE).
|
|
67
|
+
|
|
68
|
+
## AI disclosure
|
|
69
|
+
|
|
70
|
+
No AI was used in the making of the code.
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "format-input"
|
|
3
|
-
version = "0.
|
|
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",
|
|
@@ -1,23 +1,26 @@
|
|
|
1
1
|
import typing
|
|
2
|
-
import re
|
|
2
|
+
import regex as re
|
|
3
3
|
import sys
|
|
4
4
|
from .helpers import *
|
|
5
5
|
from .handlers import *
|
|
6
6
|
from collections import deque
|
|
7
7
|
|
|
8
8
|
NAME_PREFIX = r"(?<![a-z_:])[a-z_]+:"
|
|
9
|
-
RE_NEW_LINE = re.compile(r";(?![^[\]]*\])")
|
|
9
|
+
#RE_NEW_LINE = re.compile(r";(?![^[\]]*\])")
|
|
10
|
+
RE_NEW_LINE = re.compile(r";(?![^\[\]]*(?:\[[^\[\]]*\][^\[\]]*)*\])")
|
|
10
11
|
RE_LINE_MATCH = re.compile(
|
|
11
|
-
rf"{NAME_PREFIX}(?P<
|
|
12
|
-
|
|
13
|
-
rf"|{NAME_PREFIX}(?P<
|
|
14
|
-
rf"|{NAME_PREFIX}(?P<
|
|
15
|
-
rf"|{NAME_PREFIX}(?P<
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
r"
|
|
19
|
-
r"|\b(?P<
|
|
20
|
-
r"|\b(?P<
|
|
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
|
|
21
24
|
)
|
|
22
25
|
|
|
23
26
|
|
|
@@ -27,24 +30,23 @@ def readline(stream: typing.TextIO) -> str:
|
|
|
27
30
|
return line.rstrip("\r\n")
|
|
28
31
|
|
|
29
32
|
|
|
30
|
-
def format_input(spec: str, stream: typing.TextIO | None = None, named_variables: bool = False) -> list[typing.Any] | dict[str, typing.Any]:
|
|
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]:
|
|
31
34
|
if stream is None: stream = sys.stdin
|
|
32
35
|
|
|
33
36
|
output = {} if named_variables else []
|
|
34
|
-
inp_queue: deque[str] = deque()
|
|
37
|
+
inp_queue: deque[str] = _input_queue if _input_queue is not None else deque()
|
|
35
38
|
lines = RE_NEW_LINE.split(spec)
|
|
36
39
|
|
|
37
40
|
for line in lines:
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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)
|
|
42
45
|
|
|
43
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")
|
|
44
48
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if "named" in m.lastgroup: var, dt = m.group().split(":")
|
|
49
|
+
if "named" in m.lastgroup: var, dt = m.group().split(":", 1)
|
|
48
50
|
else: dt = m.group()
|
|
49
51
|
|
|
50
52
|
match (m.lastgroup):
|
|
@@ -58,9 +60,11 @@ def format_input(spec: str, stream: typing.TextIO | None = None, named_variables
|
|
|
58
60
|
case "type_list": ret = handle_type_list(dt, inp_queue, max_size=-1)
|
|
59
61
|
case "named_type": ret = handle_type(dt, inp_queue)
|
|
60
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)
|
|
61
65
|
|
|
62
66
|
if "named" in m.lastgroup: ret = {var: ret}
|
|
63
|
-
|
|
67
|
+
|
|
64
68
|
if isinstance(ret, dict) and not named_variables:
|
|
65
69
|
output.extend([ v for v in ret.values() ])
|
|
66
70
|
elif isinstance(ret, dict) and named_variables:
|
|
@@ -68,6 +72,8 @@ def format_input(spec: str, stream: typing.TextIO | None = None, named_variables
|
|
|
68
72
|
elif not isinstance(ret, dict) and not named_variables:
|
|
69
73
|
output.append(ret)
|
|
70
74
|
else:
|
|
71
|
-
raise
|
|
75
|
+
raise Exception("Unexpected error.")
|
|
76
|
+
|
|
77
|
+
inp_queue.clear()
|
|
72
78
|
|
|
73
79
|
return output
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: format-input
|
|
3
|
+
Version: 0.3.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
|
+
Requires-Dist: regex>=2026.1.15
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
|
|
17
|
+
# Format Input
|
|
18
|
+
|
|
19
|
+
`Format Input` is a small Python library for parsing console or text-stream input
|
|
20
|
+
according to a compact format specification. Instead of repeatedly calling
|
|
21
|
+
`input()`, splitting strings, and converting values by hand, describe the shape
|
|
22
|
+
of each input line once and receive typed Python values.
|
|
23
|
+
|
|
24
|
+
It is useful for:
|
|
25
|
+
|
|
26
|
+
- Competitive programming problems with predictable, line-based input.
|
|
27
|
+
- Command-line tools that read structured text.
|
|
28
|
+
- Small scripts that need to convert input into integers, floats, and strings.
|
|
29
|
+
- Situations where input may contain a mixture of scalar values, lists, and
|
|
30
|
+
values followed by free-form text.
|
|
31
|
+
|
|
32
|
+
For example, instead of manually splitting and converting a line:
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
parts = input().split()
|
|
36
|
+
count = int(parts[0])
|
|
37
|
+
ratio = float(parts[1])
|
|
38
|
+
name = parts[2]
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
You can describe the input once and receive typed values directly:
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
count, ratio, name = format_input("int float str")
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
See [USAGE.MD](./USAGE.MD) for all supported types and format specifications.
|
|
48
|
+
|
|
49
|
+
## Requirements
|
|
50
|
+
|
|
51
|
+
- Python 3.10 or newer
|
|
52
|
+
- `regex` 2026.1.15 or newer
|
|
53
|
+
|
|
54
|
+
## Installation
|
|
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
|
+
|
|
64
|
+
Clone the repository:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
git clone https://github.com/GabbaTK/Format-Input.git
|
|
68
|
+
cd Format-Input
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The package is stored in `src/`.
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
To run the test suite from the repository root:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
PYTHONPATH=src python -m unittest discover -s tests -v
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## License
|
|
81
|
+
|
|
82
|
+
This project is available under the [MIT License](./LICENSE).
|
|
83
|
+
|
|
84
|
+
## AI disclosure
|
|
85
|
+
|
|
86
|
+
No AI was used in the making of the code.
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
LICENSE
|
|
2
|
+
README.MD
|
|
2
3
|
pyproject.toml
|
|
3
4
|
src/format_input/__init__.py
|
|
4
5
|
src/format_input/format_input.py
|
|
@@ -7,6 +8,7 @@ src/format_input/helpers.py
|
|
|
7
8
|
src/format_input.egg-info/PKG-INFO
|
|
8
9
|
src/format_input.egg-info/SOURCES.txt
|
|
9
10
|
src/format_input.egg-info/dependency_links.txt
|
|
11
|
+
src/format_input.egg-info/requires.txt
|
|
10
12
|
src/format_input.egg-info/top_level.txt
|
|
11
13
|
tests/test_000_edgecase.py
|
|
12
14
|
tests/test_a_basic_dtype.py
|
|
@@ -15,4 +17,5 @@ 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
|
|
17
19
|
tests/test_f_extra.py
|
|
20
|
+
tests/test_g_groups.py
|
|
18
21
|
tests/test_mix_abcd.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
regex>=2026.1.15
|
|
@@ -74,6 +74,54 @@ class Test000Edgecase(unittest.TestCase):
|
|
|
74
74
|
source = io.StringIO("42")
|
|
75
75
|
self.assertEqual(format_input("count:int extra:?", source, named_variables=True), {"count": 42, "extra": ""})
|
|
76
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
|
+
|
|
77
125
|
|
|
78
126
|
if __name__ == "__main__":
|
|
79
127
|
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()
|
format_input-0.2.0/PKG-INFO
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: format-input
|
|
3
|
-
Version: 0.2.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
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: format-input
|
|
3
|
-
Version: 0.2.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
|
|
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
|