Skip to content

Commit c797b75

Browse files
authored
cleanup(examples) (#161)
* cleanup(removed redundant logic, removed broken example typo from printl to print, made a simpler chained logic command and fixed odd formatting) * Worked through pylint errors bringing up code quality from < 3 to > 9.8
1 parent a3bd09a commit c797b75

38 files changed

+1203
-440
lines changed

.pylintrc

Lines changed: 575 additions & 0 deletions
Large diffs are not rendered by default.

examples/blocks_list.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
#!/usr/bin/env python
2-
2+
"""
3+
Retrieve list of blocked items
4+
"""
35
from __future__ import print_function
46

57
import pyslurm
6-
from time import gmtime, strftime
78

8-
def display(block_dict):
99

10+
def display(block_dict):
11+
"""Format output"""
1012
if block_dict:
1113

12-
date_fields = [ ]
14+
date_fields = []
1315

14-
print('{0:*^80}'.format(''))
16+
print("{0:*^80}".format(""))
1517

1618
for key, value in block_dict.items():
1719

@@ -22,19 +24,23 @@ def display(block_dict):
2224
ddate = value[part_key]
2325
if ddate == 0:
2426
print("\t{0:<17} : N/A".format(part_key))
25-
elif ('reason_uid' in part_key) and (value['reason'] is None):
27+
elif ("reason_uid" in part_key) and (value["reason"] is None):
2628
print("\t{0:<17} :".format(part_key))
2729
else:
2830
ddate = pyslurm.epoch2date(ddate)
2931
print("\t{0:<17} : {1}".format(part_key, ddate))
30-
elif part_key == 'connection_type':
31-
print("\t{0:<17} : {1}".format(part_key, pyslurm.get_connection_type(value[part_key])))
32-
elif part_key == 'state':
32+
elif part_key == "connection_type":
33+
print(
34+
"\t{0:<17} : {1}".format(
35+
part_key, pyslurm.get_connection_type(value[part_key])
36+
)
37+
)
38+
elif part_key == "state":
3339
print("\t{0:<17} : {1}".format(part_key, value[part_key]))
3440
else:
3541
print("\t{0:<17} : {1}".format(part_key, value[part_key]))
3642

37-
print('{0:*^80}'.format(''))
43+
print("{0:*^80}".format(""))
3844

3945

4046
if __name__ == "__main__":
@@ -43,15 +49,11 @@ def display(block_dict):
4349
try:
4450
a.load()
4551
block_dict = a.get()
46-
except ValueError as e:
47-
print("Block query failed - {0}".format(e.args[0]))
52+
except ValueError as value_error:
53+
print("Block query failed - {0}".format(value_error.args[0]))
4854
else:
49-
if len(block_dict) > 0:
50-
55+
if block_dict:
5156
display(block_dict)
52-
print()
53-
print("Block IDs - {0}".format(a.ids()))
54-
print()
55-
57+
print("\nBlock IDs - {0}\n".format(a.ids()))
5658
else:
5759
print("No Blocks found !")

examples/blocks_list2.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#!/usr/bin/env python
2-
2+
"""
3+
Retrieve list of blocked items
4+
"""
35
from __future__ import print_function
46

5-
import pyslurm
6-
import sys
77
from time import sleep
88

9-
class DictDiffer(object):
9+
import pyslurm
10+
1011

12+
class DictDiffer:
1113
"""
1214
http://stackoverflow.com/questions/1165352/fast-comparison-between-two-python-dictionary
1315
@@ -16,20 +18,37 @@ class DictDiffer(object):
1618
(2) items removed
1719
(3) keys same in both but changed values
1820
(4) keys same in both and unchanged values
19-
"""
21+
"""
2022

2123
def __init__(self, current_dict, past_dict):
24+
"""set class attr"""
2225
self.current_dict, self.past_dict = current_dict, past_dict
23-
self.set_current, self.set_past = set(current_dict.keys()), set(past_dict.keys())
26+
self.set_current, self.set_past = (
27+
set(current_dict.keys()),
28+
set(past_dict.keys()),
29+
)
2430
self.intersect = self.set_current.intersection(self.set_past)
31+
2532
def added(self):
33+
"""Check if added"""
2634
return self.set_current - self.intersect
35+
2736
def removed(self):
37+
"""Check if removed"""
2838
return self.set_past - self.intersect
39+
2940
def changed(self):
30-
return set(o for o in self.intersect if self.past_dict[o] != self.current_dict[o])
41+
"""Check if changed"""
42+
return set(
43+
o for o in self.intersect if self.past_dict[o] != self.current_dict[o]
44+
)
45+
3146
def unchanged(self):
32-
return set(o for o in self.intersect if self.past_dict[o] == self.current_dict[o])
47+
"""Check for no change"""
48+
return set(
49+
o for o in self.intersect if self.past_dict[o] == self.current_dict[o]
50+
)
51+
3352

3453
if __name__ == "__main__":
3554

@@ -45,14 +64,17 @@ def unchanged(self):
4564

4665
sleep(0.5)
4766

48-
while 1:
49-
67+
while True:
5068
new_dict = a.get()
5169
newUpdate = a.lastUpdate()
5270
if newUpdate > lastUpdate:
5371

5472
lastUpdate = a.lastUpdate()
55-
print("Block data update time changed - {0}".format(pyslurm.epoch2date(lastUpdate)))
73+
print(
74+
"Block data update time changed - {0}".format(
75+
pyslurm.epoch2date(lastUpdate)
76+
)
77+
)
5678

5779
b = DictDiffer(block_dict, new_dict)
5880
if b.changed():
@@ -64,13 +86,9 @@ def unchanged(self):
6486
if b.removed():
6587
print("\tRemoved block {0}".format(b.removed()))
6688
change = 1
67-
6889
if change == 0:
6990
print("\tBut no data was changed !")
7091
change = 0
71-
7292
block_dict = new_dict
7393

7494
sleep(interval)
75-
76-
sys.exit()

examples/cancel_job.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#!/usr/bin/env python
2-
2+
"""
3+
Cancel a scheduled job
4+
"""
35
from __future__ import print_function
46

57
import pyslurm
68

79
try:
810
rc = pyslurm.slurm_kill_job(51, 9, 0)
9-
except ValueError as e:
10-
print("Cancel job error - {0}".format(e.args[0]))
11+
except ValueError as value_error:
12+
print("Cancel job error - {0}".format(value_error.args[0]))
1113
else:
1214
print("Success - cancelled job")

examples/checkpoint_job.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#!/usr/bin/env python
2-
2+
"""
3+
Retrieve a jobs checkpoint status
4+
"""
35
from __future__ import print_function
46

57
import pyslurm
68

79
try:
8-
time = pyslurm.slurm_checkpoint_able(2,0)
9-
except ValueError as e:
10-
print("Checkpointable job failed - {0}".format(e.args[0]))
10+
time = pyslurm.slurm_checkpoint_able(2, 0)
11+
except ValueError as value_error:
12+
print("Checkpointable job failed - {0}".format(value_error.args[0]))
1113
else:
1214
print("Job can be checkpointed at {0}".format(time))

examples/controllers.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
#!/usr/bin/env python
2-
2+
"""
3+
Retrieve list up Slurm controllers
4+
"""
35
from __future__ import print_function
46

5-
import pyslurm
67
import socket
7-
import sys
88

9-
def controller_up(controller=1):
9+
import pyslurm
1010

11+
12+
def controller_up(controller=1):
13+
"""Check if controller up via ping"""
1114
try:
1215
pyslurm.slurm_ping(controller)
13-
except valueError as e:
14-
print("Failed - {0}".format(e.args[0]))
16+
except ValueError as value_error:
17+
print("Failed - {0}".format(value_error.args[0]))
1518
else:
1619
print("Success")
1720

21+
1822
if __name__ == "__main__":
1923

2024
print()
@@ -37,11 +41,11 @@ def controller_up(controller=1):
3741
print("\nPinging SLURM controllers")
3842

3943
if primary:
40-
print("\tPrimary .....", end=' ')
44+
print("\tPrimary .....", end=" ")
4145
controller_up()
4246

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

examples/debug_levels.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
#!/usr/bin/env python
2-
2+
"""
3+
How to set Slurm debug level
4+
"""
35
from __future__ import print_function
46

57
import pyslurm
68

7-
SLURM_DEBUG=1
8-
SCHED_DEBUG=0
9+
SLURM_DEBUG = 1
10+
SCHED_DEBUG = 0
911

1012
try:
1113
print("Setting Slurmd debug level to {0}".format(SLURM_DEBUG))
1214
rc = pyslurm.slurm_set_debug_level(SLURM_DEBUG)
1315
if rc == 0:
1416
print("\tSuccess...Slurmd debug level updated to {0}".format(SLURM_DEBUG))
15-
except ValueError as e:
16-
print("\tError - {0}".format(e.args[0]))
17+
except ValueError as value_error:
18+
print("\tError - {0}".format(value_error.args[0]))
1719

1820
try:
1921
print("Setting Schedlog debug level to {0}".format(SCHED_DEBUG))
2022
rc = pyslurm.slurm_set_schedlog_level(SCHED_DEBUG)
2123
if rc == 0:
2224
print("\tSuccess...Schedlog log level updated to {0}".format(SCHED_DEBUG))
23-
except ValueError as e:
24-
print("\tError - {0}".format(e.args[0]))
25+
except ValueError as value_error:
26+
print("\tError - {0}".format(value_error.args[0]))

examples/hostlist.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env python
2-
2+
"""
3+
Retrieve Slurm hosts
4+
"""
35
from __future__ import print_function
46

57
import pyslurm
@@ -9,7 +11,6 @@
911
hosts = "dummy0,dummy1,dummy1,dummy3,dummy4"
1012
print("Creating hostlist ...... with {0}".format(hosts))
1113
if b.create(hosts):
12-
1314
print()
1415
print("\tHost list count is {0}".format(b.count()))
1516
node = "dummy3"

examples/job_test.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#!/usr/bin/env python
2-
2+
"""
3+
Class to access/modify Slurm Job Information.
4+
"""
35
from __future__ import print_function
46

57
import pyslurm
68

79
try:
810
a = pyslurm.job()
911

10-
jobs = a.get()
12+
jobs = a.get()
1113
print(jobs)
12-
except ValueError as e:
13-
print("Job list error - {0}".format(e.args[0]))
14+
except ValueError as value_error:
15+
print("Job list error - {0}".format(value_error.args[0]))

examples/jobs_list.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
#!/usr/bin/env python
2-
2+
"""
3+
List Slurm jobs
4+
"""
35
from __future__ import print_function
46

57
import pyslurm
6-
import sys
78

8-
from time import gmtime, strftime, sleep
99

1010
def display(job_dict):
11-
11+
"""Format output"""
1212
if job_dict:
1313

14-
time_fields = ['time_limit']
14+
time_fields = ["time_limit"]
1515

16-
date_fields = ['start_time',
17-
'submit_time',
18-
'end_time',
19-
'eligible_time',
20-
'resize_time']
16+
date_fields = [
17+
"start_time",
18+
"submit_time",
19+
"end_time",
20+
"eligible_time",
21+
"resize_time",
22+
]
2123

2224
for key, value in sorted(job_dict.items()):
2325

@@ -40,6 +42,7 @@ def display(job_dict):
4042

4143
print("-" * 80)
4244

45+
4346
if __name__ == "__main__":
4447

4548
try:
@@ -54,9 +57,9 @@ def display(job_dict):
5457
print("Number of Jobs - {0}".format(len(jobs)))
5558
print()
5659

57-
pending = a.find('job_state', 'PENDING')
58-
running = a.find('job_state', 'RUNNING')
59-
held = a.find('job_state', 'RUNNING')
60+
pending = a.find("job_state", "PENDING")
61+
running = a.find("job_state", "RUNNING")
62+
held = a.find("job_state", "RUNNING")
6063

6164
print("Number of pending jobs - {0}".format(len(pending)))
6265
print("Number of running jobs - {0}".format(len(running)))
@@ -67,5 +70,5 @@ def display(job_dict):
6770
print()
6871
else:
6972
print("No jobs found !")
70-
except ValueError as e:
71-
print("Job query failed - {0}".format(e.args[0]))
73+
except ValueError as value_error:
74+
print("Job query failed - {0}".format(value_error.args[0]))

0 commit comments

Comments
 (0)