cleanup Arguments: name attrs and methods as non-public

This commit is contained in:
hroff-1902
2019-07-18 17:55:35 +03:00
parent fbd229810f
commit 7af24dc486
5 changed files with 29 additions and 32 deletions

View File

@@ -342,14 +342,15 @@ class Arguments(object):
"""
Arguments Class. Manage the arguments received by the cli
"""
def __init__(self, args: Optional[List[str]], description: str) -> None:
def __init__(self, args: Optional[List[str]], description: str,
no_default_config: bool = False) -> None:
self.args = args
self.parsed_arg: Optional[argparse.Namespace] = None
self._parsed_arg: Optional[argparse.Namespace] = None
self.parser = argparse.ArgumentParser(description=description)
self._no_default_config = no_default_config
def _load_args(self) -> None:
self.build_args(optionlist=ARGS_MAIN)
self._build_args(optionlist=ARGS_MAIN)
self._build_subcommands()
def get_parsed_arg(self) -> argparse.Namespace:
@@ -357,13 +358,13 @@ class Arguments(object):
Return the list of arguments
:return: List[str] List of arguments
"""
if self.parsed_arg is None:
if self._parsed_arg is None:
self._load_args()
self.parsed_arg = self.parse_args()
self._parsed_arg = self._parse_args()
return self.parsed_arg
return self._parsed_arg
def parse_args(self, no_default_config: bool = False) -> argparse.Namespace:
def _parse_args(self) -> argparse.Namespace:
"""
Parses given arguments and returns an argparse Namespace instance.
"""
@@ -371,12 +372,12 @@ class Arguments(object):
# Workaround issue in argparse with action='append' and default value
# (see https://bugs.python.org/issue16399)
if not no_default_config and parsed_arg.config is None:
if not self._no_default_config and parsed_arg.config is None:
parsed_arg.config = [constants.DEFAULT_CONFIG]
return parsed_arg
def build_args(self, optionlist, parser=None):
def _build_args(self, optionlist, parser=None):
parser = parser or self.parser
for val in optionlist:
@@ -396,17 +397,17 @@ class Arguments(object):
# Add backtesting subcommand
backtesting_cmd = subparsers.add_parser('backtesting', help='Backtesting module.')
backtesting_cmd.set_defaults(func=start_backtesting)
self.build_args(optionlist=ARGS_BACKTEST, parser=backtesting_cmd)
self._build_args(optionlist=ARGS_BACKTEST, parser=backtesting_cmd)
# Add edge subcommand
edge_cmd = subparsers.add_parser('edge', help='Edge module.')
edge_cmd.set_defaults(func=start_edge)
self.build_args(optionlist=ARGS_EDGE, parser=edge_cmd)
self._build_args(optionlist=ARGS_EDGE, parser=edge_cmd)
# Add hyperopt subcommand
hyperopt_cmd = subparsers.add_parser('hyperopt', help='Hyperopt module.')
hyperopt_cmd.set_defaults(func=start_hyperopt)
self.build_args(optionlist=ARGS_HYPEROPT, parser=hyperopt_cmd)
self._build_args(optionlist=ARGS_HYPEROPT, parser=hyperopt_cmd)
# Add list-exchanges subcommand
list_exchanges_cmd = subparsers.add_parser(
@@ -414,7 +415,7 @@ class Arguments(object):
help='Print available exchanges.'
)
list_exchanges_cmd.set_defaults(func=start_list_exchanges)
self.build_args(optionlist=ARGS_LIST_EXCHANGES, parser=list_exchanges_cmd)
self._build_args(optionlist=ARGS_LIST_EXCHANGES, parser=list_exchanges_cmd)
@staticmethod
def parse_timerange(text: Optional[str]) -> TimeRange:

View File

@@ -51,8 +51,8 @@ def test_parse_args_verbose() -> None:
def test_common_scripts_options() -> None:
arguments = Arguments(['-p', 'ETH/BTC'], '')
arguments.build_args(ARGS_DOWNLOADER)
args = arguments.parse_args()
arguments._build_args(ARGS_DOWNLOADER)
args = arguments._parse_args()
assert args.pairs == 'ETH/BTC'
@@ -180,9 +180,8 @@ def test_download_data_options() -> None:
'--exchange', 'binance'
]
arguments = Arguments(args, '')
arguments.build_args(ARGS_DOWNLOADER)
args = arguments.parse_args()
arguments._build_args(ARGS_DOWNLOADER)
args = arguments._parse_args()
assert args.pairs_file == 'file_with_pairs'
assert args.datadir == 'datadir/directory'
assert args.days == 30
@@ -197,8 +196,8 @@ def test_plot_dataframe_options() -> None:
'-p', 'UNITTEST/BTC',
]
arguments = Arguments(args, '')
arguments.build_args(ARGS_PLOT_DATAFRAME)
pargs = arguments.parse_args(True)
arguments._build_args(ARGS_PLOT_DATAFRAME)
pargs = arguments._parse_args()
assert pargs.indicators1 == "sma10,sma100"
assert pargs.indicators2 == "macd,fastd,fastk"
assert pargs.plot_limit == 30
@@ -206,7 +205,6 @@ def test_plot_dataframe_options() -> None:
def test_check_int_positive() -> None:
assert check_int_positive("3") == 3
assert check_int_positive("1") == 1
assert check_int_positive("100") == 100