Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support outputting high-dimensional lists #147

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions cyaron/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading