utils.py 3.29 KB
Newer Older
Wei Shoulin's avatar
Wei Shoulin committed
1
from datetime import datetime
Wei Shoulin's avatar
Wei Shoulin committed
2
from astropy.table import Table
Wei Shoulin's avatar
Wei Shoulin committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import time

def format_datetime(dt):
    return dt.strftime('%Y-%m-%d %H:%M:%S')

def format_date(dt):
    return dt.strftime('%Y-%m-%d')

def format_time_ms(float_time):
    local_time = time.localtime(float_time)
    data_head = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
    data_secs = (float_time - int(float_time)) * 1000
    return "%s.%03d" % (data_head, data_secs)


def get_parameter(kwargs, key, default=None):
    """ Get a specified named value for this (calling) function

    The parameter is searched for in kwargs

    :param kwargs: Parameter dictionary
    :param key: Key e.g. 'max_workers'
    :param default: Default value
    :return: result
    """

    if kwargs is None:
        return default

    value = default
    if key in kwargs.keys():
        value = kwargs[key]
    return value
    
def to_int(s, default_value = 0):
    try:
        return int(s)
    except:
        return default_value


def singleton(cls):
    _instance = {}

    def inner():
        if cls not in _instance:
            _instance[cls] = cls()
        return _instance[cls]
    return inner

def revoke(cmd, desc=''):
    def decorator(func):
        class Wrapper:
            def __init__(self, cmd, desc, func) -> None:
                self.cmd = cmd
                self.func = func
            def __str__(self) -> str:
                return "cmd"
            def __call__(self, *args, **kw):
                return self.func(*args, **kw)
        return Wrapper(cmd, desc, func)
Wei Shoulin's avatar
Wei Shoulin committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    return decorator

def fields_dtypes(rec):
    fields = tuple(rec.__dataclass_fields__.keys())
    dtypes = []
    for _, f in rec.__dataclass_fields__.items():
        if f.type == int:
            dtypes.append('i8')
        elif f.type == float:
            dtypes.append('f8')        
        elif f.type == str:
            dtypes.append('S2')
        elif f.type == list:
            dtypes.append('(12,)f8')       
        else:
            dtypes.append('S2')                
    dtypes = tuple(dtypes)
    return fields, dtypes

Wei Shoulin's avatar
Wei Shoulin committed
83
84
def tuple_fields_dtypes(rec: tuple):
    dtypes = []
Wei Shoulin's avatar
Wei Shoulin committed
85
86
    for f in rec:
        if type(f) == int:
Wei Shoulin's avatar
Wei Shoulin committed
87
            dtypes.append('i8')
Wei Shoulin's avatar
Wei Shoulin committed
88
        elif type(f) == float:
Wei Shoulin's avatar
Wei Shoulin committed
89
            dtypes.append('f8')        
Wei Shoulin's avatar
Wei Shoulin committed
90
        elif type(f) == str:
Wei Shoulin's avatar
Wei Shoulin committed
91
            dtypes.append('S2')
Wei Shoulin's avatar
Wei Shoulin committed
92
        elif type(f) == list:
Wei Shoulin's avatar
Wei Shoulin committed
93
94
95
96
97
98
            dtypes.append('(12,)f8')       
        else:
            dtypes.append('S2')                
    dtypes = tuple(dtypes)
    return dtypes

Wei Shoulin's avatar
Wei Shoulin committed
99
def to_table(query_result):
Wei Shoulin's avatar
Wei Shoulin committed
100
101
102
103
    if not query_result.success or not query_result.data:
        return Table()
    fields = query_result['columns']
    dtypes = tuple_fields_dtypes(query_result.data[0])
Wei Shoulin's avatar
Wei Shoulin committed
104
105
    t = Table(names = fields, dtype = dtypes, rows = query_result.data)
    t.meta['columns'] = fields
Wei Shoulin's avatar
Wei Shoulin committed
106
107
108
109
110
    t.meta['total'] = query_result['totalCount']

    return t

def object_list_to_table(query_result):
Wei Shoulin's avatar
Wei Shoulin committed
111
112
113
114
115
116
117
118
119
    if not query_result.success or not query_result.data:
        return Table()
    fields, dtypes = fields_dtypes(query_result.data[0])
    t = Table(names = fields, dtype = dtypes)
    t.meta['comments'] = [str(query_result.data[0].__class__)]
    t.meta['total'] = query_result['totalCount']

    for rec in query_result.data:
        t.add_row(tuple([rec.__getattribute__(k) for k in fields]))
Wei Shoulin's avatar
Wei Shoulin committed
120
    return t