Implement --only-count.
-"""
-Copy records from the first KGTK file to the output file, if
-one or more columns are (any/all) (not) empty.
-
-TODO: add --count-only to suppress copying?
-
-TODO: add --distribution?
+"""Copy records from the first KGTK file to the output file, if one or more
+columns are (any/all) (not) empty. If --only-count is True, report the count
+of qualifying records but do not write the output file.
"""
@@ -36,6 +32,8 @@ class IfEmpty(KgtkFormat):
allare: bool = attr.ib(validator=attr.validators.instance_of(bool), default=False)
notempty: bool = attr.ib(validator=attr.validators.instance_of(bool), default=False)
+ only_count: bool = attr.ib(validator=attr.validators.instance_of(bool), default=False)
+
# TODO: find working validators
# value_options: typing.Optional[KgtkValueOptions] = attr.ib(attr.validators.optional(attr.validators.instance_of(KgtkValueOptions)), default=None)
reader_options: typing.Optional[KgtkReaderOptions]= attr.ib(default=None)
@@ -101,6 +99,7 @@ class IfEmpty(KgtkFormat):
filter_idx_list.append(kr.column_name_map[column_name])
+ if not self.only_count:
if self.verbose:
print("Opening the output file: %s" % self.output_file_path, file=self.error_file, flush=True)
ew: KgtkWriter = KgtkWriter.open(kr.column_names,
@@ -121,9 +120,14 @@ class IfEmpty(KgtkFormat):
for row in kr:
input_line_count += 1
if self.filter(row, filter_idx_list):
+ if not self.only_count:
ew.write(row)
output_line_count += 1
+
+ if self.only_count:
+ print("Read %d records, %d records passed the filter." % (input_line_count, output_line_count), file=self.error_file, flush=True)
+ else:
if self.verbose:
print("Read %d records, wrote %d records." % (input_line_count, output_line_count), file=self.error_file, flush=True)
@@ -147,6 +151,9 @@ def main():
parser.add_argument( "--not-empty", dest="notempty", help="False: test if empty, True: test if not empty (default=%(default)s).",
type=optional_bool, nargs='?', const=True, default=False)
+ parser.add_argument( "--only-count", dest="only_count", help="Only count the records, do not copy them. (default=%(default)s).",
+ type=optional_bool, nargs='?', const=True, default=False)
+
KgtkReader.add_debug_arguments(parser)
KgtkReaderOptions.add_arguments(parser, mode_options=True)
@@ -171,6 +178,7 @@ def main():
output_file_path=args.output_file_path,
allare=args.allare,
notempty=args.notempty,
+ only_count = args.only_count,
reader_options=reader_options,
value_options=value_options,
error_file=error_file,
