-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.py
106 lines (84 loc) · 2.53 KB
/
utils.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
102
103
104
105
106
#!/usr/bin/env python3
import requests
def convert_num(mode, num):
"""Converts a number in any given number scale
Example:
`convert_num("100K", 600000) returns 6`
Args:
- mode: (string) the scale for the conversion ("100K", "M", "10M", "100M", "B")
- num: the number to be converted
Returns:
the converted number
"""
num = int(num)
if mode == "100K":
num = int(num / 100000)
elif mode == "M":
num = int(num / 1000000)
elif mode == "10M":
num = int(num / 10000000)
elif mode == "100M":
num = int(num / 100000000)
elif mode == "B":
num = int(num / 1000000000)
return num
def display_num(num, short=False, decimal=False):
"""Converts a number in a readable format
Args:
- num: the number to be converted
- short (optional): flag to get a long or short literal ("Mln" vs "million")
- decimal (optional): flag to print also the first decimal digit (19.1 vs 19)
Returns:
a string with a number in a readable format
"""
num = int(num)
digits = len(str(abs(num)))
if digits <= 6:
num = int(num / 1000)
if not short:
out = "{} thousand".format(num)
else:
out = "{}.000".format(num)
elif digits > 6 and digits <= 9:
if not decimal:
num = int(num / 1000000)
if not short:
out = "{} million".format(num)
else:
out = "{} Mln".format(num)
else:
num = num / 1000000
if not short:
out = "{:0.1f} million".format(num)
else:
out = "{:0.1f} Mln".format(num)
elif digits > 9:
num = num / 1000000000
if not short:
out = "{:0.1f} billion".format(num)
else:
out = "{:0.1f} Bln".format(num)
return out
def download_image(url):
"""Downloads an image, given an url
The image is saved in the download.jpg file
Args:
url: source from where download the image
"""
filename = "download.jpg"
response = requests.get(url)
file = open(filename, "wb")
file.write(response.content)
file.close()
return filename
def download(url, filename):
"""Downloads a file, given an url and filename
Args:
url: source from where download the image
filename: name of the file to save
"""
response = requests.get(url)
file = open(filename, "wb")
file.write(response.content)
file.close()
return filename