Last active
January 19, 2026 12:42
-
-
Save mdmitry1/dba5f1215ab2c142ba36b10500c8292f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python3.14 | |
| import sys | |
| from os.path import realpath, basename | |
| from rich import print as rprint | |
| from pandas import read_csv | |
| from argparse import ArgumentParser, Namespace | |
| from contextlib import redirect_stdout | |
| def add_sort_arguments() -> ArgumentParser: | |
| p = ArgumentParser() | |
| p.add_argument('--file', '-f', default="/dev/stdin") | |
| p.add_argument('--out', '-o', default=None) | |
| p.add_argument('--column', '-c', type=int, default=1) | |
| p.add_argument('--header', '-hdr', default=None, action='store_true') | |
| p.add_argument('--reverse', '-r', default=False, action='store_true') | |
| p.add_argument('--separator', '-s', default='\\s+') | |
| return p | |
| def print_result(df, r, sep, header): | |
| if '\\' == sep[0]: | |
| sep = ' ' | |
| if header: | |
| columns=df.columns.tolist() | |
| [print(f"{column}{sep}",end="") for column in columns[:-1]] | |
| print(columns[-1]) | |
| [print(*row, sep=sep) for row in r] | |
| def sort_dataframe(args: Namespace) -> int: | |
| script_name = basename(realpath(sys.argv[0])) | |
| try: | |
| df = read_csv(args.file,sep=args.separator) \ | |
| if args.header else read_csv(args.file,sep=args.separator,header=None) | |
| r = df.sort_values(by = df.columns[args.column-1], ascending = not args.reverse).values.tolist() | |
| if args.out is not None: | |
| with open(args.out, 'w') as outf, redirect_stdout(outf): | |
| print_result(df, r, args.separator, args.header) | |
| else: | |
| print_result(df, r, args.separator, args.header) | |
| return 0 | |
| except Exception as err: | |
| rprint(f"\n[magenta]{script_name}:[red] ERROR: {err}[/red]\n") | |
| return 1 | |
| if __name__ == '__main__': | |
| exit(sort_dataframe(add_sort_arguments().parse_args())) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| x | y | |
|---|---|---|
| 5 | 7 | |
| 3 | 8 | |
| 5 | 15 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| from sortdf import sort_dataframe, add_sort_arguments | |
| from os import remove, popen, getenv | |
| from os.path import exists, dirname, realpath | |
| def test_df(monkeypatch, request): | |
| root_dir = str(request.config.rootpath) + '/' | |
| with monkeypatch.context() as m: | |
| test_path = dirname(realpath(root_dir + getenv('PYTEST_CURRENT_TEST').split(':')[0])) | |
| out = test_path + '/test.out' | |
| if exists(out): | |
| remove(out) | |
| assert exists(out) == False | |
| print("") | |
| m.setattr(sys, 'argv', ['sortdf', '-hdr', '-s,', '-r', '-c2', '-f', test_path + '/test.csv', '-o', out]) | |
| assert sort_dataframe(add_sort_arguments().parse_args()) == 0 | |
| assert int(popen(f"sum {out}").read().split()[0]) == 53966 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment