Merge "Add ML based optimization to PCI opt"
[optf/osdf.git] / osdf / utils / data_conversion.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2015-2017 AT&T Intellectual Property
3 #
4 #   Licensed under the Apache License, Version 2.0 (the "License");
5 #   you may not use this file except in compliance with the License.
6 #   You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #   Unless required by applicable law or agreed to in writing, software
11 #   distributed under the License is distributed on an "AS IS" BASIS,
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #   See the License for the specific language governing permissions and
14 #   limitations under the License.
15 #
16 # -------------------------------------------------------------------------
17 #
18
19 from collections import defaultdict
20 import itertools
21
22 from dateutil.parser import parse
23 from dateutil import tz
24
25
26 def tuples_to_multi_val_dict(kvw_tuples, colnums=(0, 1)):
27     """Given a list of k,v tuples, get a dictionary of the form k -> [v1,v2,...,vn]
28
29     :param kvw_tuples: list of k,v,w tuples (e.g. [(k1,v1,a1), (k2,v2,a2), (k1,v3,a3), (k1,v4,a4)]
30     :param colnums: column numbers
31     :return: a dict of str:set, something like {k1: {v1, v3, v4}, k2: {v2}} or {k1: {a1, a3, a4}, k2: {a2}}
32     """
33     res = defaultdict(set)
34     for x in kvw_tuples:
35         key, val = x[colnums[0]], x[colnums[1]]
36         res[key].add(val)
37     return dict((k, set(v)) for k, v in res.items())
38
39
40 def tuples_to_dict(kvw_tuples, colnums=(0, 1)):
41     """Given a list of k,v tuples, get a dictionary of the form k -> v
42
43     :param kvw_tuples: list of k,v,w tuples (e.g. [(k1,v1,a1), (k2,v2,a2), (k3,v3,a3), (k1,v4,a4)]
44     :param colnums: column numbers
45     :return: a dict; something like {k1: v4, k2: v2, k3: v3} (note, k1 is repeated, so last val is retained)
46     """
47     return dict((x[colnums[0]], x[colnums[1]]) for x in kvw_tuples)
48
49
50 def utc_time_from_ts(timestamp):
51     """Return corresponding UTC timestamp for a given ISO timestamp (or anything that parse accepts)
52
53     """
54     return parse(timestamp).astimezone(tz.tzutc()).strftime('%Y-%m-%d %H:%M:%S')
55
56
57 def list_flatten(_l):
58     """Flatten a complex nested list of nested lists into a flat list
59
60     """
61     return itertools.chain(*[list_flatten(j) if isinstance(j, list) else [j] for j in _l])
62
63
64 text_to_symbol = {
65     'greater': ">",
66     'less': "<",
67     'equal': "="
68 }
69
70
71 def decode_data(data):
72     """Decode bytes to string
73
74     """
75     return data.decode(encoding='utf-8') if isinstance(data, bytes) else data