Rewrite sorted.py, unique.py with pipeable.

This commit is contained in:
voussoir 2020-11-30 22:06:09 -08:00
parent 37dc5eaa35
commit 9e1819a795
2 changed files with 45 additions and 31 deletions

View file

@ -1,19 +1,28 @@
''' import argparse
Sort the lines coming from stdin and print them.
'''
from voussoirkit import clipext
import sys import sys
if len(sys.argv) > 1: from voussoirkit import pipeable
text = clipext.resolve(sys.argv[1])
else:
text = clipext.resolve('!input')
text = text.split('\n') def sorted_argparse(args):
if '-l' in sys.argv: lines = pipeable.input(args.source, read_files=True, skip_blank=True, strip=True)
text.sort(key=lambda x: x.lower()) lines = list(lines)
else: if args.nocase:
text.sort() lines.sort(key=lambda x: x.lower())
else:
lines.sort()
new_text = '\n'.join(text) for line in lines:
print(new_text) pipeable.output(line)
def main(argv):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('source')
parser.add_argument('--nocase', dest='nocase', action='store_true')
parser.set_defaults(func=sorted_argparse)
args = parser.parse_args(argv)
return args.func(args)
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))

View file

@ -1,19 +1,24 @@
''' import argparse
Keep the unique lines coming from stdin and print them.
'''
from voussoirkit import clipext
import sys import sys
if len(sys.argv) > 1: from voussoirkit import pipeable
source = sys.argv[1]
else:
source = '!input'
lines = clipext.resolve(source, split_lines=True)
new_text = [] def unique_argparse(args):
seen = set() lines = pipeable.input(args.source, read_files=True, skip_blank=True)
for line in lines: seen = set()
for line in lines:
if line not in seen: if line not in seen:
#new_text.append(line) pipeable.output(line)
seen.add(line) seen.add(line)
print(line)
def main(argv):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('source')
parser.set_defaults(func=unique_argparse)
args = parser.parse_args(argv)
return args.func(args)
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))