Starter Argparse and Setup.py templates

I tend to develop my python scripts as small apps, this way I can call and use them from every where in command line without relying on the calling the python scripts

Here’s a template of the setup.py and argparse template that I always start with.

Setup.py example is for making the application as a package and installable.

Install_requires is a list of packages required by the app.
Entry_points is optional if given, app can be called using the ‘app_name’ from command line.

import os
from setuptools import find_packages, setup
 
setup(
    name= 'name',
    version="3.0",
    packages=find_packages(),
    include_package_data=True,
    zip_safe=False,
    license="Private",
    description= "This is the description",
    author= "author_name",
    author_email= "author_contact_email",
 
    install_requires=["psutil", "pywin32"],
 
    entry_points={
        'console_scripts': ['app_name = app:main']
    }
)
 

Argparse example template

Following a starter template for using argparse.

def main():
    parser = argparse.ArgumentParser(
        description="Pro Thermals: Uses Iges from NX Update to perform thermal analysis on scenery Model")
    parser.add_argument("indir", type=str, help="Input dir ")
    parser.add_argument("outdir", type=str, help="Output dir ")
    parser.add_argument("-s", "--scpath", type=str, help="simulation executable path ",
                        default="W:\\simulation_app\\MSWindows\\bin\\x64")
    parser.add_argument("-o", "--omp-threads", type=int, help="Specifies how many OMP_NUM_THREADS app uses while running ",
                        default=1)
 
    args = parser.parse_args()
 
    input_dir = args.indir or os.getcwd()
    output_dir = args.outdir
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
 
    run_ok = db_thermals(input_dir,
                         output_dir,
                         args.scpath, args.omp_threads)
 
if __name__ == "__main__":
    main()

In the above example, indir and outdir are required positional arguments, while scpath and omp-threads are optional arguments with some defaults.

.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s