From 8ec974f08bba5e95d60c44456ac2a41e298f6b0d Mon Sep 17 00:00:00 2001 From: weilycoder Date: Wed, 20 Nov 2024 17:26:35 +0800 Subject: [PATCH 1/3] Support outputting high-dimensional lists --- cyaron/io.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/cyaron/io.py b/cyaron/io.py index bbfa7f9..571ac53 100644 --- a/cyaron/io.py +++ b/cyaron/io.py @@ -200,6 +200,26 @@ def __write(self, file: IOBase, *args, **kwargs): if arg == "\n": self.is_first_char[file] = True + def __write_list(self, file: IOBase, *args, **kwargs): + 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) + 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 +254,12 @@ def input_writeln(self, *args, **kwargs): args.append("\n") self.input_write(*args, **kwargs) + def input_write_list(self, *args, **kwargs): + self.__write_list(self.input_file, *args, **kwargs) + + def input_write_listln(self, *args, **kwargs): + self.input_write_list(*args, "\n", **kwargs) + def input_clear_content(self, pos: int = 0): """ Clear the content of input @@ -303,6 +329,12 @@ def output_writeln(self, *args, **kwargs): args.append("\n") self.output_write(*args, **kwargs) + def output_write_list(self, *args, **kwargs): + self.__write_list(self.output_file, *args, **kwargs) + + def output_write_listln(self, *args, **kwargs): + self.output_write_list(*args, "\n", **kwargs) + def output_clear_content(self, pos: int = 0): """ Clear the content of output From 26e5b8da897ea2d42b8946e815bee178f8dcdc44 Mon Sep 17 00:00:00 2001 From: weilycoder Date: Wed, 20 Nov 2024 17:27:32 +0800 Subject: [PATCH 2/3] Add comments --- cyaron/io.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/cyaron/io.py b/cyaron/io.py index 571ac53..ee554c0 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 @@ -201,6 +202,10 @@ def __write(self, file: IOBase, *args, **kwargs): 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: @@ -255,9 +260,26 @@ def input_writeln(self, *args, **kwargs): 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): @@ -330,9 +352,26 @@ def output_writeln(self, *args, **kwargs): 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): From 8ad4a6b22c4bb3d18543f3145ee7d80f4e14a688 Mon Sep 17 00:00:00 2001 From: weilycoder Date: Wed, 20 Nov 2024 17:41:44 +0800 Subject: [PATCH 3/3] Fix separating --- cyaron/io.py | 1 + 1 file changed, 1 insertion(+) diff --git a/cyaron/io.py b/cyaron/io.py index ee554c0..63a656b 100644 --- a/cyaron/io.py +++ b/cyaron/io.py @@ -217,6 +217,7 @@ def __write_list(self, file: IOBase, *args, **kwargs): 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: