Don't use the crazy units data descriptor, store as bits and convert as needed

This commit is contained in:
Matt Martz 2016-09-12 09:51:33 -05:00
parent 64b03777da
commit f88c41f97f
1 changed files with 15 additions and 39 deletions

View File

@ -483,25 +483,6 @@ class HTTPUploader(threading.Thread):
self.result = sum(self.request.data.total) self.result = sum(self.request.data.total)
class UnitsDataDescriptor(object):
"""Data Descriptor class, that converts values from Bytes to specified
units when setting the value.
"""
def __init__(self, name, default=None):
self.name = name
self.default = default
def __get__(self, obj, objtype=None):
return getattr(obj, '_%s' % self.name, self.default)
def __set__(self, obj, val):
setattr(obj, '_%s' % self.name, val * obj.units[1])
def __delete__(self, obj):
raise NotImplementedError()
class SpeedtestResults(object): class SpeedtestResults(object):
"""Class for holding the results of a speedtest, including: """Class for holding the results of a speedtest, including:
@ -515,11 +496,11 @@ class SpeedtestResults(object):
to get a share results image link. to get a share results image link.
""" """
download = UnitsDataDescriptor('download', default=0) download = 0
upload = UnitsDataDescriptor('upload', default=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)): units=('bit', 8, 1)):
self._download = download self._download = download
self._upload = upload self._upload = upload
self.ping = ping self.ping = ping
@ -543,15 +524,8 @@ class SpeedtestResults(object):
if self._share: if self._share:
return self._share return self._share
if self.units[1] == 1: bit_download = self.download * self.units[2]
bit_download = self.download * 8 bit_upload = self.upload * self.units[2]
bit_upload = self.upload * 8
elif self.units[1] == 8:
bit_download = self.download
bit_upload = self.upload
else:
raise SpeedtestConfigError('Unknown units, valid configurations '
'are ("bit", 8) and ("byte", 1)')
download = int(round(bit_download / 1000.0, 0)) download = int(round(bit_download / 1000.0, 0))
ping = int(round(self.ping, 0)) ping = int(round(self.ping, 0))
@ -637,16 +611,16 @@ class SpeedtestResults(object):
return """Ping: %s ms return """Ping: %s ms
Download: %0.2f M%s/s Download: %0.2f M%s/s
Upload: %0.2f M%s/s""" % (self.ping, Upload: %0.2f M%s/s""" % (self.ping,
(self.download / 1000.0 / 1000.0), (self.download / 1000.0 / 1000.0) / self.units[2],
self.units[0], self.units[0],
(self.upload / 1000.0 / 1000.0), (self.upload / 1000.0 / 1000.0) / self.units[2],
self.units[0]) 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)): def __init__(self, config=None, units=('bit', 8, 1)):
self.config = {} self.config = {}
self.get_config() self.get_config()
if config is not None: if config is not None:
@ -1011,7 +985,7 @@ class Speedtest(object):
stop = timeit.default_timer() stop = timeit.default_timer()
self.results.download = ( self.results.download = (
sum(finished) / (stop - start) (sum(finished) / (stop - start)) * 8
) )
if self.results.download > 100000: if self.results.download > 100000:
self.config['threads']['upload'] = 8 self.config['threads']['upload'] = 8
@ -1078,7 +1052,7 @@ class Speedtest(object):
stop = timeit.default_timer() stop = timeit.default_timer()
self.results.upload = ( self.results.upload = (
sum(finished) / (stop - start) (sum(finished) / (stop - start)) * 8
) )
return self.results.upload return self.results.upload
@ -1125,7 +1099,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), default=('bit', 8), const=('byte', 1, 8), default=('bit', 8, 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',
@ -1315,14 +1289,16 @@ 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[0]), ((results.download / 1000.0 / 1000.0) / args.units[2],
args.units[0]),
quiet) quiet)
printer('Testing upload speed', quiet, printer('Testing upload speed', quiet,
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[0]), ((results.upload / 1000.0 / 1000.0) / args.units[2],
args.units[0]),
quiet) quiet)
if args.simple: if args.simple: