@@ -57,11 +57,26 @@ def __del__(self):
5757 except Exception :
5858 pass
5959
60+ def __enter__ (self ):
61+ return self
62+
63+ def __exit__ (self , exc_type , exc_val , exc_tb ):
64+ """__del__(self) -> None
65+ Exit the context of the IO object and close the input file and the output file
66+ """
67+ try :
68+ self .input_file .close ()
69+ self .output_file .close ()
70+ except Exception :
71+ pass
72+
6073 @staticmethod
6174 def __write (file , * args , ** kwargs ):
62- """__write(file, *args) -> None
75+ """__write(file, *args, **kwargs ) -> None
6376 Write every element in *args into file. If the element isn't "\n ", insert a space. It will convert every element into str
6477 file file -> the file object to write
78+ **kwargs:
79+ str separator = " " -> a string used to separate every element
6580 """
6681 separator = kwargs .get ("separator" , " " )
6782 for arg in args :
@@ -73,14 +88,18 @@ def __write(file, *args, **kwargs):
7388 file .write (separator )
7489
7590 def input_write (self , * args , ** kwargs ):
76- """input_write(self, *args) -> None
91+ """input_write(self, *args, **kwargs ) -> None
7792 Write every element in *args into the input file. Splits with spaces. It will convert every element into string
93+ **kwargs:
94+ str separator = " " -> a string used to separate every element
7895 """
7996 IO .__write (self .input_file , * args , ** kwargs )
8097
8198 def input_writeln (self , * args , ** kwargs ):
82- """input_writeln(self, *args) -> None
99+ """input_writeln(self, *args, **kwargs ) -> None
83100 Write every element in *args into the input file and turn to a new line. Splits with spaces. It will convert every element into string
101+ **kwargs:
102+ str separator = " " -> a string used to separate every element
84103 """
85104 args = list (args )
86105 args .append ("\n " )
@@ -93,19 +112,23 @@ def output_gen(self, shell_cmd):
93112 """
94113 self .input_file .close ()
95114 with open (self .input_filename , 'r' ) as f :
96- self .output_file .write (subprocess .check_output (shell_cmd , shell = True , stdin = f ))
115+ self .output_file .write (subprocess .check_output (shell_cmd , shell = True , stdin = f ). decode ( 'ascii' ) )
97116
98117 self .input_file = open (self .input_filename , 'a' )
99118
100119 def output_write (self , * args , ** kwargs ):
101- """output_write(self, *args) -> None
120+ """output_write(self, *args, **kwargs ) -> None
102121 Write every element in *args into the output file. Splits with spaces. It will convert every element into string
122+ **kwargs:
123+ str separator = " " -> a string used to separate every element
103124 """
104125 IO .__write (self .output_file , * args , ** kwargs )
105126
106127 def output_writeln (self , * args , ** kwargs ):
107- """output_writeln(self, *args) -> None
128+ """output_writeln(self, *args, **kwargs ) -> None
108129 Write every element in *args into the output file and turn to a new line. Splits with spaces. It will convert every element into string
130+ **kwargs:
131+ str separator = " " -> a string used to separate every element
109132 """
110133 args = list (args )
111134 args .append ("\n " )
0 commit comments