2023-03-03 12:59:00 -06:00
|
|
|
#!/usr/bin/env python
|
2023-02-28 13:52:31 -06:00
|
|
|
# Exercise 10
|
2023-03-03 12:49:43 -06:00
|
|
|
# sort command
|
2023-02-28 13:52:31 -06:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
def modify(lines, args, name):
|
|
|
|
if name in args.keys() and args[name] is True:
|
|
|
|
for i, line in enumerate(lines):
|
|
|
|
match name:
|
|
|
|
case "ignore_leading_blanks":
|
|
|
|
lines[i]["mut"] = lines[i]["mut"].strip()
|
|
|
|
case "ignore_case":
|
|
|
|
lines[i]["mut"] = lines[i]["mut"].lower()
|
|
|
|
case "numeric_sort":
|
|
|
|
if lines[i]["mut"].isnumeric():
|
|
|
|
lines[i]["mut"] = int(lines[i]["mut"])
|
|
|
|
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("stream", default=sys.stdin, nargs="*")
|
|
|
|
parser.add_argument("-f", "--ignore-case", action="store_true")
|
|
|
|
parser.add_argument("-b", "--ignore-leading-blanks", action="store_true")
|
|
|
|
parser.add_argument("-n", "--numeric-sort", action="store_true")
|
|
|
|
parser.add_argument("-r", "--reverse", action="store_true")
|
|
|
|
args = parser.parse_args().__dict__
|
|
|
|
lines = []
|
|
|
|
|
|
|
|
for stream in args["stream"]:
|
|
|
|
if isinstance(args["stream"], list):
|
|
|
|
for line in Path(stream).open().readlines():
|
|
|
|
lines.append({"fix": line.rstrip(), "mut": line.rstrip()})
|
|
|
|
else:
|
|
|
|
lines.append({"fix": stream.strip(), "mut": stream.strip()})
|
|
|
|
|
|
|
|
modify(lines, args, "ignore_leading_blanks")
|
|
|
|
modify(lines, args, "ignore_case")
|
|
|
|
modify(lines, args, "numeric_sort")
|
|
|
|
|
|
|
|
lines = sorted(lines, key=lambda k: k["mut"])
|
|
|
|
|
|
|
|
if args["reverse"]:
|
|
|
|
lines.reverse()
|
|
|
|
|
|
|
|
for line in lines:
|
|
|
|
print(line["fix"])
|