I don't have a lot of experience with object-oriented Python and want to refactor a simple command-line tool. My current script simply imports needed libraries, has a few function definitions, uses global variables (which I know is bad practice) and uses argparse all in one .py file, e.g.:
import argparse
dict = {}
#Some code to populate the dict, used both in checking the argument and later in the script
def check_value(value):
if not dict.has_key(value):
raise argparse.ArgumentTypeError("%s is invalid." % value)
return value
parser = argparse.ArgumentParser(…)
parser.add_argument('…', type=check_value, help='…')
args = parser.parse_args()
# Some other code that uses the dict
As well, the way that I handle some of the argument parsing uses a function similar to "abc" which modifies a dictionary I need later in the script. How do I make this less ugly? Or is using global variables acceptable for a simple commandline script?
Aucun commentaire:
Enregistrer un commentaire