From 7166674d6c4ba0726ce534b5c97358b11092260a Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 22 Jun 2019 20:09:39 +0200 Subject: [PATCH] Move check_int_positive out of arguments class --- freqtrade/arguments.py | 31 ++++++++++++++++--------------- freqtrade/tests/test_arguments.py | 16 ++++++++-------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/freqtrade/arguments.py b/freqtrade/arguments.py index 1ec32d1f0..0d0896b2a 100644 --- a/freqtrade/arguments.py +++ b/freqtrade/arguments.py @@ -10,6 +10,18 @@ import arrow from freqtrade import __version__, constants +def check_int_positive(value: str) -> int: + try: + uint = int(value) + if uint <= 0: + raise ValueError + except ValueError: + raise argparse.ArgumentTypeError( + f"{value} is invalid for this parameter, should be a positive integer value" + ) + return uint + + class TimeRange(NamedTuple): """ NamedTuple Defining timerange inputs. @@ -33,6 +45,7 @@ class Arguments(object): self.parser = argparse.ArgumentParser(description=description) def _load_args(self) -> None: + # Common options self.common_options() self.main_options() self._build_subcommands() @@ -318,7 +331,7 @@ class Arguments(object): '--random-state', help='Set random state to some positive integer for reproducible hyperopt results.', dest='hyperopt_random_state', - type=Arguments.check_int_positive, + type=check_int_positive, metavar='INT', ) parser.add_argument( @@ -327,7 +340,7 @@ class Arguments(object): "optimization path (default: 1).", dest='hyperopt_min_trades', default=1, - type=Arguments.check_int_positive, + type=check_int_positive, metavar='INT', ) @@ -422,18 +435,6 @@ class Arguments(object): return TimeRange(stype[0], stype[1], start, stop) raise Exception('Incorrect syntax for timerange "%s"' % text) - @staticmethod - def check_int_positive(value: str) -> int: - try: - uint = int(value) - if uint <= 0: - raise ValueError - except ValueError: - raise argparse.ArgumentTypeError( - f"{value} is invalid for this parameter, should be a positive integer value" - ) - return uint - def common_scripts_options(self, subparser: argparse.ArgumentParser = None) -> None: """ Parses arguments common for scripts. @@ -462,7 +463,7 @@ class Arguments(object): '--days', help='Download data for given number of days.', dest='days', - type=Arguments.check_int_positive, + type=check_int_positive, metavar='INT', ) parser.add_argument( diff --git a/freqtrade/tests/test_arguments.py b/freqtrade/tests/test_arguments.py index d9292bdb5..20e63d7e7 100644 --- a/freqtrade/tests/test_arguments.py +++ b/freqtrade/tests/test_arguments.py @@ -3,7 +3,7 @@ import argparse import pytest -from freqtrade.arguments import Arguments, TimeRange +from freqtrade.arguments import Arguments, TimeRange, check_int_positive # Parse common command-line-arguments. Used for all tools @@ -206,18 +206,18 @@ def test_plot_dataframe_options() -> None: def test_check_int_positive() -> None: - assert Arguments.check_int_positive("3") == 3 - assert Arguments.check_int_positive("1") == 1 - assert Arguments.check_int_positive("100") == 100 + assert check_int_positive("3") == 3 + assert check_int_positive("1") == 1 + assert check_int_positive("100") == 100 with pytest.raises(argparse.ArgumentTypeError): - Arguments.check_int_positive("-2") + check_int_positive("-2") with pytest.raises(argparse.ArgumentTypeError): - Arguments.check_int_positive("0") + check_int_positive("0") with pytest.raises(argparse.ArgumentTypeError): - Arguments.check_int_positive("3.5") + check_int_positive("3.5") with pytest.raises(argparse.ArgumentTypeError): - Arguments.check_int_positive("DeadBeef") + check_int_positive("DeadBeef")