Skip to content

Commit 983dfc7

Browse files
committed
style: autopep8/black recommendations, f-strings, code cleanup
1 parent 2adf917 commit 983dfc7

33 files changed

+363
-415
lines changed

examples/controllers.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,47 @@
44
"""
55

66
import socket
7-
87
import pyslurm
98

109

11-
def controller_up(controller=1):
10+
def controller_up(controller=0):
1211
"""Check if controller up via ping"""
1312
try:
1413
pyslurm.slurm_ping(controller)
15-
except ValueError as value_error:
16-
print("Failed - {0}".format(value_error.args[0]))
14+
except ValueError as ping_error:
15+
print(f"Failed - {ping_error.args[0]}")
1716
else:
1817
print("Success")
1918

2019

2120
if __name__ == "__main__":
2221

2322
print()
24-
print("PySLURM\t\t{0}".format(pyslurm.version()))
25-
print("SLURM API\t{0}-{1}-{2}\n".format(*pyslurm.slurm_api_version()))
23+
print(f"PySLURM\t\t{pyslurm.version()}")
24+
major, minor, micro = pyslurm.slurm_api_version()
25+
print(f"SLURM API\t{major}-{minor}-{micro}\n")
2626

2727
host = socket.gethostname()
28-
print("Checking host.....{0}".format(host))
28+
print(f"Checking host.....{host}")
2929

3030
try:
3131
a = pyslurm.is_controller(host)
32-
print("\tHost is controller ({0})\n".format(a))
32+
print(f"\tHost is controller ({a})\n")
3333

3434
print("Querying SLURM controllers")
35-
primary, backup = pyslurm.get_controllers()
36-
37-
print("\tPrimary - {0}".format(primary))
38-
print("\tBackup - {0}".format(backup))
39-
40-
print("\nPinging SLURM controllers")
41-
42-
if primary:
43-
print("\tPrimary .....", end=" ")
44-
controller_up()
35+
control_machs = pyslurm.get_controllers()
36+
37+
X = 0
38+
for machine in control_machs:
39+
if X == 0:
40+
print(f"\tPrimary - {machine}")
41+
print("\t\tPing .....", end=" ")
42+
controller_up()
43+
else:
44+
print(f"\tBackup{X} - {machine}")
45+
print("\t\tPing .....", end=" ")
46+
controller_up(X)
47+
X = X + 1
4548

46-
if backup:
47-
print("\tBackup .....", end=" ")
48-
controller_up(2)
4949
except ValueError as value_error:
50-
print("Error - {0}".format(value_error.args[0]))
50+
print(f"Error - {value_error.args[0]}")

examples/debug_levels.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
SCHED_DEBUG = 0
1010

1111
try:
12-
print("Setting Slurmd debug level to {0}".format(SLURM_DEBUG))
12+
print(f"Setting Slurmd debug level to {SLURM_DEBUG}")
1313
rc = pyslurm.slurm_set_debug_level(SLURM_DEBUG)
1414
if rc == 0:
15-
print("\tSuccess...Slurmd debug level updated to {0}".format(SLURM_DEBUG))
15+
print(f"\tSuccess...Slurmd debug level updated to {SLURM_DEBUG}")
1616
except ValueError as value_error:
17-
print("\tError - {0}".format(value_error.args[0]))
17+
print(f"\tError - {value_error.args[0]}")
1818

1919
try:
20-
print("Setting Schedlog debug level to {0}".format(SCHED_DEBUG))
20+
print(f"Setting Schedlog debug level to {SCHED_DEBUG}")
2121
rc = pyslurm.slurm_set_schedlog_level(SCHED_DEBUG)
2222
if rc == 0:
23-
print("\tSuccess...Schedlog log level updated to {0}".format(SCHED_DEBUG))
23+
print(f"\tSuccess...Schedlog log level updated to {SCHED_DEBUG}")
2424
except ValueError as value_error:
25-
print("\tError - {0}".format(value_error.args[0]))
25+
print(f"\tError - {value_error.args[0]}")

examples/hostlist.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,52 @@
77

88
b = pyslurm.hostlist()
99

10-
hosts = "dummy0,dummy1,dummy1,dummy3,dummy4"
11-
print("Creating hostlist ...... with {0}".format(hosts))
12-
if b.create(hosts):
10+
HOSTS = "dummy0,dummy1,dummy1,dummy3,dummy4"
11+
print(f"Creating hostlist ...... with {HOSTS}")
12+
if b.create(HOSTS):
1313
print()
14-
print("\tHost list count is {0}".format(b.count()))
15-
node = "dummy3"
16-
pos = b.find(node)
14+
print(f"\tHost list count is {b.count()}")
15+
NODE = "dummy3"
16+
pos = b.find(NODE)
1717
if pos == -1:
18-
print("Failed to find {0} in list".format(node))
18+
print(f"Failed to find {NODE} in list")
1919
else:
20-
print("\tHost {0} found at position {1}".format(node, pos))
20+
print(f"\tHost {NODE} found at position {pos}")
2121
print("\tCalling uniq on current host list")
2222
b.uniq()
2323

24-
print("\tNew host list is {0}".format(b.get()))
25-
print("\tNew host list count is {0}".format(b.count()))
26-
pos = b.find(node)
24+
print(f"\tNew host list is {b.get()}")
25+
print(f"\tNew host list count is {b.count()}")
26+
pos = b.find(NODE)
2727
if pos == -1:
28-
print("Failed to find {0} in list".format(node))
28+
print(f"Failed to find {NODE}")
2929
else:
30-
print("\tHost {0} found at position {1}".format(node, pos))
30+
print(f"\tHost {NODE} found at position {pos}")
3131

32-
print("\tRanged host list is {0}".format(b.get()))
32+
print(f"\tRanged host list is {b.get()}")
3333
print()
3434

35-
node = "dummy18"
36-
print("\tPushing new entry {0}".format(node))
37-
if b.push(node):
35+
NODE = "dummy18"
36+
print(f"\tPushing new entry {NODE}")
37+
if b.push(NODE):
3838
print("\t\tSuccess !")
39-
print("\tNew ranged list is {0}".format(b.get()))
39+
print(f"\tNew ranged list is {b.get()}")
4040
else:
4141
print("\t\tFailed !")
4242
print()
4343

4444
print("\tDropping first host from list")
4545
name = b.pop()
4646
if name:
47-
print("\t\tDropped host {0} from list".format(name))
48-
print("\t\tNew host count is {0}".format(b.count()))
49-
print("\t\tNew host list is {0}".format(b.get()))
47+
print(f"\t\tDropped host {name} from list")
48+
print(f"\t\tNew host count is {b.count()}")
49+
print(f"\t\tNew host list is {b.get()}")
5050
else:
5151
print("\t\tFailed !")
5252

5353
print("Destroying host list")
5454
b.destroy()
55-
print("\tHost listcount is {0}".format(b.count()))
55+
print(f"\tHost listcount is {b.count()}")
5656

5757
else:
5858
print("\tFailed to create initial list !")

examples/job_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
jobs = a.get()
1212
print(jobs)
1313
except ValueError as value_error:
14-
print("Job list error - {0}".format(value_error.args[0]))
14+
print(f"Job list error - {value_error.args[0]}")

examples/jobs_list.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ def display(job_dict):
2222

2323
for key, value in sorted(job_dict.items()):
2424

25-
print("JobID {0} :".format(key))
25+
print(f"JobID {key} :")
2626
for part_key in sorted(value.keys()):
2727

2828
if part_key in time_fields:
29-
print("\t{0:<20} : Infinite".format(part_key))
29+
print(f"\t{part_key:<20} : Infinite")
3030
continue
3131

3232
if part_key in date_fields:
3333

3434
if value[part_key] == 0:
35-
print("\t{0:<20} : N/A".format(part_key))
35+
print(f"\t{part_key:<20} : N/A")
3636
else:
3737
ddate = pyslurm.epoch2date(value[part_key])
38-
print("\t{0:<20} : {1}".format(part_key, ddate))
38+
print(f"\t{part_key:<20} : {ddate}")
3939
else:
40-
print("\t{0:<20} : {1}".format(part_key, value[part_key]))
40+
print("\t{part_key:<20} : {value[part_key]}")
4141

4242
print("-" * 80)
4343

@@ -53,21 +53,21 @@ def display(job_dict):
5353
display(jobs)
5454

5555
print()
56-
print("Number of Jobs - {0}".format(len(jobs)))
56+
print(f"Number of Jobs - {len(jobs)}")
5757
print()
5858

5959
pending = a.find("job_state", "PENDING")
6060
running = a.find("job_state", "RUNNING")
6161
held = a.find("job_state", "RUNNING")
6262

63-
print("Number of pending jobs - {0}".format(len(pending)))
64-
print("Number of running jobs - {0}".format(len(running)))
63+
print(f"Number of pending jobs - {len(pending)}")
64+
print(f"Number of running jobs - {len(running)}")
6565
print()
6666

67-
print("JobIDs in Running state - {0}".format(running))
68-
print("JobIDs in Pending state - {0}".format(pending))
67+
print(f"JobIDs in Running state - {running}")
68+
print(f"JobIDs in Pending state - {pending}")
6969
print()
7070
else:
7171
print("No jobs found !")
7272
except ValueError as value_error:
73-
print("Job query failed - {0}".format(value_error.args[0]))
73+
print(f"Job query failed - {value_error.args[0]}")

examples/jobsteps_layout.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@ def display(steps):
1212

1313
for job, job_step in sorted(steps.items()):
1414

15-
print("Job: {0}".format(job))
15+
print(f"Job: {job}")
1616
for step, step_dict in job_step.items():
1717

18-
print("\tStep: {0}".format(step))
18+
print(f"\tStep: {step}")
1919
for task, value in sorted(step_dict.items()):
2020

2121
if task in date_fields:
2222

2323
if value == 0:
24-
print("\t\t{0:<20} : N/A".format(task))
24+
print(f"\t\t{task:<20} : N/A")
2525
else:
2626
ddate = pyslurm.epoch2date(value)
27-
print("\t\t{0:<20} : {1}".format(task, ddate))
27+
print(f"\t\t{task:<20} : {ddate}")
2828
else:
29-
print("\t\t{0:<20} : {1}".format(task, value))
29+
print(f"\t\t{task:<20} : {value}")
3030

3131

3232
if __name__ == "__main__":
3333

3434
a = pyslurm.jobstep()
35-
steps = a.get()
35+
job_steps = a.get()
3636

37-
if steps:
38-
display(steps)
37+
if job_steps:
38+
display(job_steps)

examples/jobsteps_list.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010

1111
if a:
1212
for job, job_step in sorted(a.items()):
13-
print("Job: {0}".format(job))
13+
print(f"Job: {job}")
1414
for step, step_data in sorted(job_step.items()):
15-
print("\tStep: {0}".format(step))
15+
print(f"\tStep: {step}")
1616
for step_item, item_data in sorted(step_data.items()):
1717
if "start_time" in step_item:
1818
ddate = pyslurm.epoch2date(item_data)
19-
print("\t\t{0:<15} : {1}".format(step_item, ddate))
19+
print(f"\t\t{step_item:<15} : {ddate}")
2020
else:
21-
print("\t\t{0:<15} : {1}".format(step_item, item_data))
21+
print(f"\t\t{step_item:<15} : {item_data}")
2222
layout = steps.layout(job, step)
2323
print("\t\tLayout:")
2424
for name, value in sorted(layout.items()):
25-
print("\t\t\t{0:<15} : {1}".format(name, value))
25+
print(f"\t\t\t{name:<15} : {value}")
2626

27-
print("{0:*^80}".format(""))
27+
print(f"{'':*^80}")
2828
else:
2929
print("No jobsteps found !")

examples/licenses.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@
77
def display(lic_dict):
88
"""Format output"""
99
if lic_dict:
10-
print(
11-
"State last updated : {0}".format(slurm.epoch2date(licenses.lastUpdate()))
12-
)
13-
print("{0:*^80}".format(""))
10+
license_date = slurm.epoch2date(licenses.lastUpdate())
11+
print(f"State last updated : {license_date}")
12+
print(f"{'':*^80}")
1413

1514
for key, value in lic_dict.items():
1615

17-
print("{0} :".format(key))
16+
print(f"{key} :")
1817
for part_key in sorted(value.keys()):
19-
print("\t{0:<17} : {1}".format(part_key, value[part_key]))
18+
print(f"\t{part_key:<17} : {value[part_key]}")
2019

21-
print("{0:*^80}".format(""))
20+
print(f"{'':*^80}")
2221
else:
2322
print("No Licenses found !")
2423

@@ -43,11 +42,11 @@ def display(lic_dict):
4342
new = licenses.lastUpdate()
4443
if new > old:
4544
old = new
46-
print("{0:*^80}".format(""))
45+
print(f"{'':*^80}")
4746
display(lic)
48-
print("{0:*^80}".format(""))
47+
print(f"{'':*^80}")
4948
except ValueError as value_error:
50-
print("License error : {0}".format(value_error.args[0]))
49+
print(f"License error : {value_error.args[0]}")
5150
sys.exit(-1)
5251
except KeyboardInterrupt:
5352
print("Exiting....")

examples/listdb_cluster.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,30 @@
99

1010
def cluster_display(cluster):
1111
"""Format output"""
12-
for key, value in cluster.items():
13-
if key == "accounting":
12+
for cluster_key, cluster_value in cluster.items():
13+
if cluster_key == "accounting":
1414
print("\t accounting {")
15-
for acct_key, acct_value in value.items():
16-
print("\t\t{}={}".format(acct_key, acct_value))
15+
for acct_key, acct_value in cluster_value.items():
16+
print(f"\t\t{acct_key}={acct_value}")
1717
print("\t }")
1818
else:
19-
print("\t{}={}".format(key, value))
19+
print(f"\t{cluster_key}={cluster_value}")
2020

2121

2222
if __name__ == "__main__":
2323
try:
2424
start = (datetime(2016, 12, 1) - datetime(1970, 1, 1)).total_seconds()
2525
end = (datetime(2016, 12, 2) - datetime(1970, 1, 1)).total_seconds() - 1
26-
print("start={}, end={}".format(start, end))
26+
print(f"start={start}, end={end}")
2727
clusters = pyslurm.slurmdb_clusters()
2828
print(clusters.set_cluster_condition(start, end))
2929
clusters_dict = clusters.get()
3030
if clusters_dict:
3131
for key, value in clusters_dict.items():
32-
print("{} Clusters: {}".format("{", key))
32+
print(f"{'{'} Clusters: {key}")
3333
cluster_display(value)
3434
print("}")
3535
else:
3636
print("No cluster found --")
3737
except ValueError as e:
38-
print("Error:{}".format(e.args[0]))
38+
print(f"Error:{e.args[0]}")

0 commit comments

Comments
 (0)