Black formatting

This commit is contained in:
Kate 2021-03-14 15:53:31 +00:00
parent 0ba67dd6fa
commit 8e67ec85b4
3 changed files with 53 additions and 39 deletions

View file

@ -2,30 +2,37 @@ from tabulate import tabulate
from decimal import Decimal
import yfi as y
def generate_table(stonks):
table_data = []
total = 0
for ticker in stonks.keys():
price = y.get_current_price(ticker)
shares_count = stonks[ticker]
table_data.append([ticker, shares_count, round(Decimal(price) * Decimal(stonks[ticker]), 2)])
total += (Decimal(price) * Decimal(stonks[ticker]))
table_data.append(
[ticker, shares_count, round(Decimal(price) * Decimal(stonks[ticker]), 2)]
)
total += Decimal(price) * Decimal(stonks[ticker])
table_data.append(['Total', '', round(total, 2)])
table_data.append(["Total", "", round(total, 2)])
return str( tabulate(table_data, ['Ticker', 'Shares', 'Value ($)'], tablefmt="pretty") )
return str(
tabulate(table_data, ["Ticker", "Shares", "Value ($)"], tablefmt="pretty")
)
def watchlist_table(watchlist):
table_data = []
header = ['Ticker', 'Current Price ($)', 'Estimated Price ($)']
header = ["Ticker", "Current Price ($)", "Estimated Price ($)"]
for stonk in watchlist.keys():
table_data.append([
table_data.append(
[
stonk,
y.get_current_price(stonk),
watchlist[stonk],
])
]
)
return str(tabulate(table_data, header, tablefmt="pretty"))

View file

@ -3,14 +3,12 @@ import matplotlib, io
from datetime import datetime
def generate_table(headers, data, title="", bg='skyblue', border='steelblue'):
def generate_table(headers, data, title="", bg="skyblue", border="steelblue"):
timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
font = {'family' : 'monospace',
'weight' : 'normal',
'size' : 12}
font = {"family": "monospace", "weight": "normal", "size": 12}
matplotlib.rc('font', **font)
matplotlib.rc("font", **font)
title_text = title
footer_text = timestamp
@ -19,7 +17,6 @@ def generate_table(headers, data, title="", bg='skyblue', border='steelblue'):
column_headers = headers
if data:
cell_text = []
for row in data:
@ -27,7 +24,6 @@ def generate_table(headers, data, title="", bg='skyblue', border='steelblue'):
else:
cell_text = [["No Data"] + ["" for x in range(len(headers) - 1)]]
# Create the figure. Setting a small pad on tight_layout
# seems to better regulate white space. Sometimes experimenting
# with an explicit figsize here can produce better outcome.
@ -37,18 +33,19 @@ def generate_table(headers, data, title="", bg='skyblue', border='steelblue'):
image_height = 100 + (67 * (len(data) + 1))
image_width = 6 * dpi
plt.figure(linewidth=2,
plt.figure(
linewidth=2,
edgecolor=fig_border,
facecolor=fig_background_color,
tight_layout={'pad':1},
tight_layout={"pad": 1},
dpi=dpi,
figsize=(image_width/dpi, image_height/dpi)
figsize=(image_width / dpi, image_height / dpi),
)
# Add a table at the bottom of the axes
the_table = plt.table(cellText=cell_text,
colLabels=column_headers,
loc='upper center')
the_table = plt.table(
cellText=cell_text, colLabels=column_headers, loc="upper center"
)
# Scaling is the only influence we have over top and bottom cell padding.
# Make the rows taller (i.e., make cell y scale larger).
@ -66,7 +63,14 @@ def generate_table(headers, data, title="", bg='skyblue', border='steelblue'):
plt.suptitle(title_text, x=0.5, y=((image_height - 15) / image_height))
# Add footer
plt.figtext(((image_width - 15)/image_width), 1-((image_height - 15)/image_height), footer_text, horizontalalignment='right', size=12, weight='light')
plt.figtext(
((image_width - 15) / image_width),
1 - ((image_height - 15) / image_height),
footer_text,
horizontalalignment="right",
size=12,
weight="light",
)
# Force the figure to update, so backends center objects correctly within the figure.
# Without plt.draw() here, the title will center on the axes and not the figure.
@ -74,6 +78,6 @@ def generate_table(headers, data, title="", bg='skyblue', border='steelblue'):
# Create image. plt.savefig ignores figure edge and face colors, so map them.
image_buffer = io.BytesIO()
plt.savefig(image_buffer, format='png')
plt.savefig(image_buffer, format="png")
image_buffer.seek(0)
return image_buffer

11
yfi.py
View file

@ -8,14 +8,16 @@ def stock_exists(ticker):
except KeyError:
return False
def get_current_price(ticker):
yf_obj = yf.Ticker(ticker)
todays_data = yf_obj.history(period='1d')
return round(todays_data['Close'][0], 2)
todays_data = yf_obj.history(period="1d")
return round(todays_data["Close"][0], 2)
def get_delta(ticker, return_old_price=False):
yf_obj = yf.Ticker(ticker)
old_price = yf_obj.history(interval='1d')['Close'][-2]
old_price = yf_obj.history(interval="1d")["Close"][-2]
current_price = get_current_price(ticker)
delta = 100 * ((current_price / old_price) - 1)
if return_old_price:
@ -23,6 +25,7 @@ def get_delta(ticker, return_old_price=False):
else:
return delta
def get_delta_old_price(ticker):
yf_obj = yf.Ticker(ticker)
return yf_obj.history(interval='1d')['Close'][-2]
return yf_obj.history(interval="1d")["Close"][-2]