-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFunctions.py
276 lines (219 loc) · 6.84 KB
/
Functions.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
'''
Copyright (C) 2019 Andrew Dinh
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
# Python file for general functions
import sys
sys.path.insert(0, './modules')
def getNearest(items, pivot):
return min(items, key=lambda x: abs(x - pivot))
def stringToDate(date):
from datetime import datetime
# datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
datetime_object = datetime.strptime(date, '%Y-%m-%d').date()
return(datetime_object)
def removeExtraDatesAndCloseValues(list1, list2):
# Returns the two lists but with the extra dates and corresponding close values removed
# list = [[dates], [close values]]
newList1 = [[], []]
newList2 = [[], []]
for i in range(0, len(list1[0]), 1):
for j in range(0, len(list2[0]), 1):
if list1[0][i] == list2[0][j]:
newList1[0].append(list1[0][i])
newList2[0].append(list1[0][i])
newList1[1].append(list1[1][i])
newList2[1].append(list2[1][j])
break
returnList = []
returnList.append(newList1)
returnList.append(newList2)
return returnList
def stringIsInt(s):
try:
int(s)
return True
except ValueError:
return False
def strintIsFloat(s):
try:
float(s)
return True
except ValueError:
return False
def fromCache(r):
import requests_cache
from termcolor import colored, cprint
if r.from_cache is True:
cprint('(Response taken from cache)', 'white', attrs=['dark'])
return
def getJoke():
import requests
from termcolor import colored, cprint
import requests_cache
from halo import Halo
import sys
with requests_cache.disabled():
'''
f = requests.get('https://official-joke-api.appspot.com/jokes/random').json()
print('')
print(f['setup'])
print(f['punchline'], end='\n\n')
'''
headers = {'Accept': 'application/json',
'User-Agent': 'fund-indicators (https://github.com/andrewkdinh/fund-indicators)'}
url = 'https://icanhazdadjoke.com'
cprint('GET:' + url, 'white', attrs=['dark'])
with Halo(spinner='dots'):
f = requests.get(url,
headers=headers).json()
print('')
print(colored(f['joke'], 'green'))
def hasNumbers(inputString):
return any(char.isdigit() for char in inputString)
def checkPackage(package):
import importlib.util
import sys
spec = importlib.util.find_spec(package)
if spec is None:
return False
else:
return True
def checkPackages(listOfPackages):
import importlib.util
import sys
packagesInstalled = True
packages = listOfPackages
for i in range(0, len(packages), 1):
package_name = packages[i]
spec = importlib.util.find_spec(package_name)
if spec is None:
print(package_name, "is not installed\nPlease enter 'pip install -r requirements.txt' to install all required packages")
packagesInstalled = False
return packagesInstalled
def checkPythonVersion():
import platform
# print('Checking Python version')
i = platform.python_version()
r = i.split('.')
k = float(''.join((r[0], '.', r[1])))
if k < 3.3:
print('Your Python version is', i,
'\nIt needs to be greater than version 3.3')
return False
else:
print('Your Python version of', i, 'is good')
return True
def isConnected():
import socket # To check internet connection
try:
# connect to the host -- tells us if the host is actually reachable
socket.setdefaulttimeout(5)
socket.create_connection(('1.1.1.1', 53))
print('Internet connection is good')
return True
except OSError:
# pass
print("No internet connection!")
return False
def fileExists(file):
import os.path
return os.path.exists(file)
def listIndexExists(i):
try:
i
return True
except IndexError:
return False
def removeOutliers(i):
import statistics
m = statistics.median(i)
firstQ = []
thirdQ = []
for x in i:
if x < m:
firstQ.append(x)
elif x > m:
thirdQ.append(x)
firstQm = statistics.median(firstQ)
thirdQm = statistics.median(thirdQ)
iqr = (thirdQm - firstQm) * 1.5
goodList = []
badList = []
for x in i:
if x < (thirdQm + iqr) and x > (firstQm - iqr):
goodList.append(x)
else:
# In case I want to know. If not, then I just make it equal to returnlist[0]
badList.append(x)
returnList = [goodList, badList, firstQm, m, thirdQm, iqr]
return returnList
def validateJson(text):
import json
try:
json.loads(text)
return True
except ValueError:
return False
def keyInDict(dict, key):
if key in dict:
return True
else:
return False
def getWeather():
import requests
from termcolor import colored, cprint
import requests_cache
from halo import Halo
import sys
sys.path.insert(0, './modules')
with requests_cache.disabled():
url = 'https://wttr.in?format=3'
cprint('GET:' + url, 'white', attrs=['dark'])
with Halo(spinner='dots'):
f = requests.get(url)
print('')
print(colored('Current weather in ' + f.text, 'green'), end='')
def detectDisplay():
import os
try:
t = os.environ["DISPLAY"]
except KeyError:
return False
if t == ':0.0':
return True
else:
return False
def trueOrFalse():
found = False
print('[1] Yes\n[2] No')
while found is False:
answer = str(input('Answer: '))
if stringIsInt(answer) is True:
temp = int(answer)
if temp == 1:
return True
elif temp == 2:
return False
else:
print('Please choose either 1 or 2')
pass
elif answer.lower() == 'yes':
return True
elif answer.lower() == 'no':
return False
else:
print('Please choose a number or type an answer')
def main():
exit()
if __name__ == "__main__":
main()