string-to-code 0.2.4__py3-none-any.whl → 0.2.6__py3-none-any.whl
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.
- string_to_code/core.py +31 -17
- {string_to_code-0.2.4.dist-info → string_to_code-0.2.6.dist-info}/METADATA +1 -1
- {string_to_code-0.2.4.dist-info → string_to_code-0.2.6.dist-info}/RECORD +5 -5
- {string_to_code-0.2.4.dist-info → string_to_code-0.2.6.dist-info}/LICENSE +0 -0
- {string_to_code-0.2.4.dist-info → string_to_code-0.2.6.dist-info}/WHEEL +0 -0
string_to_code/core.py
CHANGED
|
@@ -2,9 +2,12 @@
|
|
|
2
2
|
core functions of the string_to_code module
|
|
3
3
|
"""
|
|
4
4
|
import typing
|
|
5
|
+
import collections.abc
|
|
5
6
|
import random
|
|
6
7
|
import functools
|
|
7
8
|
|
|
9
|
+
Strings: typing.TypeAlias = typing.List[str]
|
|
10
|
+
|
|
8
11
|
|
|
9
12
|
class Atom(typing.NamedTuple):
|
|
10
13
|
"""represents a single character to be printed"""
|
|
@@ -12,16 +15,23 @@ class Atom(typing.NamedTuple):
|
|
|
12
15
|
atom_char: str
|
|
13
16
|
|
|
14
17
|
|
|
18
|
+
CalledListEntry: typing.TypeAlias = int | Atom
|
|
19
|
+
InitialCall: typing.TypeAlias = CalledListEntry | None
|
|
20
|
+
|
|
21
|
+
|
|
15
22
|
class SimpleFunction(typing.NamedTuple):
|
|
16
23
|
"""
|
|
17
24
|
represents a function with no arguments and no return value calling
|
|
18
25
|
other functions of such type or displaying single characters
|
|
19
26
|
"""
|
|
20
27
|
|
|
21
|
-
called_list: typing.List[
|
|
28
|
+
called_list: typing.List[CalledListEntry]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
SimpleFunctions: typing.TypeAlias = typing.List[SimpleFunction]
|
|
22
32
|
|
|
23
33
|
|
|
24
|
-
def str_pieces(in_str, in_pieces_len):
|
|
34
|
+
def str_pieces(in_str: str, in_pieces_len: typing.List[int]) -> Strings:
|
|
25
35
|
"""returns in_str splited into pieces of lengths as in in_pieces_len"""
|
|
26
36
|
assert all(_ > 0 for _ in in_pieces_len)
|
|
27
37
|
assert sum(in_pieces_len) == len(in_str)
|
|
@@ -34,8 +44,8 @@ def str_pieces(in_str, in_pieces_len):
|
|
|
34
44
|
return res
|
|
35
45
|
|
|
36
46
|
|
|
37
|
-
def random_pieces_len(in_total_len):
|
|
38
|
-
"""returns a list of positive numbers
|
|
47
|
+
def random_pieces_len(in_total_len: int) -> typing.List[int]:
|
|
48
|
+
"""returns a list of positive numbers with their sum being in_total_len"""
|
|
39
49
|
cur_num = in_total_len
|
|
40
50
|
res = []
|
|
41
51
|
while cur_num > 0:
|
|
@@ -47,12 +57,12 @@ def random_pieces_len(in_total_len):
|
|
|
47
57
|
return res
|
|
48
58
|
|
|
49
59
|
|
|
50
|
-
def random_split(in_str):
|
|
60
|
+
def random_split(in_str: str) -> Strings:
|
|
51
61
|
"""randomply splits in_str"""
|
|
52
62
|
return str_pieces(in_str, random_pieces_len(len(in_str)))
|
|
53
63
|
|
|
54
64
|
|
|
55
|
-
def _interesting_random_split(in_str):
|
|
65
|
+
def _interesting_random_split(in_str: str) -> Strings:
|
|
56
66
|
assert len(in_str) > 1
|
|
57
67
|
res = random_split(in_str)
|
|
58
68
|
while len(res) == 1:
|
|
@@ -60,10 +70,12 @@ def _interesting_random_split(in_str):
|
|
|
60
70
|
return res
|
|
61
71
|
|
|
62
72
|
|
|
63
|
-
def get_function_namer(
|
|
73
|
+
def get_function_namer(
|
|
74
|
+
in_name_prefix: str = "f_",
|
|
75
|
+
) -> collections.abc.Callable[[int], str]:
|
|
64
76
|
"""returns a defult function namer"""
|
|
65
77
|
|
|
66
|
-
def _inner(in_id):
|
|
78
|
+
def _inner(in_id: int) -> str:
|
|
67
79
|
return in_name_prefix + str(in_id)
|
|
68
80
|
|
|
69
81
|
return _inner
|
|
@@ -71,11 +83,11 @@ def get_function_namer(in_name_prefix="f_"):
|
|
|
71
83
|
|
|
72
84
|
def _prepare_printer_program(
|
|
73
85
|
in_str: str,
|
|
74
|
-
) -> typing.Tuple[
|
|
75
|
-
needed_functions:
|
|
86
|
+
) -> typing.Tuple[InitialCall, SimpleFunctions]:
|
|
87
|
+
needed_functions: SimpleFunctions = []
|
|
76
88
|
|
|
77
89
|
@functools.lru_cache(maxsize=None)
|
|
78
|
-
def _generate_code(in_str: str) ->
|
|
90
|
+
def _generate_code(in_str: str) -> CalledListEntry:
|
|
79
91
|
if len(in_str) == 1:
|
|
80
92
|
return Atom(in_str)
|
|
81
93
|
|
|
@@ -94,13 +106,13 @@ class PrinterProgram:
|
|
|
94
106
|
It consists only of SimpleFunctions and Atoms.
|
|
95
107
|
"""
|
|
96
108
|
|
|
97
|
-
def __init__(self, initial_call, needed_functions):
|
|
109
|
+
def __init__(self, initial_call: InitialCall, needed_functions: SimpleFunctions):
|
|
98
110
|
self._initial_call = initial_call
|
|
99
111
|
self._needed_functions = needed_functions
|
|
100
112
|
|
|
101
113
|
self._check_data()
|
|
102
114
|
|
|
103
|
-
def _check_data(self):
|
|
115
|
+
def _check_data(self) -> None:
|
|
104
116
|
if self.initial_call is not None and not isinstance(self.initial_call, Atom):
|
|
105
117
|
assert self.needed_functions
|
|
106
118
|
if self.needed_functions:
|
|
@@ -110,7 +122,9 @@ class PrinterProgram:
|
|
|
110
122
|
for fun_id, fun in enumerate(self.needed_functions):
|
|
111
123
|
assert all(_ < fun_id for _ in fun.called_list if not isinstance(_, Atom))
|
|
112
124
|
|
|
113
|
-
def needed_function_definitions_str_list(
|
|
125
|
+
def needed_function_definitions_str_list(
|
|
126
|
+
self, in_function_to_str, **kwargs
|
|
127
|
+
) -> Strings:
|
|
114
128
|
"""
|
|
115
129
|
returns a list of string representations of the definition of
|
|
116
130
|
the needed functions
|
|
@@ -121,16 +135,16 @@ class PrinterProgram:
|
|
|
121
135
|
]
|
|
122
136
|
|
|
123
137
|
@property
|
|
124
|
-
def initial_call(self):
|
|
138
|
+
def initial_call(self) -> InitialCall:
|
|
125
139
|
"""returns the 'entry point' of the program"""
|
|
126
140
|
return self._initial_call
|
|
127
141
|
|
|
128
142
|
@property
|
|
129
|
-
def needed_functions(self):
|
|
143
|
+
def needed_functions(self) -> SimpleFunctions:
|
|
130
144
|
"""returns the list of all needed functions"""
|
|
131
145
|
return self._needed_functions
|
|
132
146
|
|
|
133
147
|
|
|
134
|
-
def get_printer_program(in_str):
|
|
148
|
+
def get_printer_program(in_str: str) -> PrinterProgram:
|
|
135
149
|
"""returns a PrinterProgram object diplaying in_str"""
|
|
136
150
|
return PrinterProgram(*_prepare_printer_program(in_str))
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
string_to_code/README.md,sha256=A8BkDQpr-Os630YfGnaU-i_kigyVB8bNLhvLfIyzQR8,1225
|
|
2
2
|
string_to_code/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
3
|
string_to_code/c_like_utils.py,sha256=G2HMxU6J9Lczv28yTNJSXLAeTyOHY1RV6gKltvf1h1E,2091
|
|
4
|
-
string_to_code/core.py,sha256=
|
|
4
|
+
string_to_code/core.py,sha256=oZg32ZUt_vSi1d58I-5aWd-ecXuQ8BUMOoWuozTRVUA,4350
|
|
5
5
|
string_to_code/string_to_code.py,sha256=ldAczke3qmY4UnodSP6oaq-Gu89IOubZAXUDflpo61k,1212
|
|
6
6
|
string_to_code/to_ada.py,sha256=-QUNxghg4KcZzqya0YgBoHJnFA2aYwnjw28Qec-EqJc,2264
|
|
7
7
|
string_to_code/to_algol68.py,sha256=ENxJbUKIL-iOXz3QrhXdGVUhrv5b_Ogjt_1NLrYbZjY,1641
|
|
@@ -18,7 +18,7 @@ string_to_code/to_pascal.py,sha256=EQ1wUQ-jz5frIB0_OQ3h2v8BDitzKFhVsey_xlX3XMg,1
|
|
|
18
18
|
string_to_code/to_python3.py,sha256=ZLU29deSg-rsfR5HmlB5ZCH2pURGmGFdNDfcIZ_DdNU,1588
|
|
19
19
|
string_to_code/to_rust.py,sha256=xbkKKhvFxbOBtSgh-5_hGJ8zSBDt4vNIqEiJW9JsoXw,1925
|
|
20
20
|
string_to_code/utils.py,sha256=P3qkuOtsZbhejTnouIUuz4qM_tvNrWBkzrX8NOztmok,3368
|
|
21
|
-
string_to_code-0.2.
|
|
22
|
-
string_to_code-0.2.
|
|
23
|
-
string_to_code-0.2.
|
|
24
|
-
string_to_code-0.2.
|
|
21
|
+
string_to_code-0.2.6.dist-info/LICENSE,sha256=LQrcSkcUyN1FtFYDxWK05puxNN9o2q8PV-_2SwFDTWc,1068
|
|
22
|
+
string_to_code-0.2.6.dist-info/METADATA,sha256=PGNiafyvbMWIseFzPZe2LQxYYxeKQcPvnLoICA2M9NI,1849
|
|
23
|
+
string_to_code-0.2.6.dist-info/WHEEL,sha256=d2fvjOD7sXsVzChCqf0Ty0JbHKBaLYwDbGQDwQTnJ50,88
|
|
24
|
+
string_to_code-0.2.6.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|