<commit_before>import forexDataScrape as fds 
import dbapi
import datetime
import itertools


def getData(ticker):

	endTime = fds.startOfDay(datetime.datetime.utcnow())
	#endTime = endTime.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
	startTime = fds.startOfDay(endTime - datetime.timedelta(days=1))
	interval = "1m"

	fds.execScrape(ticker, startTime, endTime, interval)
	data = fds.execScrape(ticker, startTime, endTime, "1d")

	return data


#data = {ticker : data, ticker2: data2}
def updateTicker(ticker, data, old):
 	cols = db.formats["forex"]["cols"]

 	print data
	update = {}
 	for i in range(0,len(cols)):
 		col = cols[i]
 		if col == "current_rate":
 			update[col] = data["quote"]["close"][0]
		elif col == "last_rate":
			if old[ticker][i] == None:
				update[col] = 0.
			else:
				update[col] = old[ticker][i]
		elif col == "change_day":
			if old[ticker][i] == None or old[ticker][i] == 0:
				update[col] = 0.
			else:
				update[col] = (data["quote"]["close"][0]/old[ticker][i] - 1)*100
		else:
			update[col] = old[ticker][i]
	print "hi"
	print update

	db.insert("forex", update)


def updateForex():
	currencies = ["USD", "EUR", "JPY", "GBP", "CHF", "CAD", "AUD", "NZD"]

 	currencyPairs = list(itertools.permutations(currencies, 2))
	currencyTickers = []

	for pair in currencyPairs:
 		currencyTickers.append(pair[0] + pair[1] + "=X")

	db = dbapi.API()
	db.connect()
	old = db.getAllData('forex')
	old = mapify(old)

	for ticker in currencyTickers:
		data = getData(ticker)
		updateTicker(ticker,data,old)


#data must be two dimensions
def mapify(data):
	m = {}
	for row in data:
		m[row[0]] = row
	print m
	return m

#def pullPrevData

if __name__ == '__main__':
 	#getData("USDEUR=X")
 	db = dbapi.API()
 	db.connect()
 	updateForex()<commit_msg>Fixed bug with last rate and change calculations<commit_after>import forexDataScrape as fds 
import dbapi
import datetime
import itertools


def getData(ticker):

	endTime = fds.startOfDay(datetime.datetime.utcnow())
	#endTime = endTime.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
	startTime = fds.startOfDay(endTime - datetime.timedelta(days=1))
	interval = "1m"

	fds.execScrape(ticker, startTime, endTime, interval)
	data = fds.execScrape(ticker, startTime, endTime, "1d")

	return data


#data = {ticker : data, ticker2: data2}
def updateTicker(ticker, data, old):
 	cols = db.formats["forex"]["cols"]

 	print old
	update = {}
 	for i in range(0,len(cols)):
 		col = cols[i]
 		if col == "current_rate":
 			update[col] = data["quote"]["close"][0]
		elif col == "last_rate":
			if old[ticker][i-1] == None:
				update[col] = 0.
			else:
				update[col] = old[ticker][i-1]
		elif col == "change_day":
			if update["last_rate"] == 0:
				update[col] = 0.
			else:
				update[col] = (data["quote"]["close"][0]/update["last_rate"] - 1)*100
		else:
			update[col] = old[ticker][i]
	
	print update

	db.insert("forex", update)


def updateForex():
	currencies = ["USD", "EUR", "JPY", "GBP", "CHF", "CAD", "AUD", "NZD"]

 	currencyPairs = list(itertools.permutations(currencies, 2))
	currencyTickers = []

	for pair in currencyPairs:
 		currencyTickers.append(pair[0] + pair[1] + "=X")

	db = dbapi.API()
	db.connect()
	old = db.getAllData('forex')
	old = mapify(old)

	for ticker in currencyTickers:
		data = getData(ticker)
		updateTicker(ticker,data,old)


#data must be two dimensions
def mapify(data):
	m = {}
	for row in data:
		m[row[0]] = row
	print m
	return m

#def pullPrevData

if __name__ == '__main__':
 	#getData("USDEUR=X")
 	db = dbapi.API()
 	db.connect()
 	updateForex()
{'fraction_non_alphanumeric': 0.1653674832962138, 'fraction_numerical': 0.013363028953229399, 'mean_word_length': 4.199710564399421, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4405887', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1415, 'n_words': 327}