Commit 40a76cc7 authored by juga's avatar juga
Browse files

Reorder methods in BWV3Header

1. magic methods
2. classmethods
3. staticmethods
4. properties
5. methods
parent aea806bf
......@@ -109,52 +109,25 @@ class V3BWHeader(object):
[setattr(self, k, v) for k, v in kwargs.items()
if k in EXTRA_ARG_KEYVALUES]
@property
def keyvalue_unordered_tuple_ls(self):
"""Return list of KeyValue tuples that do not have specific order."""
# sort the list to generate determinist headers
keyvalue_tuple_ls = sorted([(k, v) for k, v in self.__dict__.items()
if k in UNORDERED_KEYVALUES])
return keyvalue_tuple_ls
@property
def keyvalue_tuple_ls(self):
"""Return list of all KeyValue tuples"""
return [('version', self.version)] + self.keyvalue_unordered_tuple_ls
@property
def keyvalue_v110str_ls(self):
"""Return KeyValue list of strings following spec v1.1.0."""
keyvalues = [self.timestamp] + [KEYVALUE_SEP_V110.join([k, v])
for k, v in self.keyvalue_tuple_ls]
return keyvalues
@property
def strv110(self):
"""Return header string following spec v1.1.0."""
header_str = LINE_SEP.join(self.keyvalue_v110str_ls) + LINE_SEP + \
LINE_TERMINATOR
return header_str
@property
def keyvalue_v200_ls(self):
"""Return KeyValue list of strings following spec v2.0.0."""
keyvalue = [self.timestamp] + [KEYVALUE_SEP_V200.join([k, v])
for k, v in self.keyvalue_tuple_ls]
return keyvalue
@property
def strv200(self):
"""Return header string following spec v2.0.0."""
header_str = LINE_SEP.join(self.keyvalue_v200_ls) + LINE_SEP + \
LINE_TERMINATOR
return header_str
def __str__(self):
if self.version == '1.1.0':
return self.strv110
return self.strv200
@classmethod
def from_results(cls, conf, results):
kwargs = dict()
latest_bandwidth = cls.latest_bandwidth_from_results(results)
earliest_bandwidth = cls.earliest_bandwidth_from_results(results)
generator_started = cls.generator_started_from_file(conf)
timestamp = str(latest_bandwidth)
kwargs['latest_bandwidth'] = unixts_to_isodt_str(latest_bandwidth)
kwargs['earliest_bandwidth'] = unixts_to_isodt_str(earliest_bandwidth)
if generator_started is not None:
kwargs['generator_started'] = generator_started
h = cls(timestamp, **kwargs)
return h
@classmethod
def from_lines_v110(cls, lines):
"""
......@@ -184,10 +157,6 @@ class V3BWHeader(object):
assert isinstance(text, str)
return self.from_lines_v110(text.split(LINE_SEP))
@property
def num_lines(self):
return len(self.__str__().split(LINE_SEP))
@staticmethod
def generator_started_from_file(conf):
'''
......@@ -208,19 +177,50 @@ class V3BWHeader(object):
def earliest_bandwidth_from_results(results):
return round(min([r.time for fp in results for r in results[fp]]))
@classmethod
def from_results(cls, conf, results):
kwargs = dict()
latest_bandwidth = cls.latest_bandwidth_from_results(results)
earliest_bandwidth = cls.earliest_bandwidth_from_results(results)
generator_started = cls.generator_started_from_file(conf)
timestamp = str(latest_bandwidth)
kwargs['latest_bandwidth'] = unixts_to_isodt_str(latest_bandwidth)
kwargs['earliest_bandwidth'] = unixts_to_isodt_str(earliest_bandwidth)
if generator_started is not None:
kwargs['generator_started'] = generator_started
h = cls(timestamp, **kwargs)
return h
@property
def keyvalue_unordered_tuple_ls(self):
"""Return list of KeyValue tuples that do not have specific order."""
# sort the list to generate determinist headers
keyvalue_tuple_ls = sorted([(k, v) for k, v in self.__dict__.items()
if k in UNORDERED_KEYVALUES])
return keyvalue_tuple_ls
@property
def keyvalue_tuple_ls(self):
"""Return list of all KeyValue tuples"""
return [('version', self.version)] + self.keyvalue_unordered_tuple_ls
@property
def keyvalue_v110str_ls(self):
"""Return KeyValue list of strings following spec v1.1.0."""
keyvalues = [self.timestamp] + [KEYVALUE_SEP_V110.join([k, v])
for k, v in self.keyvalue_tuple_ls]
return keyvalues
@property
def strv110(self):
"""Return header string following spec v1.1.0."""
header_str = LINE_SEP.join(self.keyvalue_v110str_ls) + LINE_SEP + \
LINE_TERMINATOR
return header_str
@property
def keyvalue_v200_ls(self):
"""Return KeyValue list of strings following spec v2.0.0."""
keyvalue = [self.timestamp] + [KEYVALUE_SEP_V200.join([k, v])
for k, v in self.keyvalue_tuple_ls]
return keyvalue
@property
def strv200(self):
"""Return header string following spec v2.0.0."""
header_str = LINE_SEP.join(self.keyvalue_v200_ls) + LINE_SEP + \
LINE_TERMINATOR
return header_str
@property
def num_lines(self):
return len(self.__str__().split(LINE_SEP))
class V3BWLine(object):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment