Skip to content

Commit 0942353

Browse files
committed
Rewrite output_gen
1 parent d814892 commit 0942353

File tree

2 files changed

+69
-54
lines changed

2 files changed

+69
-54
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
config.py
2+
*.cpp
23
*.in
34
*.out
45
*.exe

cyaron/io.py

Lines changed: 68 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Classes:
44
IO: IO tool class. It will process the input and output files.
55
"""
6+
67
from __future__ import absolute_import
78
import os
89
import re
@@ -18,34 +19,37 @@ class IO:
1819
"""IO tool class. It will process the input and output files."""
1920

2021
@overload
21-
def __init__(self,
22-
input_file: Optional[Union[IOBase, str, int]] = None,
23-
output_file: Optional[Union[IOBase, str, int]] = None,
24-
data_id: Optional[int] = None,
25-
disable_output: bool = False,
26-
make_dirs: bool = False):
27-
...
22+
def __init__(
23+
self,
24+
input_file: Optional[Union[IOBase, str, int]] = None,
25+
output_file: Optional[Union[IOBase, str, int]] = None,
26+
data_id: Optional[int] = None,
27+
disable_output: bool = False,
28+
make_dirs: bool = False,
29+
): ...
2830

2931
@overload
30-
def __init__(self,
31-
data_id: Optional[int] = None,
32-
file_prefix: Optional[str] = None,
33-
input_suffix: str = '.in',
34-
output_suffix: str = '.out',
35-
disable_output: bool = False,
36-
make_dirs: bool = False):
37-
...
32+
def __init__(
33+
self,
34+
data_id: Optional[int] = None,
35+
file_prefix: Optional[str] = None,
36+
input_suffix: str = ".in",
37+
output_suffix: str = ".out",
38+
disable_output: bool = False,
39+
make_dirs: bool = False,
40+
): ...
3841

3942
def __init__( # type: ignore
40-
self,
41-
input_file: Optional[Union[IOBase, str, int]] = None,
42-
output_file: Optional[Union[IOBase, str, int]] = None,
43-
data_id: Optional[int] = None,
44-
file_prefix: Optional[str] = None,
45-
input_suffix: str = '.in',
46-
output_suffix: str = '.out',
47-
disable_output: bool = False,
48-
make_dirs: bool = False):
43+
self,
44+
input_file: Optional[Union[IOBase, str, int]] = None,
45+
output_file: Optional[Union[IOBase, str, int]] = None,
46+
data_id: Optional[int] = None,
47+
file_prefix: Optional[str] = None,
48+
input_suffix: str = ".in",
49+
output_suffix: str = ".out",
50+
disable_output: bool = False,
51+
make_dirs: bool = False,
52+
):
4953
"""
5054
Args:
5155
input_file (optional): input file object or filename or file descriptor.
@@ -87,11 +91,12 @@ def __init__( # type: ignore
8791
self.input_file, self.output_file = None, None
8892
if file_prefix is not None:
8993
# legacy mode
90-
input_file = '{}{{}}{}'.format(self.__escape_format(file_prefix),
91-
self.__escape_format(input_suffix))
92-
output_file = '{}{{}}{}'.format(
93-
self.__escape_format(file_prefix),
94-
self.__escape_format(output_suffix))
94+
input_file = "{}{{}}{}".format(
95+
self.__escape_format(file_prefix), self.__escape_format(input_suffix)
96+
)
97+
output_file = "{}{{}}{}".format(
98+
self.__escape_format(file_prefix), self.__escape_format(output_suffix)
99+
)
95100
self.input_filename, self.output_filename = None, None
96101
self.__input_temp, self.__output_temp = False, False
97102
self.__init_file(input_file, data_id, "i", make_dirs)
@@ -101,9 +106,13 @@ def __init__( # type: ignore
101106
self.output_file = None
102107
self.is_first_char = {}
103108

104-
def __init_file(self, f: Union[IOBase, str, int,
105-
None], data_id: Union[int, None],
106-
file_type: str, make_dirs: bool):
109+
def __init_file(
110+
self,
111+
f: Union[IOBase, str, int, None],
112+
data_id: Union[int, None],
113+
file_type: str,
114+
make_dirs: bool,
115+
):
107116
if isinstance(f, IOBase):
108117
# consider ``f`` as a file object
109118
if file_type == "i":
@@ -112,8 +121,12 @@ def __init_file(self, f: Union[IOBase, str, int,
112121
self.output_file = f
113122
elif isinstance(f, int):
114123
# consider ``f`` as a file descor
115-
self.__init_file(open(f, 'w+', encoding="utf-8", newline='\n'),
116-
data_id, file_type, make_dirs)
124+
self.__init_file(
125+
open(f, "w+", encoding="utf-8", newline="\n"),
126+
data_id,
127+
file_type,
128+
make_dirs,
129+
)
117130
elif f is None:
118131
# consider wanna temp file
119132
fd, self.input_filename = tempfile.mkstemp()
@@ -133,8 +146,11 @@ def __init_file(self, f: Union[IOBase, str, int,
133146
else:
134147
self.output_filename = filename
135148
self.__init_file(
136-
open(filename, 'w+', newline='\n', encoding='utf-8'), data_id,
137-
file_type, make_dirs)
149+
open(filename, "w+", newline="\n", encoding="utf-8"),
150+
data_id,
151+
file_type,
152+
make_dirs,
153+
)
138154

139155
def __escape_format(self, st: str):
140156
"""replace "{}" to "{{}}" """
@@ -266,28 +282,26 @@ def output_gen(
266282
origin_pos = self.input_file.tell()
267283
self.input_file.seek(0)
268284

269-
output_fileno = self.output_file.fileno()
270-
if replace_EOL:
271-
temp_outfile = tempfile.TemporaryFile("w+")
272-
output_fileno = temp_outfile.fileno()
273-
274-
subprocess.check_call(
285+
proc = subprocess.Popen(
275286
shell_cmd,
276287
shell=True,
277-
timeout=time_limit,
278-
stdin=self.input_file.fileno(),
279-
stdout=output_fileno,
280-
universal_newlines=True,
288+
stdin=self.input_file,
289+
stdout=subprocess.PIPE,
290+
universal_newlines=replace_EOL,
281291
)
282-
self.input_file.seek(origin_pos)
283292

284-
if replace_EOL:
285-
temp_outfile.seek(0)
286-
buf = temp_outfile.read(65536)
287-
while buf != "":
288-
self.output_file.write(buf)
289-
buf = temp_outfile.read(65536)
290-
temp_outfile.close()
293+
try:
294+
output, _ = proc.communicate(timeout=time_limit)
295+
except subprocess.TimeoutExpired:
296+
proc.kill()
297+
raise
298+
else:
299+
if replace_EOL:
300+
self.output_file.write(output)
301+
else:
302+
os.write(self.output_file.fileno(), output)
303+
finally:
304+
self.input_file.seek(origin_pos)
291305

292306
log.debug(self.output_filename, " done")
293307

0 commit comments

Comments
 (0)