From a17135d85dddca0ac5d0436137d42e6bd8f9864a Mon Sep 17 00:00:00 2001 From: Jeremy Saklad Date: Tue, 3 Aug 2021 13:54:36 -0500 Subject: [PATCH] Move EnumAction to different file This will make it easier to move main() in the future. --- Bone Market Solver.py | 28 +--------------------------- objects/enumaction.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 27 deletions(-) create mode 100644 objects/enumaction.py diff --git a/Bone Market Solver.py b/Bone Market Solver.py index 9ff4bff..b122f3e 100644 --- a/Bone Market Solver.py +++ b/Bone Market Solver.py @@ -19,6 +19,7 @@ from data.declarations import Declaration from data.embellishments import Embellishment from data.skulls import Skull from data.torsos import Torso +from objects.enumaction import EnumAction # This multiplier is applied to the profit margin to avoid losing precision due to rounding. PROFIT_MARGIN_MULTIPLIER = 10000000 @@ -1283,33 +1284,6 @@ def Solve(shadowy_level, bone_market_fluctuations = None, zoological_mania = Non return printer.PrintableSolution(solver) -class EnumAction(argparse.Action): - def __init__(self, **kwargs): - # Pop off the type value - enum = kwargs.pop('type', None) - - nargs = kwargs.pop('nargs', None) - - # Generate choices from the Enum - kwargs.setdefault('choices', tuple(member.name.lower() for member in enum)) - - super(EnumAction, self).__init__(**kwargs) - - self._enum = enum - self._nargs = nargs - - def __call__(self, parser, namespace, values, option_string=None): - # Convert value back into an Enum - enum = self._enum[values.upper()] - - if self._nargs is None or self._nargs == '?': - setattr(namespace, self.dest, enum) - else: - items = getattr(namespace, self.dest, list()) - items.append(enum) - setattr(namespace, self.dest, items) - - def main(): parser = argparse.ArgumentParser( prog='Bone Market Solver', diff --git a/objects/enumaction.py b/objects/enumaction.py new file mode 100644 index 0000000..be1c935 --- /dev/null +++ b/objects/enumaction.py @@ -0,0 +1,30 @@ +__all__ = ['EnumAction'] +__author__ = "Jeremy Saklad" + +from argparse import Action + +class EnumAction(Action): + def __init__(self, **kwargs): + # Pop off the type value + enum = kwargs.pop('type', None) + + nargs = kwargs.pop('nargs', None) + + # Generate choices from the Enum + kwargs.setdefault('choices', tuple(member.name.lower() for member in enum)) + + super(EnumAction, self).__init__(**kwargs) + + self._enum = enum + self._nargs = nargs + + def __call__(self, parser, namespace, values, option_string=None): + # Convert value back into an Enum + enum = self._enum[values.upper()] + + if self._nargs is None or self._nargs == '?': + setattr(namespace, self.dest, enum) + else: + items = getattr(namespace, self.dest, list()) + items.append(enum) + setattr(namespace, self.dest, items)