Skip to content

Commit

Permalink
Run autopep8 on all Python files
Browse files Browse the repository at this point in the history
This change should be a functional no-op.

TODO: script this into the git commit process or at least into the
      source tarball creation step.
  • Loading branch information
klausman committed Jun 3, 2014
1 parent b65aa2a commit bdb42af
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
34 changes: 18 additions & 16 deletions transonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@


class Pingresult:

"""Encapsulate one ping result, including RTT et al"""

def __init__(self, hostname="UNKNOWN", pstats=None, rtt=None, retval=-1):
self.hostname = hostname
self.retval = retval
Expand All @@ -47,8 +49,8 @@ def __init__(self, hostname="UNKNOWN", pstats=None, rtt=None, retval=-1):

def __str__(self):
return ("%s S%s/R%s, maMD: %s/%s/%s/%s" %
(self.hostname, self.pstats.txcount, self.pstats.rxcount,
self.rtt.rmin, self.rtt.ravg, self.rtt.rmax, self.rtt.rmdev))
(self.hostname, self.pstats.txcount, self.pstats.rxcount,
self.rtt.rmin, self.rtt.ravg, self.rtt.rmax, self.rtt.rmdev))


def eprint(fmt, *args):
Expand Down Expand Up @@ -79,8 +81,8 @@ def pinger(host, count):
(retval, output) = subprocess.getstatusoutput(cmd)

for line in output.split("\n"):
#print(line)
if line[2:2+len("packets transmitted")] == "packets transmitted":
# print(line)
if line[2:2 + len("packets transmitted")] == "packets transmitted":
stats = line.split()
if "errors," in stats:
pstat = __Pingstats__(int(stats[0]), int(stats[3]),
Expand All @@ -95,8 +97,8 @@ def pinger(host, count):
r_min, r_avg, r_max, r_mdev = rtts.split("/")
rtts = __RTTstats__(r_min, r_avg, r_max, r_mdev)
continue
res = Pingresult(host, pstat, rtts, retval)
#print(res)
res = Pingresult(host, pstat, rtts, retval)
# print(res)
return res


Expand All @@ -105,6 +107,7 @@ def frl_list(resultlist, _):
return "\n".join(str(x) for x in resultlist)
FORMATTERS["list"] = frl_list


def frl_cell(resultlist, replies):
"""
Format the resultlist as "cells"
Expand All @@ -124,7 +127,7 @@ def frl_cell(resultlist, replies):
else:
counts[0] += 1
res.append(pres.hostname)
return " ".join(res)+"\n%i up, %i down" % (counts[0], counts[1])
return " ".join(res) + "\n%i up, %i down" % (counts[0], counts[1])
FORMATTERS["cell"] = frl_cell


Expand All @@ -143,7 +146,7 @@ def frl_ccell(resultlist, replies):
else:
counts[0] += 1
res.append(".")
return "".join(res)+"\n%i up, %i down" % (counts[0], counts[1])
return "".join(res) + "\n%i up, %i down" % (counts[0], counts[1])
FORMATTERS["ccell"] = frl_ccell


Expand Down Expand Up @@ -185,8 +188,8 @@ def main():
print_version()
sys.exit(0)

cmdp = argparse.ArgumentParser(description=
'Ping hosts in parallel and show results')
cmdp = argparse.ArgumentParser(
description='Ping hosts in parallel and show results')
cmdp.add_argument('targets', metavar='target', nargs='+',
help='Hostname or IPv4 to ping')
cmdp.add_argument('--count', "-c", metavar='count', default=5, type=int,
Expand All @@ -200,9 +203,9 @@ def main():
'Note: the actual number will be the minimum of this and '
'the number of hosts to ping')
cmdp.add_argument('--mode', '-m', metavar='mode',
help='Output mode, one of %s (list)' %
(", ".join(sorted(FORMATTERS.keys()))),
choices=FORMATTERS.keys(), default='list')
help='Output mode, one of %s (list)' %
(", ".join(sorted(FORMATTERS.keys()))),
choices=FORMATTERS.keys(), default='list')
cmdp.add_argument('--terse', '-t', default=False,
action="store_true", help='Terse output. This will not '
'output anything except whatever the result formatter '
Expand All @@ -216,7 +219,6 @@ def main():
'number of replies, even if larger than number of '
'requests sent.')


args = cmdp.parse_args()

TERSE = args.terse
Expand All @@ -239,10 +241,10 @@ def main():
ppinger = partial(pinger, count=args.count)
results = pool.map(ppinger, args.targets)
end = time.time()
#print(results)
# print(results)
print(formatresultlist(results, args.mode, args.replies))
eprint("Time taken: %.3f seconds (%.3f per host)" %
(end-start, (end-start)/len(args.targets)))
(end - start, (end - start) / len(args.targets)))


if __name__ == "__main__":
Expand Down
29 changes: 17 additions & 12 deletions transonic2.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@
__RTTstats__ = namedtuple('__RTTstats__', "rmin ravg rmax rmdev")

# The next two functions make partial() objects pickleable in 2.6


def _pickle_partial(obj):
"""Pickle a functools.partial()"""
return _unpickle_partial, (obj.func, obj.args, obj.keywords)


def _unpickle_partial(func, args, keywords):
"""Unpickle a functools.partial()"""
# pylint: disable-msg=W0142
Expand All @@ -47,7 +50,9 @@ def _unpickle_partial(func, args, keywords):


class Pingresult:

"""Encapsulate one ping result, including RTT et al"""

def __init__(self, hostname="UNKNOWN", pstats=None, rtt=None):
self.hostname = hostname
if pstats:
Expand All @@ -61,8 +66,8 @@ def __init__(self, hostname="UNKNOWN", pstats=None, rtt=None):

def __str__(self):
return ("%s S%s/R%s, maMD: %s/%s/%s/%s" %
(self.hostname, self.pstats.txcount, self.pstats.rxcount,
self.rtt.rmin, self.rtt.ravg, self.rtt.rmax, self.rtt.rmdev))
(self.hostname, self.pstats.txcount, self.pstats.rxcount,
self.rtt.rmin, self.rtt.ravg, self.rtt.rmax, self.rtt.rmdev))


def eprint(fmt, *args):
Expand Down Expand Up @@ -92,27 +97,27 @@ def pinger(host, count):
cmd = ['ping', '-W', '1', '-c', str(count), '-q', host]
pcomm = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output, _ = pcomm.communicate() # we drop stderr and ignore it
output, _ = pcomm.communicate() # we drop stderr and ignore it

# pylint gets confused here; pylint: disable-msg=E1103
for line in output.split("\n"):
if line[2:2+len("packets transmitted")] == "packets transmitted":
if line[2:2 + len("packets transmitted")] == "packets transmitted":
stats = line.split()
if "errors," in stats:
pstat = __Pingstats__(int(stats[0]), int(stats[3]),
int(stats[7][:-1]), int(stats[11][:-2]))
int(stats[7][:-1]), int(stats[11][:-2]))
else:
pstat = __Pingstats__(int(stats[0]), int(stats[3]),
int(stats[5][:-1]), int(stats[9][:-2]))
int(stats[5][:-1]), int(stats[9][:-2]))
continue

if line.startswith("rtt min/avg/max/mdev"):
rtts = line.split(None, 4)[3]
r_min, r_avg, r_max, r_mdev = rtts.split("/")
rtts = __RTTstats__(r_min, r_avg, r_max, r_mdev)
continue
res = Pingresult(host, pstat, rtts)
#print(res)
res = Pingresult(host, pstat, rtts)
# print(res)
return res


Expand Down Expand Up @@ -141,7 +146,7 @@ def frl_cell(resultlist, replies):
else:
counts[0] += 1
res.append(pres.hostname)
return " ".join(res)+"\n%i up, %i down" % (counts[0], counts[1])
return " ".join(res) + "\n%i up, %i down" % (counts[0], counts[1])
FORMATTERS["cell"] = frl_cell


Expand All @@ -160,7 +165,7 @@ def frl_ccell(resultlist, replies):
else:
counts[0] += 1
res.append(".")
return "".join(res)+"\n%i up, %i down" % (counts[0], counts[1])
return "".join(res) + "\n%i up, %i down" % (counts[0], counts[1])
FORMATTERS["ccell"] = frl_ccell


Expand Down Expand Up @@ -253,10 +258,10 @@ def main():
start = time.time()
results = pool.map(ppinger, arguments)
end = time.time()
#print(results)
# print(results)
print(formatresultlist(results, opts.mode, opts.replies))
eprint("Time taken: %.3f seconds (%.3f per host)" %
(end-start, (end-start)/len(arguments)))
(end - start, (end - start) / len(arguments)))

if __name__ == "__main__":
main()

0 comments on commit bdb42af

Please sign in to comment.