2019-01-16 08:54:06 -08:00
|
|
|
# Python file for general functions
|
2019-02-14 12:17:22 -08:00
|
|
|
|
2019-03-18 10:26:07 -07:00
|
|
|
import sys
|
|
|
|
sys.path.insert(0, './modules')
|
|
|
|
|
2019-01-31 13:22:02 -08:00
|
|
|
def getNearest(items, pivot):
|
|
|
|
return min(items, key=lambda x: abs(x - pivot))
|
|
|
|
|
2019-02-14 12:17:22 -08:00
|
|
|
|
2019-01-31 13:22:02 -08:00
|
|
|
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)
|
2019-02-14 12:17:22 -08:00
|
|
|
|
2019-01-31 13:22:02 -08:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-01-16 08:54:06 -08:00
|
|
|
|
2019-02-14 12:17:22 -08:00
|
|
|
def stringIsInt(s):
|
|
|
|
try:
|
|
|
|
int(s)
|
|
|
|
return True
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
|
2019-02-21 11:43:46 -08:00
|
|
|
|
2019-02-14 12:17:22 -08:00
|
|
|
def strintIsFloat(s):
|
|
|
|
try:
|
|
|
|
float(s)
|
|
|
|
return True
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
|
2019-02-21 11:43:46 -08:00
|
|
|
|
2019-02-14 12:17:22 -08:00
|
|
|
def fromCache(r):
|
|
|
|
import requests_cache
|
2019-03-18 10:26:07 -07:00
|
|
|
from termcolor import colored, cprint
|
2019-02-14 12:17:22 -08:00
|
|
|
if r.from_cache == True:
|
2019-03-18 10:26:07 -07:00
|
|
|
cprint('(Response taken from cache)', 'white', attrs=['dark'])
|
2019-02-21 11:43:46 -08:00
|
|
|
return
|
|
|
|
|
2019-02-14 12:17:22 -08:00
|
|
|
|
2019-03-01 11:06:09 -08:00
|
|
|
def getJoke():
|
|
|
|
import requests
|
2019-03-18 10:26:07 -07:00
|
|
|
import sys
|
|
|
|
from termcolor import colored, cprint
|
2019-03-01 11:06:09 -08:00
|
|
|
import requests_cache
|
2019-03-18 10:26:07 -07:00
|
|
|
from halo import Halo
|
2019-03-01 11:06:09 -08:00
|
|
|
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)'}
|
2019-03-18 10:26:07 -07:00
|
|
|
url = 'https://icanhazdadjoke.com'
|
|
|
|
|
|
|
|
cprint('Get: ' + url, 'white', attrs=['dark'])
|
|
|
|
with Halo(spinner='dots'):
|
|
|
|
f = requests.get('https://icanhazdadjoke.com/', headers=headers).json()
|
2019-03-01 11:06:09 -08:00
|
|
|
print('')
|
2019-03-18 10:26:07 -07:00
|
|
|
print(colored(f['joke'], 'green'))
|
2019-03-01 11:06:09 -08:00
|
|
|
|
|
|
|
|
|
|
|
def hasNumbers(inputString):
|
|
|
|
return any(char.isdigit() for char in inputString)
|
|
|
|
|
|
|
|
|
|
|
|
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.create_connection(("www.andrewkdinh.com", 80))
|
|
|
|
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)
|
|
|
|
|
2019-03-18 10:26:07 -07:00
|
|
|
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:
|
|
|
|
badList.append(x) # In case I want to know. If not, then I just make it equal to returnlist[0]
|
|
|
|
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
|
2019-03-01 11:06:09 -08:00
|
|
|
|
2019-01-16 08:54:06 -08:00
|
|
|
def main():
|
|
|
|
exit()
|
|
|
|
|
2019-01-31 13:22:02 -08:00
|
|
|
|
2019-01-16 08:54:06 -08:00
|
|
|
if __name__ == "__main__":
|
2019-01-31 13:22:02 -08:00
|
|
|
main()
|