Commit 14e9e1d4 authored by BO ZHANG's avatar BO ZHANG 🏀
Browse files

updated FileRecorder.__repr__()

parent bb226846
import os
from collections import namedtuple from collections import namedtuple
from astropy import table from astropy import table
import os
File = namedtuple( FileRecord = namedtuple(
"FileRecord", "FileRecord",
["filepath", "db", "comment", "existence"] ["filepath", "db", "comment", "existence"]
) )
...@@ -10,7 +11,7 @@ File = namedtuple( ...@@ -10,7 +11,7 @@ File = namedtuple(
class FileRecorder(list): class FileRecorder(list):
""" """
File Recorder, inherited from the built-in ``list``. FileRecord Recorder, inherited from the built-in ``list``.
This recorder is used to record files generated by functional modules. This recorder is used to record files generated by functional modules.
In principle, a CSST pipeline module should return a status (CsstStatus) In principle, a CSST pipeline module should return a status (CsstStatus)
...@@ -62,22 +63,19 @@ class FileRecorder(list): ...@@ -62,22 +63,19 @@ class FileRecorder(list):
super(FileRecorder, self).__init__(*args, **kwargs) super(FileRecorder, self).__init__(*args, **kwargs)
@staticmethod @staticmethod
def Record(filepath: str = "", db: bool = False, comment: str = ""): def FileRecord(filepath: str = "", db: bool = False, comment: str = ""):
return File(filepath=filepath, db=db, comment=comment, existence=os.path.exists(filepath)) return FileRecord(filepath=filepath, db=db, comment=comment, existence=os.path.exists(filepath))
def add_record(self, filepath: str = "", db: bool = False, comment: str = ""): def add_record(self, filepath: str = "", db: bool = False, comment: str = ""):
existence = os.path.exists(filepath) existence = os.path.exists(filepath)
assert isinstance(filepath, str) assert isinstance(filepath, str)
assert isinstance(db, bool) assert isinstance(db, bool)
assert isinstance(comment, str) assert isinstance(comment, str)
super().append(File(filepath=filepath, db=db, comment=comment, existence=existence)) super().append(FileRecord(filepath=filepath, db=db, comment=comment, existence=existence))
def to_table(self): def to_table(self):
return table.Table([_._asdict() for _ in self]) return table.Table([_._asdict() for _ in self])
def __repr__(self):
return self.to_table().__repr__()
def pprint(self, *args, **kwargs): def pprint(self, *args, **kwargs):
print("<FileRecorder length={}>".format(len(self))) print("<FileRecorder length={}>".format(len(self)))
return self.to_table().pprint(*args, **kwargs) return self.to_table().pprint(*args, **kwargs)
...@@ -85,3 +83,11 @@ class FileRecorder(list): ...@@ -85,3 +83,11 @@ class FileRecorder(list):
def pprint_all(self, *args, **kwargs): def pprint_all(self, *args, **kwargs):
print("<FileRecorder length={}>".format(len(self))) print("<FileRecorder length={}>".format(len(self)))
return self.to_table().pprint_all(*args, **kwargs) return self.to_table().pprint_all(*args, **kwargs)
def __repr__(self):
t = self.to_table()
lines, outs = t.formatter._pformat_table(
t, max_lines=-1, max_width=-1, show_name=True, show_unit=None, show_dtype=False, align=None)
if outs['show_length']:
lines.append(f'Length = {len(self)} rows')
return "\n".join(lines)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment