未验证 提交 ad8d0ced 编写于 作者: Z zenghsh3 提交者: GitHub

add more comment (#384)

上级 afe3fef4
...@@ -19,12 +19,24 @@ __all__ = ['CSVLogger'] ...@@ -19,12 +19,24 @@ __all__ = ['CSVLogger']
class CSVLogger(object): class CSVLogger(object):
def __init__(self, output_file): def __init__(self, output_file):
"""CSV Logger which can write dict result to csv file """CSV Logger which can write dict result to csv file.
Args:
output_file(str): filename of the csv file.
""" """
self.output_file = open(output_file, "w") self.output_file = open(output_file, "w")
self.csv_writer = None self.csv_writer = None
def log_dict(self, result): def log_dict(self, result):
"""Ouput result to the csv file.
Will create the header of the csv file automatically when the function is called for the first time.
Ususally, the keys of the result should be the same every time you call the function.
Args:
result(dict)
"""
assert isinstance(result, dict), "the input should be a dict."
if self.csv_writer is None: if self.csv_writer is None:
self.csv_writer = csv.DictWriter(self.output_file, result.keys()) self.csv_writer = csv.DictWriter(self.output_file, result.keys())
self.csv_writer.writeheader() self.csv_writer.writeheader()
...@@ -38,4 +50,9 @@ class CSVLogger(object): ...@@ -38,4 +50,9 @@ class CSVLogger(object):
self.output_file.flush() self.output_file.flush()
def close(self): def close(self):
if not self.output_file.closed:
self.output_file.close()
def __del__(self):
if not self.output_file.closed:
self.output_file.close() self.output_file.close()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册