OOF design framework seed code
[optf/osdf.git] / 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 import itertools
20 from collections import defaultdict
21
22 from dateutil import tz
23 from dateutil.parser import parse
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     :param kvw_tuples: list of k,v,w tuples (e.g. [(k1,v1,a1), (k2,v2,a2), (k1,v3,a3), (k1,v4,a4)]
29     :param colnums: column numbers
30     :return: a dict of str:set, something like {k1: {v1, v3, v4}, k2: {v2}} or {k1: {a1, a3, a4}, k2: {a2}}
31     """
32     res = defaultdict(set)
33     for x in kvw_tuples:
34         key, val = x[colnums[0]], x[colnums[1]]
35         res[key].add(val)
36     return dict((k, set(v)) for k, v in res.items())
37
38
39 def tuples_to_dict(kvw_tuples, colnums=(0, 1)):
40     """Given a list of k,v tuples, get a dictionary of the form k -> v
41     :param kvw_tuples: list of k,v,w tuples (e.g. [(k1,v1,a1), (k2,v2,a2), (k3,v3,a3), (k1,v4,a4)]
42     :param colnums: column numbers
43     :return: a dict; something like {k1: v4, k2: v2, k3: v3} (note, k1 is repeated, so last val is retained)
44     """
45     return dict((x[colnums[0]], x[colnums[1]]) for x in kvw_tuples)
46
47
48 def utc_time_from_ts(timestamp):
49     """Return corresponding UTC timestamp for a given ISO timestamp (or anything that parse accepts)"""
50     return parse(timestamp).astimezone(tz.tzutc()).strftime('%Y-%m-%d %H:%M:%S')
51
52
53 def list_flatten(l):
54     """Flatten a complex nested list of nested lists into a flat list"""
55     return itertools.chain(*[list_flatten(j) if isinstance(j, list) else [j] for j in l])
56
57
58 text_to_symbol = {
59     'greater': ">",
60     'less': "<",
61     'equal': "="
62 }