format-input 0.1.0__tar.gz → 0.2.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.2.0/PKG-INFO +14 -0
- {format_input-0.1.0 → format_input-0.2.0}/pyproject.toml +2 -2
- {format_input-0.1.0 → format_input-0.2.0}/src/format_input/format_input.py +22 -11
- format_input-0.2.0/src/format_input/handlers.py +84 -0
- {format_input-0.1.0 → format_input-0.2.0}/src/format_input/helpers.py +4 -0
- format_input-0.2.0/src/format_input.egg-info/PKG-INFO +14 -0
- {format_input-0.1.0 → format_input-0.2.0}/src/format_input.egg-info/SOURCES.txt +2 -1
- format_input-0.2.0/tests/test_000_edgecase.py +79 -0
- {format_input-0.1.0 → format_input-0.2.0}/tests/test_a_basic_dtype.py +2 -1
- format_input-0.2.0/tests/test_f_extra.py +42 -0
- format_input-0.1.0/PKG-INFO +0 -74
- format_input-0.1.0/README.md +0 -59
- format_input-0.1.0/src/format_input/handlers.py +0 -146
- format_input-0.1.0/src/format_input.egg-info/PKG-INFO +0 -74
- {format_input-0.1.0 → format_input-0.2.0}/LICENSE +0 -0
- {format_input-0.1.0 → format_input-0.2.0}/setup.cfg +0 -0
- {format_input-0.1.0 → format_input-0.2.0}/src/format_input/__init__.py +0 -0
- {format_input-0.1.0 → format_input-0.2.0}/src/format_input.egg-info/dependency_links.txt +0 -0
- {format_input-0.1.0 → format_input-0.2.0}/src/format_input.egg-info/top_level.txt +0 -0
- {format_input-0.1.0 → format_input-0.2.0}/tests/test_b_basic_dtype_list.py +0 -0
- {format_input-0.1.0 → format_input-0.2.0}/tests/test_c_basic_dtype_list_len.py +0 -0
- {format_input-0.1.0 → format_input-0.2.0}/tests/test_d_basic_dtype_greedy.py +0 -0
- {format_input-0.1.0 → format_input-0.2.0}/tests/test_e_named_dtype.py +0 -0
- {format_input-0.1.0 → format_input-0.2.0}/tests/test_mix_abcd.py +0 -0
|
@@ -0,0 +1,14 @@
|
|
|
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
|
|
@@ -8,10 +8,12 @@ from collections import deque
|
|
|
8
8
|
NAME_PREFIX = r"(?<![a-z_:])[a-z_]+:"
|
|
9
9
|
RE_NEW_LINE = re.compile(r";(?![^[\]]*\])")
|
|
10
10
|
RE_LINE_MATCH = re.compile(
|
|
11
|
-
rf"{NAME_PREFIX}(?P<
|
|
11
|
+
rf"{NAME_PREFIX}(?P<named_extra>\?)(?![\w?])" # e:?
|
|
12
|
+
rf"|{NAME_PREFIX}(?P<named_type_greedy>str\*)(?![\w*])" # t:str*
|
|
12
13
|
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
14
|
rf"|{NAME_PREFIX}(?P<named_type_list>(?:int|str|float)\[\])(?![\w\[])" # n:int[] f:float[] t:str[]
|
|
14
15
|
rf"|{NAME_PREFIX}(?P<named_type>(?:int|str|float))\b" # n:int f:float t:str
|
|
16
|
+
r"|(?<![\w:])(?P<extra>\?)(?![\w?])" # ?
|
|
15
17
|
r"|\b(?P<type_greedy>str\*)(?![\w*])" # str*
|
|
16
18
|
r"|\b(?P<type_len_list>(?:int|float|str)\[[1-9]\d*\])(?![\w\[])" # int[5] float[7] str[1]
|
|
17
19
|
r"|\b(?P<type_list>(?:int|str|float)\[\])(?![\w\[])" # int[] float[] str[]
|
|
@@ -21,7 +23,7 @@ RE_LINE_MATCH = re.compile(
|
|
|
21
23
|
|
|
22
24
|
def readline(stream: typing.TextIO) -> str:
|
|
23
25
|
line = stream.readline()
|
|
24
|
-
if line == "":
|
|
26
|
+
#if line == "": raise EOFError()
|
|
25
27
|
return line.rstrip("\r\n")
|
|
26
28
|
|
|
27
29
|
|
|
@@ -39,16 +41,25 @@ def format_input(spec: str, stream: typing.TextIO | None = None, named_variables
|
|
|
39
41
|
inp_queue.extend(inps)
|
|
40
42
|
|
|
41
43
|
for m in RE_LINE_MATCH.finditer(line):
|
|
42
|
-
|
|
44
|
+
|
|
45
|
+
#if len(inp_queue) == 0: raise EOFError("Tried to parse an empty input queue.")
|
|
46
|
+
|
|
47
|
+
if "named" in m.lastgroup: var, dt = m.group().split(":")
|
|
48
|
+
else: dt = m.group()
|
|
49
|
+
|
|
43
50
|
match (m.lastgroup):
|
|
44
|
-
case "
|
|
45
|
-
case "
|
|
46
|
-
case "
|
|
47
|
-
case "
|
|
48
|
-
case "
|
|
49
|
-
case "
|
|
50
|
-
case "
|
|
51
|
-
case "
|
|
51
|
+
case "named_extra": ret = handle_extra(inp_queue)
|
|
52
|
+
case "extra": ret = handle_extra(inp_queue)
|
|
53
|
+
case "named_type_greedy": ret = handle_type_greedy(inp_queue)
|
|
54
|
+
case "type_greedy": ret = handle_type_greedy(inp_queue)
|
|
55
|
+
case "named_type_len_list": ret = handle_type_list(dt, inp_queue, max_size=int(m.group().split("[", 1)[1][:-1]))
|
|
56
|
+
case "type_len_list": ret = handle_type_list(dt, inp_queue, max_size=int(m.group().split("[", 1)[1][:-1]))
|
|
57
|
+
case "named_type_list": ret = handle_type_list(dt, inp_queue, max_size=-1)
|
|
58
|
+
case "type_list": ret = handle_type_list(dt, inp_queue, max_size=-1)
|
|
59
|
+
case "named_type": ret = handle_type(dt, inp_queue)
|
|
60
|
+
case "type": ret = handle_type(dt, inp_queue)
|
|
61
|
+
|
|
62
|
+
if "named" in m.lastgroup: ret = {var: ret}
|
|
52
63
|
|
|
53
64
|
if isinstance(ret, dict) and not named_variables:
|
|
54
65
|
output.extend([ v for v in ret.values() ])
|
|
@@ -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
|
|
@@ -0,0 +1,14 @@
|
|
|
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,5 +1,4 @@
|
|
|
1
1
|
LICENSE
|
|
2
|
-
README.md
|
|
3
2
|
pyproject.toml
|
|
4
3
|
src/format_input/__init__.py
|
|
5
4
|
src/format_input/format_input.py
|
|
@@ -9,9 +8,11 @@ src/format_input.egg-info/PKG-INFO
|
|
|
9
8
|
src/format_input.egg-info/SOURCES.txt
|
|
10
9
|
src/format_input.egg-info/dependency_links.txt
|
|
11
10
|
src/format_input.egg-info/top_level.txt
|
|
11
|
+
tests/test_000_edgecase.py
|
|
12
12
|
tests/test_a_basic_dtype.py
|
|
13
13
|
tests/test_b_basic_dtype_list.py
|
|
14
14
|
tests/test_c_basic_dtype_list_len.py
|
|
15
15
|
tests/test_d_basic_dtype_greedy.py
|
|
16
16
|
tests/test_e_named_dtype.py
|
|
17
|
+
tests/test_f_extra.py
|
|
17
18
|
tests/test_mix_abcd.py
|
|
@@ -0,0 +1,79 @@
|
|
|
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
|
+
|
|
78
|
+
if __name__ == "__main__":
|
|
79
|
+
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()
|
format_input-0.1.0/PKG-INFO
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
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.
|
format_input-0.1.0/README.md
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
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.
|
|
@@ -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
|
|
@@ -1,74 +0,0 @@
|
|
|
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.
|
|
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
|