LINK: Python3 Sample Scripts Git Repo
Git Repo contains many sample programmes that explain many concepts and use cases.
Generate requirements.txt
python -m venv venv
source venv/bin/activate
pip install prometheus_client prometheus_flask_exporter flask requests
pip freeze > requirements.txt
deactivate
Argparse
Argparse is very useful for receiving user inputs and provide useful options. Implementation and documentation may take some time to understand, I have created few sample script explaining few scenarios.
epilog "text.: Displays information about script when entered without argument
required=true : Ensure input is provided for this field
default="value" : Set default value for argument if none provided
Help "text" : Help for command option
'-a', '--action' : Argument can be passed with -a or --action
nargs='+' : Allows you to accept multiple values as input for same argument
'arg' : Positional argument compulsory. Works best with subparse
argparser.print_help() : Print help information
Error Handling
Use try: Except
Where possible, group all your commands with in Try: except: block to provide meaningful errors. Most of the time all you need is Exception message and it can be achieved like below
try:
print(Y) #This result in failure as Y not defined
print("This command not executed as previous failed")
except Exception as Err:
print(ERR) #Prints exception message only.
print("More useful error message custom to section")
exit() #Make sure to exit