-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_looper1.py
More file actions
executable file
·50 lines (41 loc) · 1.63 KB
/
py_looper1.py
File metadata and controls
executable file
·50 lines (41 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3
# python_template1.sh
# 27 May 2025 | datainscience.com
#
# This file is the bare minimum for a readable and maintainable shell script
# The key elements are:
# 1) A usage/help function
# 2) Command line arguments - miniumon of "-h" for "help/usage"
# 3) Live/Dry-run flag
# - I like the default to be non-live so I can review the commnads
# 4) Input for 'count' how many times to run the command
import os
import argparse
# Create the parser
# https://docs.python.org/3/library/argparse.html
parser = argparse.ArgumentParser(prog="looper", description="Looper run a command some number of times")
# Add the arguments - argparse builds help automatically
# We'll dd two arguments here
parser.add_argument( "-l", "--live",
action="store_true",
required=False,
default=False,
help="Acturally run commands. Default is to just print the command lines")
parser.add_argument( "-c", "--count",
type=int,
required=True,
help="How many times to run the command")
# Parse the arguments
args = parser.parse_args()
cmd_base = "./test_script.py"
# Python needs a "range()" function for 'for loops'
for c in range(0,args.count):
# Build up the command string
print("++===============================++")
cmd=f"{cmd_base} --arg1 some_text_{c} --failpct 30"
# Always print the ccommand that will be run
print(f"Running: {cmd}")
# If we're in 'live mode' run the command
if args.live:
os.system(cmd)
print("--===============================--\n")