-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTMB_Common.py
101 lines (85 loc) · 2.18 KB
/
TMB_Common.py
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
"""
Module containing miscellaneous functions used by a variety of other modules
"""
from typing import Union
Number = Union[int, float]
def indent(n: int) -> str:
return n * " "
def rangemap_name(name: str) -> str:
return name + "_range_map"
def pointmap_name(name: str) -> str:
return name + "_point_map"
def name_to_filename(x: str) -> str:
"""
Convert a full species name into a valid file name
"""
name_replace_list = [
[" ", "_"],
["(", ""],
[")", ""],
[",", ""],
[".", ""],
["\"", ""],
["æ", "_ae_"],
["ö", "_o_"],
["œ", "_oe_"],
["ç", "_c_"],
["[", "_"],
["]", "_"]
]
for r in name_replace_list:
x = x.replace(r[0], r[1])
return x
def place_to_filename(x: str) -> str:
"""
Convert a location name into a valid file name
"""
place_replace_list = [
[", ", "_-_"],
[" (", "_-_"],
[")", ""],
["/", "-"],
[" ", "_"],
["\"", ""],
["'", ""],
["ç", "c"],
["ñ", "n"],
["ã", "a"],
["á", "a"],
["é", "e"],
["í", "i"],
["ó", "o"],
["ơ", "o"],
["ú", "u"],
["ū", "u"]
]
for r in place_replace_list:
x = x.replace(r[0], r[1])
return x
def unicode_to_html_encoding(x: str) -> str:
"""
Change unicode characters to html encoding in order to display correctly in google maps
"""
unicode_replace_list = [
["ç", "ç"], # 231
["ñ", "ñ"], # 241
["ã", "ã"], # 227
["á", "á"], # 225
["é", "é"], # 233
["í", "í"], # 237
["ó", "ó"], # 243
["ơ", "ơ"], # 417
["ú", "ú"], # 250
["ū", "ū"] # 363
]
for r in unicode_replace_list:
x = x.replace(r[0], r[1])
return x
def str_to_number(number: str) -> Number:
"""
Convert a string to either an int or float, avoiding the use of eval() for security reasons
"""
try:
return int(number)
except ValueError:
return float(number)