June 2019
Beginner to intermediate
770 pages
19h 24m
English
We have a potential Don't Repeat Yourself (DRY) issue between our construction of the argument parser and the use of the arguments to configure the application. We built the arguments using some keys that are repeated.
We can eliminate this repetition by creating some internal configurations that map to the externally visible values. For example, we might define this global as follows:
dealer_rule_map = {"Hit17": Hit17, "Stand17", Stand17}
We can use it to create the argument parser, as follows:
parser.add_argument( "--dealerhit", action="store", default="Hit17", choices=dealer_rule_map.keys(), dest='dealer_rule')
We can use it to create the working objects, as follows:
dealer_rule = dealer_rule_map[config.dealer_rule]() ...
Read now
Unlock full access