# HG changeset patch # User Raphaël Gomès <rgomes@octobus.net> # Date 1678893445 -3600 # Wed Mar 15 16:17:25 2023 +0100 # Node ID e402bad5cc90e9c30edb4583d9e8a13c82d6e099 # Parent d82a0b5b58a88623d796a92236c4cae61433628c bin: rewrite `diff-result` with `click` This is much simpler and more powerful than rolling it by hand. diff --git a/python-libs/poulpe/bin/diff-result b/python-libs/poulpe/bin/diff-result --- a/python-libs/poulpe/bin/diff-result +++ b/python-libs/poulpe/bin/diff-result @@ -1,32 +1,10 @@ #!/usr/bin/env python3 -# -# Small tool to compare two results -import argparse import collections -import sys + +import click import poulpe -err = poulpe.err - - -def compare(old_path, new_path, full=False): - old = poulpe.get_data(old_path) - if old is None: - err(f'old file not found: {old_path}') - return 1 - new = poulpe.get_data(new_path) - if new is None: - err(f'new file not found: {new_path}') - return 1 - - if full: - full_compare(old, new) - else: - simple_compare(old, new) - - return 0 - def simple_compare(old, new): old_median = old['result']['time']['median'] @@ -37,8 +15,6 @@ print(f"{old_median:.4f} -> {new_median:.4f}: {diff:.4f} ({ratio:.2f})") - return 0 - def _linear_data(data, prefix=""): """call to display data""" @@ -77,33 +53,30 @@ else: n = v[1] print(f"{k}: {o} -> {n}") - return 0 -def _parsers(): - cmd_parser = argparse.ArgumentParser(prog='poulpe-bin-env-util') +@click.command(no_args_is_help=True) +@click.argument("old_result", type=click.Path(exists=True, dir_okay=False)) +@click.argument("new_result", type=click.Path(exists=True, dir_okay=False)) +@click.option( + "--full", is_flag=True, default=False, help="also show the unchanged parts" +) +def diff_result(old_result, new_result, full): + """Small tool to compare two results together - cmd_parser.add_argument( - 'OLD_RESULT', - help="path to the old result", - ) - cmd_parser.add_argument( - 'NEW_RESULT', - help="path to the new result", - ) + \b + ARGS: + <OLD_RESULT> is the path the old result + <NEW_RESULT> is the path the old result + """ + old = poulpe.get_data(old_result) + new = poulpe.get_data(new_result) - cmd_parser.add_argument('--full', action='store_const', const=True) - return cmd_parser - - -def main(args): - parser = _parsers() - param = parser.parse_args(args) - ret = compare(param.OLD_RESULT, param.NEW_RESULT, full=param.full) - return ret + if full: + full_compare(old, new) + else: + simple_compare(old, new) if __name__ == "__main__": - ret = main(sys.argv[1:]) - assert ret is not None - sys.exit(ret) + diff_result()