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 @@
'''
Sort the lines coming from stdin and print them.
'''
from voussoirkit import clipext
import argparse
import sys
if len(sys.argv) > 1:
text = clipext.resolve(sys.argv[1])
else:
text = clipext.resolve('!input')
from voussoirkit import pipeable
text = text.split('\n')
if '-l' in sys.argv:
text.sort(key=lambda x: x.lower())
def sorted_argparse(args):
lines = pipeable.input(args.source, read_files=True, skip_blank=True, strip=True)
lines = list(lines)
if args.nocase:
lines.sort(key=lambda x: x.lower())
else:
text.sort()
lines.sort()
new_text = '\n'.join(text)
print(new_text)
for line in lines:
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 @@
'''
Keep the unique lines coming from stdin and print them.
'''
from voussoirkit import clipext
import argparse
import sys
if len(sys.argv) > 1:
source = sys.argv[1]
else:
source = '!input'
lines = clipext.resolve(source, split_lines=True)
from voussoirkit import pipeable
new_text = []
def unique_argparse(args):
lines = pipeable.input(args.source, read_files=True, skip_blank=True)
seen = set()
for line in lines:
if line not in seen:
#new_text.append(line)
pipeable.output(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:]))