From f88c41f97f510b8074190d3079d95cff491a3b48 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 12 Sep 2016 09:51:33 -0500 Subject: [PATCH] Don't use the crazy units data descriptor, store as bits and convert as needed --- speedtest.py | 54 +++++++++++++++------------------------------------- 1 file changed, 15 insertions(+), 39 deletions(-) diff --git a/speedtest.py b/speedtest.py index 86c3a8c..4ba78f6 100755 --- a/speedtest.py +++ b/speedtest.py @@ -483,25 +483,6 @@ class HTTPUploader(threading.Thread): 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 for holding the results of a speedtest, including: @@ -515,11 +496,11 @@ class SpeedtestResults(object): to get a share results image link. """ - download = UnitsDataDescriptor('download', default=0) - upload = UnitsDataDescriptor('upload', default=0) + download = 0 + upload = 0 def __init__(self, download=0, upload=0, ping=0, server=None, - units=('bit', 8)): + units=('bit', 8, 1)): self._download = download self._upload = upload self.ping = ping @@ -543,15 +524,8 @@ class SpeedtestResults(object): if self._share: return self._share - if self.units[1] == 1: - bit_download = self.download * 8 - 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)') + bit_download = self.download * self.units[2] + bit_upload = self.upload * self.units[2] download = int(round(bit_download / 1000.0, 0)) ping = int(round(self.ping, 0)) @@ -637,16 +611,16 @@ class SpeedtestResults(object): 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.download / 1000.0 / 1000.0) / self.units[2], self.units[0], - (self.upload / 1000.0 / 1000.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)): + def __init__(self, config=None, units=('bit', 8, 1)): self.config = {} self.get_config() if config is not None: @@ -1011,7 +985,7 @@ class Speedtest(object): stop = timeit.default_timer() self.results.download = ( - sum(finished) / (stop - start) + (sum(finished) / (stop - start)) * 8 ) if self.results.download > 100000: self.config['threads']['upload'] = 8 @@ -1078,7 +1052,7 @@ class Speedtest(object): stop = timeit.default_timer() self.results.upload = ( - sum(finished) / (stop - start) + (sum(finished) / (stop - start)) * 8 ) return self.results.upload @@ -1125,7 +1099,7 @@ def parse_args(): except AttributeError: pass 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 ' 'not affect the image generated by --share') parser.add_argument('--share', action='store_true', @@ -1315,14 +1289,16 @@ 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[0]), + ((results.download / 1000.0 / 1000.0) / args.units[2], + args.units[0]), quiet) printer('Testing upload speed', quiet, end=('', '\n')[bool(debug)]) speedtest.upload(callback=callback) 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) if args.simple: