commit 992ea07eb0eb3baf232ca9c0278794ec632bca6f Author: juga0 juga@riseup.net Date: Wed Aug 29 14:45:08 2018 +0000
Reorder methods in BWV3Line
1. magic methods 2. classmethods 3. staticmethods 4. properties 5. methods --- sbws/lib/v3bwfile.py | 130 +++++++++++++++++++++++++-------------------------- 1 file changed, 65 insertions(+), 65 deletions(-)
diff --git a/sbws/lib/v3bwfile.py b/sbws/lib/v3bwfile.py index d663445..9324bde 100644 --- a/sbws/lib/v3bwfile.py +++ b/sbws/lib/v3bwfile.py @@ -249,39 +249,31 @@ class V3BWLine(object): [setattr(self, k, v) for k, v in kwargs.items() if k in BW_EXTRA_ARG_KEYVALUES]
- @property - def bw_keyvalue_tuple_ls(self): - """Return list of KeyValue Bandwidth Line tuples.""" - # sort the list to generate determinist headers - keyvalue_tuple_ls = sorted([(k, v) for k, v in self.__dict__.items() - if k in BW_KEYVALUES]) - return keyvalue_tuple_ls - - @property - def bw_keyvalue_v110str_ls(self): - """Return list of KeyValue Bandwidth Line strings following - spec v1.1.0. - """ - bw_keyvalue_str = [KEYVALUE_SEP_V110 .join([k, str(v)]) - for k, v in self.bw_keyvalue_tuple_ls] - return bw_keyvalue_str - - @property - def bw_strv110(self): - """Return Bandwidth Line string following spec v1.1.0.""" - bw_line_str = BW_KEYVALUE_SEP_V110.join( - self.bw_keyvalue_v110str_ls) + LINE_SEP - if len(bw_line_str) > BW_LINE_SIZE: - # if this is the case, probably there are too many KeyValues, - # or the limit needs to be changed in Tor - log.warn("The bandwidth line %s is longer than %s", - len(bw_line_str), BW_LINE_SIZE) - return bw_line_str - def __str__(self): return self.bw_strv110
@classmethod + def from_results(cls, results): + success_results = [r for r in results if isinstance(r, ResultSuccess)] + # log.debug('len(success_results) %s', len(success_results)) + node_id = '$' + results[0].fingerprint + bw = cls.bw_from_results(success_results) + kwargs = dict() + kwargs['nick'] = results[0].nickname + if getattr(results[0], 'master_key_ed25519'): + kwargs['master_key_ed25519'] = results[0].master_key_ed25519 + kwargs['rtt'] = cls.rtt_from_results(success_results) + kwargs['time'] = cls.last_time_from_results(results) + kwargs.update(cls.result_types_from_results(results)) + bwl = cls(node_id, bw, **kwargs) + return bwl + + @classmethod + def from_data(cls, data, fingerprint): + assert fingerprint in data + return cls.from_results(data[fingerprint]) + + @classmethod def from_bw_line_v110(cls, line): assert isinstance(line, str) kwargs = dict([kv.split(KEYVALUE_SEP_V110) @@ -294,6 +286,24 @@ class V3BWLine(object): return bw_line
@staticmethod + def last_time_from_results(results): + return unixts_to_isodt_str(round(max([r.time for r in results]))) + + @staticmethod + def rtt_from_results(results): + # convert from miliseconds to seconds + rtts = [(round(rtt * 1000)) for r in results for rtt in r.rtts] + rtt = round(median(rtts)) + return rtt + + @staticmethod + def result_types_from_results(results): + rt_dict = dict([(result_type_to_key(rt.value), + num_results_of_type(results, rt.value)) + for rt in _ResultType]) + return rt_dict + + @staticmethod def bw_from_results(results): median_bw = median([dl['amount'] / dl['duration'] for r in results for dl in r.downloads]) @@ -319,44 +329,34 @@ class V3BWLine(object): bw_kb = max(round(bw / 1024), 1) return bw_kb
- @staticmethod - def last_time_from_results(results): - return unixts_to_isodt_str(round(max([r.time for r in results]))) - - @staticmethod - def rtt_from_results(results): - # convert from miliseconds to seconds - rtts = [(round(rtt * 1000)) for r in results for rtt in r.rtts] - rtt = round(median(rtts)) - return rtt - - @staticmethod - def result_types_from_results(results): - rt_dict = dict([(result_type_to_key(rt.value), - num_results_of_type(results, rt.value)) - for rt in _ResultType]) - return rt_dict + @property + def bw_keyvalue_tuple_ls(self): + """Return list of KeyValue Bandwidth Line tuples.""" + # sort the list to generate determinist headers + keyvalue_tuple_ls = sorted([(k, v) for k, v in self.__dict__.items() + if k in BW_KEYVALUES]) + return keyvalue_tuple_ls
- @classmethod - def from_results(cls, results): - success_results = [r for r in results if isinstance(r, ResultSuccess)] - # log.debug('len(success_results) %s', len(success_results)) - node_id = '$' + results[0].fingerprint - bw = cls.bw_from_results(success_results) - kwargs = dict() - kwargs['nick'] = results[0].nickname - if getattr(results[0], 'master_key_ed25519'): - kwargs['master_key_ed25519'] = results[0].master_key_ed25519 - kwargs['rtt'] = cls.rtt_from_results(success_results) - kwargs['time'] = cls.last_time_from_results(results) - kwargs.update(cls.result_types_from_results(results)) - bwl = cls(node_id, bw, **kwargs) - return bwl + @property + def bw_keyvalue_v110str_ls(self): + """Return list of KeyValue Bandwidth Line strings following + spec v1.1.0. + """ + bw_keyvalue_str = [KEYVALUE_SEP_V110 .join([k, str(v)]) + for k, v in self.bw_keyvalue_tuple_ls] + return bw_keyvalue_str
- @classmethod - def from_data(cls, data, fingerprint): - assert fingerprint in data - return cls.from_results(data[fingerprint]) + @property + def bw_strv110(self): + """Return Bandwidth Line string following spec v1.1.0.""" + bw_line_str = BW_KEYVALUE_SEP_V110.join( + self.bw_keyvalue_v110str_ls) + LINE_SEP + if len(bw_line_str) > BW_LINE_SIZE: + # if this is the case, probably there are too many KeyValues, + # or the limit needs to be changed in Tor + log.warn("The bandwidth line %s is longer than %s", + len(bw_line_str), BW_LINE_SIZE) + return bw_line_str
class V3BWFile(object):