So I'm not really sure how to do this and if this is a good idea at all. But I have a directory of some python modules and they all have references in there to some specific directories that get created while the sequential execution of one python module after the other.
So in every module I declare globally the variables:
path_to_zips = "../zips"
path_to_unzipped = "../unzipped"
And I thought there must be a better way to handle this. I know that I can declare the paths in a script and then do a
from paths_script import *
But what I would like to do is do give the user the chance to run a script (let's call it set_paths
) that would kind of interactively set those paths and if he doesn't run it there will be some defaults.
So I came up with this
import argparse
# parse some arguments
parser = argparse.ArgumentParser(description="Set the Paths for the entire project")
# get optionsal arguments
parser.add_argument("-data", "--data", dest="data_dir", metavar="", help="folder for all the Zips and SAFEs", default="../data", type=str)
parser.add_argument("-slc", "--slc", dest="slc_dir", metavar="", help="folder for all the SLCs", default="../data/SLC", type=str)
parser.add_argument("-dem", "--dem", dest="dem_dir", metavar="", help="folder for all the DEMs", default="../data/DEM", type=str)
parser.add_argument("-tup", "--tuple", dest="tuple_dir", metavar="", help="folder for all the tuples", default="../data/tuples", type=str)
args =parser.parse_args()
def parse_dates(args):
return args.data_dir, args.slc_dir, args.dem_dir, args.tuple_dir
def main():
# how to define them globally?
data_dir, slc_dir, dem_dir, tuples_dir = parse_dates(args)
if __name__ == "__main__":
main()
But then again I don't know how to treat those *_dir variables to make them importable into another script like:
from set_paths import *; print(data_dir)
Maybe someone understands the issue and has any idea;)