-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcdc_utils.py
More file actions
57 lines (56 loc) · 2.2 KB
/
cdc_utils.py
File metadata and controls
57 lines (56 loc) · 2.2 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
def filter_to_numbers(string:str):
ret = ""
for char in string:
if char in "1234567890.":
ret = ret + char
if ret != "":
return ret
else:
return "0"
def char_limit(string,chars):
show_last = 10
if len(string) > chars+show_last:
not_shown = len(string)-chars
not_shown_str = ""
if show_last > 0:
not_shown_str = f"...({not_shown-show_last} chars)..."
string = string[:chars]+not_shown_str+string[-show_last:]
return string
class NumberAbbreviations:
abbreviations = {}
def add_abbreviation(self,symbol,power_of_ten):
self.abbreviations[symbol] = pow(10,power_of_ten)
def abbrevate(self,n):
absolute = abs(n)
for abbreviation in self.abbreviations:
if absolute >= self.abbreviations[abbreviation]:
number = 0
try:
number = round(n/self.abbreviations[abbreviation],2)
except:
number = round(int(n)//int(self.abbreviations[abbreviation]),2)
return char_limit(f"{number}{abbreviation}",15)
return n
def unpack(self,n_str:str):
for abbreviation in self.abbreviations:
abr = self.abbreviations[abbreviation]
if n_str.lower().endswith(abbreviation.lower()):
new = filter_to_numbers(n_str)
return float(new)*abr
return float(filter_to_numbers(n_str))
number_abbreviation = NumberAbbreviations()
number_abbreviation.add_abbreviation("CENT",303)
number_abbreviation.add_abbreviation("GOOG",100)
number_abbreviation.add_abbreviation("VIG",63)
number_abbreviation.add_abbreviation("DEC",33)
number_abbreviation.add_abbreviation("NON",30)
number_abbreviation.add_abbreviation("OCT",27)
number_abbreviation.add_abbreviation("SEP",24)
number_abbreviation.add_abbreviation("SEX",21)
number_abbreviation.add_abbreviation("QUINT",18)
number_abbreviation.add_abbreviation("QUAD",15)
number_abbreviation.add_abbreviation("T",12)
number_abbreviation.add_abbreviation("B",9)
number_abbreviation.add_abbreviation("M",6)
number_abbreviation.add_abbreviation("K",3)
#number_abbreviation.add_abbreviation("GPLEX",pow(10,100))