Make the format optional.  Detect excess values.
@@ -863,14 +863,29 @@ class KgtkWriter(KgtkBase):
#
# TODO: Support lists.
def writef(self,
- format: str,
raw_values: typing.List[typing.Any],
+ format: typing.Optiona[str] = None,
shuffle_list: typing.Optional[typing.List[int]] = None):
values: typing.List[str] = list()
- fidx: int
idx: int
raw_value: typing.Any
+
+ if format is None:
+ for idx, raw_value in enumerate(raw_values):
+ if isinstance(raw_value, string):
+ values.append(KgtkFormat.stringify(raw_value))
+ elif isinstance(raw_value, (int, float)):
+ values.append(str(raw_value))
+ elif isinstance(raw_value, bool):
+ values.append(KgtkFormat.to_boolean(raw_value))
+ elif isinstance(raw_value, datetime):
+ values.append(KgtkFormat.from_datetime(raw_value))
+ else:
+ raise ValueError("KgtkWriter: unsupported datatype in item %d: %s" % (idx, repr(raw_values)))
+
+ else:
+ fidx: int
f: str
for fidx, f in enumerate(format):
if idx > len(raw_values):
@@ -936,6 +951,8 @@ class KgtkWriter(KgtkBase):
else:
raise ValueError("KgtkWriter: unknown format %s" % (f))
+ if idx < len(raw_values):
+ raise ValueError("KgtkWriter: too many values for format %s: %s" % (repr(format), repr(raw_values)))
if shuffle_list is not None:
values = self.shuffle(values, shuffle_list)
