def test_popitem(self, db: gspreaddb.GspreadDB): with pytest.raises(KeyError): db.popitem() db["key1"] = "value1" db["key2"] = "value2" assert db.popitem() == ("key2", "value2")
def test_clear(self, db: gspreaddb.GspreadDB): db["key1"] = "value1" db["key2"] = "value2" assert len(db) == 2 db.clear() assert len(db) == 0
def updatedaily(args): # get date and time now = datetime.now() nowdate = now.strftime("%d/%m/%Y") nowtime = now.strftime("%d/%m/%Y %H:%M:%S") # connect to google sheets dailydb = GspreadDB(DailyDbName, JsonFile) stockdb = GspreadDB(SourceDbName, JsonFile) # update data to google sheets for stock in args: dlog("Updating %s" % stock) try: tickersheet = stockdb.getstocksheet(stock) dailysheet = dailydb.getstocksheet(stock) except ValueError as ve: dailydb.log(nowtime, ve) continue trecs = tickersheet.get_all_records() dopen, high, low, close = getdaily(nowdate, trecs) dlog(nowdate, dopen, high, low, close) lastdaily = dailysheet.row_values(2) lastdate = lastdaily[0] cells = [nowdate, dopen, high, low, close] if lastdate == nowdate: dailysheet.update('A2', [cells], value_input_option='USER_ENTERED') else: dailysheet.insert_row(cells, 2, value_input_option='USER_ENTERED') dailydb.log(nowtime, "stock update completed for '%s'." % stock)
def initstock(args): nowtime = datetime.now().strftime("%d/%m/%Y %H:%M:%S") # connect to google sheets dailydb = GspreadDB(DailyDbName, 'DAILY', JsonFile) stockdb = GspreadDB(SourceDbName, 'IOPV', JsonFile) for stock in args: dlog("Initializing %s" % stock) try: tickersheet = stockdb.getstocksheet(stock) except ValueError as ve: dailydb.log(nowtime, ve) sys.exit(1) dailysheet = dailydb.initsheet(stock) trecs = tickersheet.get_all_records() dates = sorted(getdates(trecs), key=lambda x: datetime.strptime(x, "%d/%m/%Y").strftime( "%Y-%m-%d"), reverse=True) cells = [] for nowdate in dates: dopen, high, low, close = getdaily(nowdate, trecs) dlog(nowdate, dopen, high, low, close) cells.append([nowdate, dopen, high, low, close]) dailysheet.update('A2', cells, value_input_option='USER_ENTERED') dailydb.log(nowtime, "new stock creation completed for '%s'." % stock)
def runmain(args): # get time stamp for record nowtime = datetime.now().strftime("%d/%m/%Y %H:%M:%S") StockList.extend(args) if StockListFile: slist = getstocklist(StockListFile) StockList.extend(slist) if HtmlFile: iopvinfo = getiopvfromfile(HtmlFile) for stock in iopvinfo: print('\t'.join([nowtime] + stock)) elif SaveToDBase: from gspreaddb import GspreadDB db = GspreadDB(WorkbookName, 'IOPV', JsonFile) try: iopvinfo = getstocklive() except TimeoutError: db.log(nowtime, "Timeout on downloading data") return for stock in iopvinfo: try: name, iopv = stock db.addchange(name, nowtime, iopv) except ValueError as ve: db.log(nowtime, f'ERROR updating {name}: {ve}') db.log(nowtime, "stock update completed.") else: iopvinfo = getstocklive() for stock in iopvinfo: print('\t'.join([nowtime] + stock))
def test_bool(self, db: gspreaddb.GspreadDB): assert not bool(db) db["key1"] = "value1" db["key2"] = "value2" assert bool(db) db.clear() assert not bool(db)
def make_stock_charts(stocklist): assert slist dailydb = GspreadDB(DailyDbName, 'DAILY', JsonFile) figs = [] data_list = [] for stock in stocklist: df = load_gspread_stock(dailydb, stock).sort_values('DATE') fig, data = make_chart(df, stock) figs.append(fig) data_list.append(data) backup = f'{OutputFile}.org' try: os.remove(backup) except FileNotFoundError: pass try: os.rename(OutputFile, backup) except FileNotFoundError: pass with open(OutputFile, 'a') as f: f.write(html_title()) table = plot_table(data_list) f.write(table) for fig in figs: f.write(fig.to_html(full_html=False, include_plotlyjs='cdn'))
def main(stocks): assert stocks start_date = datetime.date.today() - datetime.timedelta(LastDaysRaw) # connect to google sheets dailydb = GspreadDB(DailyDbName, 'DAILY', JsonAuthFile) stockdb = GspreadDB(SourceDbName, 'IOPV', JsonAuthFile) iopv_by_date = {} daily_by_date = {} for stock in stocks: print("Processing %s" % stock) try: ticker = stockdb.getstockticker(stock) tickersheet = stockdb.getstocksheet(stock) rows = tickersheet.get_all_records() for row in rows: time = to_datetime(row['TIME'], format='%d/%m/%Y %H:%M:%S') if time.date() < start_date: continue date = str(time) if date not in iopv_by_date: iopv_by_date[date] = {'DATE': date, 'IOPV': {}} iopv_by_date[date]['IOPV'][ticker] = row['IOPV'] dailysheet = dailydb.getstocksheet(stock) rows = dailysheet.get_all_records() for row in rows: date = to_datetime(row['DATE'], format='%d/%m/%Y') date = str(date.date()) if date not in daily_by_date: daily_by_date[date] = {'DATE': date, 'IOPV': {}} ohlc = dict([(x, row[x]) for x in ('OPEN', 'HIGH', 'LOW', 'CLOSE')]) daily_by_date[date]['IOPV'][ticker] = ohlc except ValueError as ve: print(f"error getting sheet data for '{stock}': {ve}") continue print(f'Writing JSON files...') with open('iopv-raw.json', 'w') as fp: json.dump(iopv_by_date, fp, sort_keys=True, indent=4) with open('iopv-daily.json', 'w') as fp: json.dump(daily_by_date, fp, sort_keys=True, indent=4) if update_fire: fire = Firework() # update raw database print(f'Updating Firebase raw database...') for date, rec in iopv_by_date.items(): fire.update_stock_raw(date, rec) # update daily database print(f'Updating Firebase daily database...') for date, rec in daily_by_date.items(): fire.update_stock_daily(date, rec)
def test_get_set(self, db: gspreaddb.GspreadDB): with pytest.raises(KeyError): db["key"] assert db.get("key", "default") == "default" db["key"] = "value" assert db["key"] == "value" db["key"] = "new_value" assert db["key"] == "new_value"
def test_update(self, db: gspreaddb.GspreadDB): db["key1"] = "value1" dict1 = {"key1": "new_value1", "key2": "value2"} db.update(dict1) assert db["key1"] == "new_value1" assert db["key2"] == "value2" iter1 = [("key2", "new_value2"), ("key3", "value3")] db.update(iter1) assert db["key2"] == "new_value2" assert db["key3"] == "value3" db.update(key3="new_value3", key4="value4") assert db["key3"] == "new_value3" assert db["key4"] == "value4"
def test_items(self, db: gspreaddb.GspreadDB): assert list(db.items()) == [] db["key1"] = "value1" db["key2"] = "value2" assert list(db.items()) == [("key1", "value1"), ("key2", "value2")]
def test_values(self, db: gspreaddb.GspreadDB): assert list(db.values()) == [] db["key1"] = "value1" db["key2"] = "value2" assert list(db.values()) == ["value1", "value2"]
def test_setdefault(self, db: gspreaddb.GspreadDB): assert db.setdefault("key", "default") == "default" assert db.setdefault("key", "new_value") == "default"
def test_copy(self, db: gspreaddb.GspreadDB): db_copy = db.copy() assert db == db_copy assert not db is db_copy
def clear(db: gspreaddb.GspreadDB): db.clear() time.sleep(5) # prevent rate-limit