diff --git a/cyaron/io.py b/cyaron/io.py index bbfa7f9..63a656b 100644 --- a/cyaron/io.py +++ b/cyaron/io.py @@ -3,6 +3,7 @@ Classes: IO: IO tool class. It will process the input and output files. """ + from __future__ import absolute_import import os import re @@ -200,6 +201,31 @@ def __write(self, file: IOBase, *args, **kwargs): if arg == "\n": self.is_first_char[file] = True + def __write_list(self, file: IOBase, *args, **kwargs): + """ + Write every element in *args into file. If the element isn't "\n", insert `separator`. + It will convert every element into str. + """ + separators = kwargs.get("separator", " ") + if list_like(separators): + if len(separators) == 0: + raise ValueError() + separator = make_unicode(separators[0]) + separators = separators[1:] + else: + separator = separators = make_unicode(separators) + for arg in args: + if arg != "\n" and not self.is_first_char.get(file, True): + file.write(separator) + self.is_first_char[file] = True + if list_like(arg): + self.__write_list(file, *arg, separator=separators) + else: + self.is_first_char[file] = False + file.write(make_unicode(arg)) + if arg == "\n": + self.is_first_char[file] = True + def __clear(self, file: IOBase, pos: int = 0): """ Clear the content use truncate() @@ -234,6 +260,29 @@ def input_writeln(self, *args, **kwargs): args.append("\n") self.input_write(*args, **kwargs) + def input_write_list(self, *args, **kwargs): + """ + Write hith-dimensional lists in *args into the input file. Splits with `separator`. + It will convert every element into str. + Args: + *args: hith-dimensional lists to write + separator: a string or a string list used to separate every element in different + dimension. Defaults to " ". + """ + self.__write_list(self.input_file, *args, **kwargs) + + def input_write_listln(self, *args, **kwargs): + """ + Write hith-dimensional lists in *args into the input file and turn into a new line + Splits with `separator`. + It will convert every element into str. + Args: + *args: hith-dimensional lists to write + separator: a string or a string list used to separate every element in different + dimension. Defaults to " ". + """ + self.input_write_list(*args, "\n", **kwargs) + def input_clear_content(self, pos: int = 0): """ Clear the content of input @@ -303,6 +352,29 @@ def output_writeln(self, *args, **kwargs): args.append("\n") self.output_write(*args, **kwargs) + def output_write_list(self, *args, **kwargs): + """ + Write hith-dimensional lists in *args into the output file. Splits with `separator`. + It will convert every element into str. + Args: + *args: hith-dimensional lists to write + separator: a string or a string list used to separate every element in different + dimension. Defaults to " ". + """ + self.__write_list(self.output_file, *args, **kwargs) + + def output_write_listln(self, *args, **kwargs): + """ + Write hith-dimensional lists in *args into the output file and turn into a new line + Splits with `separator`. + It will convert every element into str. + Args: + *args: hith-dimensional lists to write + separator: a string or a string list used to separate every element in different + dimension. Defaults to " ". + """ + self.output_write_list(*args, "\n", **kwargs) + def output_clear_content(self, pos: int = 0): """ Clear the content of output