Don't concern SpeedtestResults with units

This commit is contained in:
Matt Martz 2016-09-13 08:50:15 -05:00
parent 6fffcd5b63
commit d0c927e8ae
1 changed files with 21 additions and 29 deletions

View File

@ -499,8 +499,7 @@ class SpeedtestResults(object):
download = 0 download = 0
upload = 0 upload = 0
def __init__(self, download=0, upload=0, ping=0, server=None, def __init__(self, download=0, upload=0, ping=0, server=None):
units=('bit', 8, 1)):
self._download = download self._download = download
self._upload = upload self._upload = upload
self.ping = ping self.ping = ping
@ -511,8 +510,6 @@ class SpeedtestResults(object):
self._share = None self._share = None
self.timestamp = datetime.datetime.utcnow().isoformat() self.timestamp = datetime.datetime.utcnow().isoformat()
self.units = units
def __repr__(self): def __repr__(self):
return repr(self.dict()) return repr(self.dict())
@ -524,12 +521,9 @@ class SpeedtestResults(object):
if self._share: if self._share:
return self._share return self._share
bit_download = self.download * self.units[2] download = int(round(self.download / 1000.0, 0))
bit_upload = self.upload * self.units[2]
download = int(round(bit_download / 1000.0, 0))
ping = int(round(self.ping, 0)) ping = int(round(self.ping, 0))
upload = int(round(bit_upload / 1000.0, 0)) upload = int(round(self.upload / 1000.0, 0))
# Build the request to send results back to speedtest.net # Build the request to send results back to speedtest.net
# We use a list instead of a dict because the API expects parameters # We use a list instead of a dict because the API expects parameters
@ -607,20 +601,11 @@ class SpeedtestResults(object):
}) })
return json.dumps(self.dict(), **kwargs) return json.dumps(self.dict(), **kwargs)
def simple(self):
return """Ping: %s ms
Download: %0.2f M%s/s
Upload: %0.2f M%s/s""" % (self.ping,
(self.download / 1000.0 / 1000.0) / self.units[2],
self.units[0],
(self.upload / 1000.0 / 1000.0) / self.units[2],
self.units[0])
class Speedtest(object): class Speedtest(object):
"""Class for performing standard speedtest.net testing operations""" """Class for performing standard speedtest.net testing operations"""
def __init__(self, config=None, units=('bit', 8, 1)): def __init__(self, config=None):
self.config = {} self.config = {}
self.get_config() self.get_config()
if config is not None: if config is not None:
@ -630,7 +615,7 @@ class Speedtest(object):
self.closest = [] self.closest = []
self.best = {} self.best = {}
self.results = SpeedtestResults(units=units) self.results = SpeedtestResults()
def get_config(self): def get_config(self):
"""Download the speedtest.net configuration and return only the data """Download the speedtest.net configuration and return only the data
@ -973,7 +958,7 @@ class Speedtest(object):
stop = timeit.default_timer() stop = timeit.default_timer()
self.results.download = ( self.results.download = (
(sum(finished) / (stop - start)) * 8 (sum(finished) / (stop - start)) * 8.0
) )
if self.results.download > 100000: if self.results.download > 100000:
self.config['threads']['upload'] = 8 self.config['threads']['upload'] = 8
@ -1040,7 +1025,7 @@ class Speedtest(object):
stop = timeit.default_timer() stop = timeit.default_timer()
self.results.upload = ( self.results.upload = (
(sum(finished) / (stop - start)) * 8 (sum(finished) / (stop - start)) * 8.0
) )
return self.results.upload return self.results.upload
@ -1087,7 +1072,7 @@ def parse_args():
except AttributeError: except AttributeError:
pass pass
parser.add_argument('--bytes', dest='units', action='store_const', parser.add_argument('--bytes', dest='units', action='store_const',
const=('byte', 1, 8), default=('bit', 8, 1), const=('byte', 8), default=('bit', 1),
help='Display values in bytes instead of bits. Does ' help='Display values in bytes instead of bits. Does '
'not affect the image generated by --share') 'not affect the image generated by --share')
parser.add_argument('--share', action='store_true', parser.add_argument('--share', action='store_true',
@ -1098,7 +1083,8 @@ def parse_args():
'information') 'information')
parser.add_argument('--csv', action='store_true', default=False, parser.add_argument('--csv', action='store_true', default=False,
help='Suppress verbose output, only show basic ' help='Suppress verbose output, only show basic '
'information in CSV format') 'information in CSV format. Speeds listed in '
'bit/s and not affected by --bytes')
parser.add_argument('--csv-delimiter', default=',', type=PARSER_TYPE_STR, parser.add_argument('--csv-delimiter', default=',', type=PARSER_TYPE_STR,
help='Single character delimiter to use in CSV ' help='Single character delimiter to use in CSV '
'output. Default ","') 'output. Default ","')
@ -1106,7 +1092,8 @@ def parse_args():
help='Print CSV headers') help='Print CSV headers')
parser.add_argument('--json', action='store_true', default=False, parser.add_argument('--json', action='store_true', default=False,
help='Suppress verbose output, only show basic ' help='Suppress verbose output, only show basic '
'information in JSON format') 'information in JSON format. Speeds listed in '
'bit/s and not affected by --bytes')
parser.add_argument('--list', action='store_true', parser.add_argument('--list', action='store_true',
help='Display a list of speedtest.net servers ' help='Display a list of speedtest.net servers '
'sorted by distance') 'sorted by distance')
@ -1217,7 +1204,7 @@ def shell():
printer('Retrieving speedtest.net configuration...', quiet) printer('Retrieving speedtest.net configuration...', quiet)
try: try:
speedtest = Speedtest(units=args.units) speedtest = Speedtest()
except ConfigRetrievalError: except ConfigRetrievalError:
printer('Cannot retrieve speedtest configuration') printer('Cannot retrieve speedtest configuration')
sys.exit(1) sys.exit(1)
@ -1277,7 +1264,7 @@ def shell():
end=('', '\n')[bool(debug)]) end=('', '\n')[bool(debug)])
speedtest.download(callback=callback) speedtest.download(callback=callback)
printer('Download: %0.2f M%s/s' % printer('Download: %0.2f M%s/s' %
((results.download / 1000.0 / 1000.0) / args.units[2], ((results.download / 1000.0 / 1000.0) / args.units[1],
args.units[0]), args.units[0]),
quiet) quiet)
@ -1285,12 +1272,17 @@ def shell():
end=('', '\n')[bool(debug)]) end=('', '\n')[bool(debug)])
speedtest.upload(callback=callback) speedtest.upload(callback=callback)
printer('Upload: %0.2f M%s/s' % printer('Upload: %0.2f M%s/s' %
((results.upload / 1000.0 / 1000.0) / args.units[2], ((results.upload / 1000.0 / 1000.0) / args.units[1],
args.units[0]), args.units[0]),
quiet) quiet)
if args.simple: if args.simple:
print_(results.simple()) print_('Ping: %s ms\nDownload: %0.2f M%s/s\nUpload: %0.2f M%s/s' %
(results.ping,
(results.download / 1000.0 / 1000.0) / args.units[1],
args.units[0],
(results.upload / 1000.0 / 1000.0) / args.units[1],
args.units[0]))
elif args.csv: elif args.csv:
print_(results.csv(delimiter=args.csv_delimiter)) print_(results.csv(delimiter=args.csv_delimiter))
elif args.json: elif args.json: