Skip to content

Commit 66d126e

Browse files
committed
Repeated args make this a good candidate for OO.
1 parent 54fd0c5 commit 66d126e

File tree

1 file changed

+61
-39
lines changed

1 file changed

+61
-39
lines changed

python/stat_runner.py

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,77 @@
1212
import subprocess
1313
import sys
1414

15-
def determine_runner(script_path):
16-
dot_split = script_path.split(os.extsep)
17-
if dot_split[-1] == "rb":
18-
return ["ruby", script_path, "csv"]
19-
else:
20-
return ["python3", "-m", script_path, "csv"]
15+
class StatRunner(object):
16+
17+
def __init__(self, script_path, output_encoding="utf-8"):
18+
self.script_path = script_path
19+
self.runstring = self.__determine_runner()
20+
self.runner = self.runstring[0]
21+
self.output_encoding = output_encoding
2122

22-
def write_stat_doc(doc_fname, doc):
23-
with open(doc_fname, "w") as stat_doc:
24-
stat_doc.write(doc)
23+
self.__setup()
2524

26-
def __make_data_dir_name(runner, script_path):
27-
parsed_path = []
28-
if runner == "python3":
29-
parsed_path = script_path.split(".")
30-
else:
31-
parsed_path = script_path.split(os.path.sep)
25+
def __setup(self):
26+
stats_dir = os.path.join(os.curdir, "stats")
27+
if not os.path.isdir(stats_dir) and not os.path.isfile(stats_dir):
28+
print("stats directory does not exist, creating...")
29+
os.mkdir(stats_dir)
30+
self.__ensure_data_dir_exists()
3231

33-
return "-".join(parsed_path)
32+
def __determine_runner(self):
33+
dot_split = self.script_path.split(os.extsep)
34+
if dot_split[-1] == "rb":
35+
return ["ruby", script_path, "csv"]
36+
else:
37+
return ["python3", "-m", script_path, "csv"]
38+
39+
def __write_stat_doc(self, doc_fname, doc):
40+
with open(doc_fname, "w") as stat_doc:
41+
stat_doc.write(doc)
42+
43+
def __make_data_dir_name(self):
44+
parsed_path = []
45+
if self.runner == "python3":
46+
parsed_path = self.script_path.split(".")
47+
else:
48+
parsed_path = self.script_path.split(os.path.sep)
49+
50+
return "-".join(parsed_path)
51+
52+
def __make_data_dir_path(self):
53+
return os.path.join(os.curdir, "stats", self.__make_data_dir_name())
54+
55+
def __ensure_data_dir_exists(self):
56+
data_dir_path = self.__make_data_dir_path()
57+
if not os.path.isdir(data_dir_path) and not os.path.isfile(data_dir_path):
58+
print("data dir for %s does not exist, creating..." % self.script_path)
59+
os.mkdir(data_dir_path)
60+
61+
def __write_data_dump(self, runner, script_path, fname, dump):
62+
data_fname = os.path.join(self.__make_data_dir_path(), fname)
63+
with open(data_fname, "w+") as data:
64+
data.write(dump)
3465

35-
def make_data_dir_path(runner, script_path):
36-
return os.path.join(os.curdir, "stats", __make_data_dir_name(runner, script_path))
66+
def gather_stats(self, iters):
67+
for runcount in range(iters):
68+
print("Run #%s for %s" % (runcount + 1, self.script_path))
69+
out = subprocess.run(self.runstring, stdout=subprocess.PIPE)
70+
self.__write_data_dump(
71+
self.runner,
72+
self.script_path,
73+
"%s.csv" % (runcount + 1),
74+
out.stdout.decode(self.output_encoding)
75+
)
3776

38-
def ensure_data_dir_exists(runner, script_path):
39-
data_dir_path = make_data_dir_path(runner, script_path)
40-
if not os.path.isdir(data_dir_path) and not os.path.isfile(data_dir_path):
41-
print("data dir for %s does not exist, creating..." % script_path)
42-
os.mkdir(data_dir_path)
43-
44-
def write_data_dump(runner, script_path, fname, dump):
45-
data_fname = os.path.join(make_data_dir_path(runner, script_path), fname)
46-
with open(data_fname, "w+") as data:
47-
data.write(dump)
77+
print("Done. Find stats in %s" % self.__make_data_dir_path())
4878

4979
if __name__ == "__main__":
5080
if len(sys.argv) != 3:
5181
print("Usage: python3 %s <script.path> <iters>" % __file__)
5282
exit(1)
5383

54-
stats_dir = os.path.join(os.curdir, "stats")
55-
if not os.path.isdir(stats_dir) and not os.path.isfile(stats_dir):
56-
print("stats directory does not exist, creating...")
57-
os.mkdir(stats_dir)
58-
59-
running_sum = 0
6084
limit = int(sys.argv[2])
61-
runstring = determine_runner(sys.argv[1])
62-
ensure_data_dir_exists(runstring[0], sys.argv[1])
85+
script_path = sys.argv[1]
6386

64-
for runcount in range(limit):
65-
out = subprocess.run(runstring, stdout=subprocess.PIPE)
66-
write_data_dump(runstring[0], sys.argv[1], "%s.csv" % (runcount + 1), out.stdout.decode("utf-8"))
87+
runner = StatRunner(script_path)
88+
runner.gather_stats(limit)

0 commit comments

Comments
 (0)