From b2fa65ecfe14bb9377fbd8afa5f457a07472b6fb Mon Sep 17 00:00:00 2001 From: Jonathan Herman Date: Mon, 11 Feb 2013 18:28:31 -0500 Subject: First attempt at plot_exps.py. --- parse/col_map.py | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'parse/col_map.py') diff --git a/parse/col_map.py b/parse/col_map.py index 6f83ca1..8132639 100644 --- a/parse/col_map.py +++ b/parse/col_map.py @@ -6,9 +6,9 @@ class ColMapBuilder(object): def build(self): columns = sorted(self.value_map.keys(), - key=lambda c: (len(self.value_map[c]), c)) + key=lambda c: (-len(self.value_map[c]), c)) col_list = filter(lambda c : len(self.value_map[c]) > 1, columns) - return ColMap(col_list) + return ColMap(col_list, self.value_map) def try_add(self, column, value): self.value_map[column].add( value ) @@ -17,9 +17,17 @@ class ColMapBuilder(object): del(self.value_map[column]) class ColMap(object): - def __init__(self, col_list): + def __init__(self, col_list, values = None): self.col_list = col_list self.rev_map = {} + self.values = values + + self.minimums = [] + for c in col_list: + end = 1 + while c[:end] in self.minimums: + end += 1 + self.minimums += [c[:end]] for i, col in enumerate(col_list): self.rev_map[col] = i @@ -27,6 +35,9 @@ class ColMap(object): def columns(self): return self.col_list + def get_values(self): + return self.values + def get_key(self, kv): '''Convert a key-value dict into an ordered tuple of values.''' key = () @@ -46,8 +57,7 @@ class ColMap(object): kv[self.col_list[i]] = key[i] return kv - - def encode(self, kv): + def encode(self, kv, minimum=False): '''Converted a dict into a string with items sorted according to the ColMap key order.''' def escape(val): @@ -55,13 +65,23 @@ class ColMap(object): vals = [] - for key in self.col_list: + if minimum: + format = "%s:%s" + join = ", " + else: + format = "%s=%s" + join = "_" + + reverse = list(self.col_list) + reverse.reverse() + for key in reverse: if key not in kv: continue - k, v = escape(key), escape(kv[key]) - vals += ["%s=%s" % (k, v)] + display = key if not minimum else self.minimums[self.rev_map[key]] + k, v = escape(display), escape(kv[key]) + vals += [format % (k, v)] - return "_".join(vals) + return join.join(vals) @staticmethod def decode(string): -- cgit v1.2.2