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
upload = 0
def __init__(self, download=0, upload=0, ping=0, server=None,
units=('bit', 8, 1)):
def __init__(self, download=0, upload=0, ping=0, server=None):
self._download = download
self._upload = upload
self.ping = ping
@ -511,8 +510,6 @@ class SpeedtestResults(object):
self._share = None
self.timestamp = datetime.datetime.utcnow().isoformat()
self.units = units
def __repr__(self):
return repr(self.dict())
@ -524,12 +521,9 @@ class SpeedtestResults(object):
if self._share:
return self._share
bit_download = self.download * self.units[2]
bit_upload = self.upload * self.units[2]
download = int(round(bit_download / 1000.0, 0))
download = int(round(self.download / 1000.0, 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
# 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)
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 for performing standard speedtest.net testing operations"""
def __init__(self, config=None, units=('bit', 8, 1)):
def __init__(self, config=None):
self.config = {}
self.get_config()
if config is not None:
@ -630,7 +615,7 @@ class Speedtest(object):
self.closest = []
self.best = {}
self.results = SpeedtestResults(units=units)
self.results = SpeedtestResults()
def get_config(self):
"""Download the speedtest.net configuration and return only the data
@ -973,7 +958,7 @@ class Speedtest(object):
stop = timeit.default_timer()
self.results.download = (
(sum(finished) / (stop - start)) * 8
(sum(finished) / (stop - start)) * 8.0
)
if self.results.download > 100000:
self.config['threads']['upload'] = 8
@ -1040,7 +1025,7 @@ class Speedtest(object):
stop = timeit.default_timer()
self.results.upload = (
(sum(finished) / (stop - start)) * 8
(sum(finished) / (stop - start)) * 8.0
)
return self.results.upload
@ -1087,7 +1072,7 @@ def parse_args():
except AttributeError:
pass
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 '
'not affect the image generated by --share')
parser.add_argument('--share', action='store_true',
@ -1098,7 +1083,8 @@ def parse_args():
'information')
parser.add_argument('--csv', action='store_true', default=False,
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,
help='Single character delimiter to use in CSV '
'output. Default ","')
@ -1106,7 +1092,8 @@ def parse_args():
help='Print CSV headers')
parser.add_argument('--json', action='store_true', default=False,
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',
help='Display a list of speedtest.net servers '
'sorted by distance')
@ -1217,7 +1204,7 @@ def shell():
printer('Retrieving speedtest.net configuration...', quiet)
try:
speedtest = Speedtest(units=args.units)
speedtest = Speedtest()
except ConfigRetrievalError:
printer('Cannot retrieve speedtest configuration')
sys.exit(1)
@ -1277,7 +1264,7 @@ def shell():
end=('', '\n')[bool(debug)])
speedtest.download(callback=callback)
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]),
quiet)
@ -1285,12 +1272,17 @@ def shell():
end=('', '\n')[bool(debug)])
speedtest.upload(callback=callback)
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]),
quiet)
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:
print_(results.csv(delimiter=args.csv_delimiter))
elif args.json: