1
0
PiggyBank/piggybank/util.py

35 lines
1.5 KiB
Python
Raw Normal View History

"""Utility functions."""
from argparse import ArgumentParser
from typing import List
from piggybank import __version__, __copyright__, __license__
from piggybank.currencies import CURRENCIES, DEFAULT_CURRENCY
__all__ = ["add_common_arguments_to_parser", "complement_array_of_coins"]
def add_common_arguments_to_parser(parser: ArgumentParser,
include_reverse_flag: bool = True) -> None:
"""Extends ArgumentParser with a common set of arguments that are shared
amongst all parsers in CLI module."""
parser.add_argument("-v", "--version", action="store_true",
help="show program's version and license and exit")
parser.add_argument("--list-currencies", action="store_true",
help="list all supported currencies and exit")
if include_reverse_flag:
parser.add_argument("-r", "--reverse", action="store_true",
help="reverse a set of coins so incomplete set"
"fills with zeros from right. E.g. '8 9' will be"
"interpreted as '8 9 0 0 0 0' instead of"
"'0 0 0 0 8 9'")
def complement_array_of_coins(coins: List[int], currency: str,
_reversed: bool = False) -> List[int]:
"""Complements array of coins up to the count of currency's coins."""
offset_array = [0] * (CURRENCIES[currency]["count"] - len(coins))
return offset_array + coins if not _reversed else coins + offset_array