Move check_int_positive out of arguments class

This commit is contained in:
Matthias 2019-06-22 20:09:39 +02:00
parent 31a2aac627
commit 7166674d6c
2 changed files with 24 additions and 23 deletions

View File

@ -10,6 +10,18 @@ import arrow
from freqtrade import __version__, constants 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): class TimeRange(NamedTuple):
""" """
NamedTuple Defining timerange inputs. NamedTuple Defining timerange inputs.
@ -33,6 +45,7 @@ class Arguments(object):
self.parser = argparse.ArgumentParser(description=description) self.parser = argparse.ArgumentParser(description=description)
def _load_args(self) -> None: def _load_args(self) -> None:
# Common options
self.common_options() self.common_options()
self.main_options() self.main_options()
self._build_subcommands() self._build_subcommands()
@ -318,7 +331,7 @@ class Arguments(object):
'--random-state', '--random-state',
help='Set random state to some positive integer for reproducible hyperopt results.', help='Set random state to some positive integer for reproducible hyperopt results.',
dest='hyperopt_random_state', dest='hyperopt_random_state',
type=Arguments.check_int_positive, type=check_int_positive,
metavar='INT', metavar='INT',
) )
parser.add_argument( parser.add_argument(
@ -327,7 +340,7 @@ class Arguments(object):
"optimization path (default: 1).", "optimization path (default: 1).",
dest='hyperopt_min_trades', dest='hyperopt_min_trades',
default=1, default=1,
type=Arguments.check_int_positive, type=check_int_positive,
metavar='INT', metavar='INT',
) )
@ -422,18 +435,6 @@ class Arguments(object):
return TimeRange(stype[0], stype[1], start, stop) return TimeRange(stype[0], stype[1], start, stop)
raise Exception('Incorrect syntax for timerange "%s"' % text) 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: def common_scripts_options(self, subparser: argparse.ArgumentParser = None) -> None:
""" """
Parses arguments common for scripts. Parses arguments common for scripts.
@ -462,7 +463,7 @@ class Arguments(object):
'--days', '--days',
help='Download data for given number of days.', help='Download data for given number of days.',
dest='days', dest='days',
type=Arguments.check_int_positive, type=check_int_positive,
metavar='INT', metavar='INT',
) )
parser.add_argument( parser.add_argument(

View File

@ -3,7 +3,7 @@ import argparse
import pytest 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 # 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: def test_check_int_positive() -> None:
assert Arguments.check_int_positive("3") == 3 assert check_int_positive("3") == 3
assert Arguments.check_int_positive("1") == 1 assert check_int_positive("1") == 1
assert Arguments.check_int_positive("100") == 100 assert check_int_positive("100") == 100
with pytest.raises(argparse.ArgumentTypeError): with pytest.raises(argparse.ArgumentTypeError):
Arguments.check_int_positive("-2") check_int_positive("-2")
with pytest.raises(argparse.ArgumentTypeError): with pytest.raises(argparse.ArgumentTypeError):
Arguments.check_int_positive("0") check_int_positive("0")
with pytest.raises(argparse.ArgumentTypeError): with pytest.raises(argparse.ArgumentTypeError):
Arguments.check_int_positive("3.5") check_int_positive("3.5")
with pytest.raises(argparse.ArgumentTypeError): with pytest.raises(argparse.ArgumentTypeError):
Arguments.check_int_positive("DeadBeef") check_int_positive("DeadBeef")