3
3
Classes:
4
4
IO: IO tool class. It will process the input and output files.
5
5
"""
6
+
6
7
from __future__ import absolute_import
7
8
import os
8
9
import re
@@ -18,34 +19,37 @@ class IO:
18
19
"""IO tool class. It will process the input and output files."""
19
20
20
21
@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
+ ): ...
28
30
29
31
@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
+ ): ...
38
41
39
42
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
+ ):
49
53
"""
50
54
Args:
51
55
input_file (optional): input file object or filename or file descriptor.
@@ -87,11 +91,12 @@ def __init__( # type: ignore
87
91
self .input_file , self .output_file = None , None
88
92
if file_prefix is not None :
89
93
# 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
+ )
95
100
self .input_filename , self .output_filename = None , None
96
101
self .__input_temp , self .__output_temp = False , False
97
102
self .__init_file (input_file , data_id , "i" , make_dirs )
@@ -101,9 +106,13 @@ def __init__( # type: ignore
101
106
self .output_file = None
102
107
self .is_first_char = {}
103
108
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
+ ):
107
116
if isinstance (f , IOBase ):
108
117
# consider ``f`` as a file object
109
118
if file_type == "i" :
@@ -112,8 +121,12 @@ def __init_file(self, f: Union[IOBase, str, int,
112
121
self .output_file = f
113
122
elif isinstance (f , int ):
114
123
# 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
+ )
117
130
elif f is None :
118
131
# consider wanna temp file
119
132
fd , self .input_filename = tempfile .mkstemp ()
@@ -133,8 +146,11 @@ def __init_file(self, f: Union[IOBase, str, int,
133
146
else :
134
147
self .output_filename = filename
135
148
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
+ )
138
154
139
155
def __escape_format (self , st : str ):
140
156
"""replace "{}" to "{{}}" """
@@ -266,28 +282,26 @@ def output_gen(
266
282
origin_pos = self .input_file .tell ()
267
283
self .input_file .seek (0 )
268
284
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 (
275
286
shell_cmd ,
276
287
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 ,
281
291
)
282
- self .input_file .seek (origin_pos )
283
292
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 )
291
305
292
306
log .debug (self .output_filename , " done" )
293
307
0 commit comments