From fc9fb0ec58f0135d38f98b8cdbe697d034ffaf35 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Mon, 30 Nov 2020 22:05:30 -0800 Subject: [PATCH] Add groups_of.py. --- groups_of.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 groups_of.py diff --git a/groups_of.py b/groups_of.py new file mode 100644 index 0000000..2a18071 --- /dev/null +++ b/groups_of.py @@ -0,0 +1,28 @@ +import argparse +import os +import sys + +from voussoirkit import gentools +from voussoirkit import pipeable + +def groupsof_argparse(args): + lines = pipeable.input(args.source, read_files=True, strip=True, skip_blank=True) + + chunks = gentools.chunk_generator(lines, args.chunk_size) + for chunk in chunks: + chunk = args.separator.join(chunk) + pipeable.output(chunk) + +def main(argv): + parser = argparse.ArgumentParser(description=__doc__) + + parser.add_argument('source') + parser.add_argument('chunk_size', type=int) + parser.add_argument('--separator', dest='separator', default=',') + parser.set_defaults(func=groupsof_argparse) + + args = parser.parse_args(argv) + return args.func(args) + +if __name__ == '__main__': + raise SystemExit(main(sys.argv[1:]))