Create a getter for Speedtest.best to raise an exception is get_best_server has not found a best server

This commit is contained in:
Matt Martz 2017-05-12 13:01:59 -05:00
parent 3ebb9734a2
commit ca72d40033
1 changed files with 15 additions and 2 deletions

View File

@ -321,6 +321,10 @@ class SpeedtestBestServerFailure(SpeedtestException):
"""Unable to determine best server"""
class SpeedtestMissingBestServer(SpeedtestException):
"""get_best_server not called or not able to determine best server"""
def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
source_address=None):
"""Connect to *address* and return the socket object.
@ -953,10 +957,19 @@ class Speedtest(object):
self.servers = {}
self.closest = []
self.best = {}
self._best = {}
self.results = SpeedtestResults(opener=self._opener, secure=secure)
@property
def best(self):
if not self._best:
raise SpeedtestMissingBestServer(
'get_best_server not called or not able to determine best '
'server'
)
return self._best
def get_config(self):
"""Download the speedtest.net configuration and return only the data
we are interested in
@ -1300,7 +1313,7 @@ class Speedtest(object):
self.results.ping = fastest
self.results.server = best
self.best.update(best)
self._best.update(best)
printer('Best Server:\n%r' % best, debug=True)
return best