From eb78014ec6905e49d62555285d53f42741d6a7c4 Mon Sep 17 00:00:00 2001 From: Dominic Ricottone Date: Wed, 17 Jun 2020 19:30:12 -0400 Subject: [PATCH] Clean up --- README.md | 36 ++++++++++++++++++------------------ filter/__main__.py | 20 ++++++++++---------- filter/cli.py | 26 +++++++++++++++++++++++++- filter/cli.toml | 4 ++++ filter/internals.py | 4 ++++ 5 files changed, 61 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 94ce6fa..9e7e983 100644 --- a/README.md +++ b/README.md @@ -3,23 +3,23 @@ Command line data filtering ```sh -$ filter -m=ab data/weights -a=.4 -b=0 -i=160 -d=1 -r -Testing on N=12 -Using alpha=0.4000, beta=0.0000 -Initial estimate of 160.0000, accelerating 1.0000 per time unit - Actual: Est.: - ======== ======== - 158.0000 159.8000 - 164.2000 162.1600 - 160.3000 162.0160 - 159.9000 161.7696 - 162.1000 162.5018 - 164.6000 163.9411 - 169.6000 166.8046 - 167.4000 167.6428 - 166.4000 167.7457 - 171.0000 169.6474 - 171.2000 170.8684 - 172.6000 172.1611 +$filter -m=ab data/weights -a=.4 -b=0 -i=160 -d=1 -r +Alpha-beta filter + α=0.4000, β=0.0000 + Initial estimate: 160.0000 changing 1.0000 per time unit +Actual: Est.: +======== ======== +158.0000 159.8000 +164.2000 162.1600 +160.3000 162.0160 +159.9000 161.7696 +162.1000 162.5018 +164.6000 163.9411 +169.6000 166.8046 +167.4000 167.6428 +166.4000 167.7457 +171.0000 169.6474 +171.2000 170.8684 +172.6000 172.1611 ``` diff --git a/filter/__main__.py b/filter/__main__.py index 9efe879..f1cfa2a 100644 --- a/filter/__main__.py +++ b/filter/__main__.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 import sys -import importlib from . import cli from . import internals @@ -9,11 +8,7 @@ from . import internals def main(): _config, _positionals = cli.main(sys.argv[1:]) - _help = "help" in _config.keys() _method = _config.get("method", "") - _files = _config.get("file", []) - _files.extend(_positionals) - _data = { "alpha": internals._try_get_float(_config, "alpha"), "beta": internals._try_get_float(_config, "beta"), @@ -24,6 +19,10 @@ def main(): "time": internals._try_get_float(_config, "time"), } + if "version" in _config.keys(): + internals._print_version() + sys.exit(0) + if _method == "ab": from . import ab as implementation elif len(_method) > 0: @@ -32,23 +31,24 @@ def main(): internals._print_usage() sys.exit(1) - if _help and len(_method) == 0: + if "help" in _config.keys() and "method" not in _config.keys(): # requesting help internals._print_help() sys.exit(0) - elif _help: + elif "help" in _config.keys(): # requesting help with a methodology - sys.stdout.print(implementation.__doc__) + sys.stdout.write(implementation.__doc__) sys.exit(0) - elif len(_method) == 0: + elif "method" not in _config.keys(): # not requesting help, but still no methodology internals._print_usage() sys.exit(1) + _files = _config.get("file", []) + _files.extend(_positionals) _data["data_raw"] = internals._get_raw_data(_files) implementation.cli_wrapper(**_data) - sys.exit(0) if __name__ == "__main__": diff --git a/filter/cli.py b/filter/cli.py index 7bf09c5..829cefd 100644 --- a/filter/cli.py +++ b/filter/cli.py @@ -5,7 +5,7 @@ import re def main(arguments): config=dict() positional=[] - pattern=re.compile(r"(?:-(?:a|b|d|f|h|x|i|m|r|t)|--(?:alpha|beta|delta|file|help|initial|method|report|time))(?:=.*)?$") + pattern=re.compile(r"(?:-(?:a|b|d|f|h|x|i|m|r|t|v|V)|--(?:alpha|beta|delta|file|help|initial|method|report|time|version))(?:=.*)?$") consuming,needing,wanting=None,0,0 attached_value=None while len(arguments) and arguments[0]!="--": @@ -94,6 +94,14 @@ def main(arguments): else: config["time"]=None consuming,needing,wanting="time",1,1 + elif option=="version": + if attached_value is not None: + message=( + 'unexpected value while parsing "version"' + ' (expected 0 values)' + ) + raise ValueError(message) from None + config["version"]=True elif option=="a": if attached_value is not None: config["alpha"]=attached_value @@ -174,6 +182,22 @@ def main(arguments): else: config["time"]=None consuming,needing,wanting="time",1,1 + elif option=="v": + if attached_value is not None: + message=( + 'unexpected value while parsing "version"' + ' (expected 0 values)' + ) + raise ValueError(message) from None + config["version"]=True + elif option=="V": + if attached_value is not None: + message=( + 'unexpected value while parsing "version"' + ' (expected 0 values)' + ) + raise ValueError(message) from None + config["version"]=True else: positional.append(arguments.pop(0)) if needing>0: diff --git a/filter/cli.toml b/filter/cli.toml index e26ad87..9f4217f 100644 --- a/filter/cli.toml +++ b/filter/cli.toml @@ -35,3 +35,7 @@ alternatives = ['r'] number = 1 alternatives = ['t'] +[version] +number = 0 +alternatives = ['v', 'V'] + diff --git a/filter/internals.py b/filter/internals.py index 3ffaa02..e91ed80 100644 --- a/filter/internals.py +++ b/filter/internals.py @@ -70,6 +70,10 @@ def _print_help() -> None: _msg = "Usage: filter -m=METHOD DATA\n" sys.stdout.write(_msg) +def _print_version() -> None: + _msg = "gap 1.0.0\n" + sys.stdout.write(_msg) + def _print_usage() -> None: _msg = "Usage: filter -m=METHOD DATA\n" sys.stderr.write(_msg) -- 2.45.2