-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.py
More file actions
130 lines (98 loc) · 3.3 KB
/
utils.py
File metadata and controls
130 lines (98 loc) · 3.3 KB
1
2
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""Utility functions for Astrodbkit"""
import functools
import re
import warnings
from datetime import datetime
from decimal import Decimal
from astroquery.simbad import Simbad
__all__ = ["json_serializer", "get_simbad_names"]
def deprecated_alias(**aliases):
"""
Decorator from StackOverflow
https://stackoverflow.com/questions/49802412/how-to-implement-deprecation-in-python-with-argument-alias
in order to handle deprecation of renamed columns
To use: add @deprecated_alias(old_name='new_name')
"""
def deco(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
rename_kwargs(f.__name__, kwargs, aliases)
return f(*args, **kwargs)
return wrapper
return deco
def rename_kwargs(func_name, kwargs, aliases):
"""Helper function used be deprecated_alias"""
for alias, new in aliases.items():
if alias in kwargs:
if new in kwargs:
raise TypeError(f"{func_name} received both {alias} and {new}")
warnings.warn(f"{alias} is deprecated; use {new}", DeprecationWarning)
kwargs[new] = kwargs.pop(alias)
def json_serializer(obj):
"""Function describing how things should be serialized in JSON.
Datetime objects are saved with datetime.isoformat(), Parameter class objects use clean_dict()
while all others use __dict__"""
if isinstance(obj, datetime):
return obj.isoformat()
if isinstance(obj, Decimal):
return float(obj)
if isinstance(obj, bytes):
return obj.decode("utf-8")
return obj.__dict__
def datetime_json_parser(json_dict):
"""Function to convert JSON dictionary objects to datetime when possible.
This is required to get datetime objects into the database.
Adapted from: https://stackoverflow.com/questions/8793448/how-to-convert-to-a-python-datetime-object-with-json-loads
"""
for key, value in json_dict.items():
if isinstance(value, str):
try:
json_dict[key] = datetime.fromisoformat(value)
except (ValueError, AttributeError):
pass
else:
pass
return json_dict
def _name_formatter(name):
"""
Clean up names of spurious formatting (extra spaces, some special characters)
Parameters
----------
name : str
Name to clean up
Returns
-------
Cleaned up name
"""
# Clean up multiple spaces
name = re.sub(r"\s\s+", " ", name)
# Clean up Simbad types
strings_to_delete = ["V* ", "EM* ", "NAME ", "** ", "Cl* ", "* "]
for pattern in strings_to_delete:
name = name.replace(pattern, "")
name = name.strip()
# Clean up 'hidden' names from Simbad
if "HIDDEN" in name.upper():
name = None
return name
def get_simbad_names(name, verbose=False):
"""
Get list of alternate names from Simbad
Parameters
----------
name : str
Name to resolve
verbose : bool
Verbosity flag
Returns
-------
List of names
"""
t = Simbad.query_objectids(name)
if t is not None and len(t) > 0:
temp = [_name_formatter(s) for s in t["id"].tolist()]
return [s for s in temp if s is not None and s != ""]
else:
if verbose:
print(f"No Simbad match for {name}")
return [name]