-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
162 lines (124 loc) · 4.43 KB
/
helpers.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from flask import redirect, render_template, session
from functools import wraps
import sqlite3
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=code, 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 closer(data):
"""Look up quote for symbol."""
data= {
'Closer.Alt': {
'companyName':'Closer.Alt',
'currency':'Beta',
'latestPrice':40.91,
'nft':'Closer.Alt'
# 'image':url
}
}
try:
for k, v in data.items():
if type(v) is dict:
return {
"name": v["companyName"],
"price": float(v["latestPrice"]),
"NFT": v["nft"]
# "image": nft["image"]
}
except (KeyError, TypeError, ValueError):
return None
def dem_say(data):
"""Look up quote for symbol."""
data= {
'Dem.Say.Alt': {
'companyName':'Dem.Say.Alt',
'currency':'Beta',
"latestPrice":24.54,
'nft':'Dem.Say.Alt'
}
}
try:
for k, v in data.items():
if type(v) is dict:
return {
"name": v["companyName"],
"price": float(v["latestPrice"]),
"NFT": v["nft"]
# "image": nft["image"]
}
except (KeyError, TypeError, ValueError):
return None
def kilimanjaro(data):
"""Look up quote for symbol."""
# url = f"file:///Users/z.emmanuel/Documents/CS50/finance/static/images/img1.jpg"
data= {
'Kilimanjaro.Alt': {
'companyName':'Kilimanjaro.Alt',
'currency':'Beta',
'latestPrice':29.76,
'nft':'Kilimanjaro.Alt'
}
}
# NFT = data.value()
try:
for k, v in data.items():
if type(v) is dict:
return {
"name": v["companyName"],
"price": float(v["latestPrice"]),
"NFT": v["nft"]
# "image": nft["image"]
}
except (KeyError, TypeError, ValueError):
return None
def naomi(data):
"""Look up quote for symbol."""
# url = f"file:///Users/z.emmanuel/Documents/CS50/finance/static/images/img1.jpg"
data= {
'Naomi.Alt': {
'companyName':'Naomi.Alt',
'currency':'Beta',
'latestPrice':30.76,
'nft':'Naomi.Alt'
}
}
# NFT = data.value()
try:
for k, v in data.items():
if type(v) is dict:
return {
"name": v["companyName"],
"price": float(v["latestPrice"]),
"NFT": v["nft"]
# "image": nft["image"]
}
except (KeyError, TypeError, ValueError):
return None
def get_db_connection():
conn = sqlite3.connect('zapa.db')
conn.row_factory = sqlite3.Row
# with open('schema.sql') as f:
# conn.executescript(f.read())
return conn
def usd(value):
"""Format value as Beta(made up) Currency."""
return f"ß {value:,.2f}"