-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.py
More file actions
123 lines (93 loc) · 3.34 KB
/
utils.py
File metadata and controls
123 lines (93 loc) · 3.34 KB
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
from datetime import datetime
from functools import wraps
import requests
import yfinance as yf
from flask import redirect, render_template, session
def apology(message, code=400):
"""Render message as an apology to user."""
def escape(s):
"""
Escape special characters.
https://github.com/jacebrowning/memegen#special-characters
"""
for old, new in [
("-", "--"),
(" ", "-"),
("_", "__"),
("?", "~q"),
("%", "~p"),
("#", "~h"),
("/", "~s"),
('"', "''"),
]:
s = s.replace(old, new)
return s
return (
render_template("apology.html", top=f"{code} error", bottom=escape(message)),
code,
)
def login_required(f):
"""
Decorate routes to require login.
https://flask.palletsprojects.com/en/1.1.x/patterns/viewdecorators/
"""
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("user_id") is None:
return redirect("/login")
return f(*args, **kwargs)
return decorated_function
def lookup(symbols):
"""Look up quote for symbol."""
# Contact API
try:
if isinstance(symbols, list):
# convert list of symbols into string of symbols with commas
symbols = ",".join(symbols)
tickers = yf.Tickers(symbols).tickers
except requests.RequestException:
return None
# Parse response
try:
required_fields = ["symbol", "longName", "currentPrice"]
# Use dict comprehension to create dict to be returned
return {
# Use dict comprehension to remove unwanted key value pairs from yfinance.Ticker.info dict
ticker: {key: tickers[ticker].info[key] for key in required_fields}
for ticker in tickers
}
except (KeyError, TypeError, ValueError):
return None
def all_symbols():
"""Get a list of all valid symbols."""
# Get JSON data from url
try:
url = "https://iextrading.com/api/mobile/refdata"
response = requests.get(url)
response.raise_for_status()
except requests.RequestException:
return None
# Parse response
try:
symbols_data = response.json()
# Use dict comprehension inisde a list comprehension to remove unwanted key value pairs from list of dictionaries
symbols_data_stripped = [
{key: value for key, value in data.items() if key in ["Symbol", "Issuer"]}
for data in symbols_data
]
# Use dict compresion inside a list comprehension to remove unwanted key value pairs from list of dictionaries
symbols_only_data = [
{key: value for key, value in data.items() if key == "Symbol"}
for data in symbols_data
]
# List comprehension to convert list of dicts into list of "Symbols"
symbols_list = [symbol["Symbol"] for symbol in symbols_data]
return symbols_data_stripped, symbols_only_data, symbols_list
except (KeyError, TypeError, ValueError):
return None
def usd(value):
"""Format value as USD."""
return f"${value / 100:,.2f}"
def datetimeformat(dt_string, format="%d-%m-%Y %H:%M:%S"):
dt_object = datetime.strptime(str(dt_string), "%Y-%m-%d %H:%M:%S")
return dt_object.strftime(format)