generated from antonyrc6/DumbTrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacts.py
130 lines (88 loc) · 3.41 KB
/
facts.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
from random import randrange
from datetime import date
import os, base64
import json
from __init__ import app, db
from sqlalchemy.exc import IntegrityError
class Facts(db.Model):
__tablename__ = 'facts' # table industry is plural, class industry is singular
# Define the User schema with "vars" from object
id = db.Column(db.Integer, primary_key=True)
stock = db.Column(db.String(255), unique=True, nullable=False)
fact = db.Column(db.String(255), unique=False, nullable=False)
# Defines a relationship between User record and Notes table, one-to-many (one user to many notes)
# constructor of a User object, initializes the instance variables within object (self)
def __init__(self, stock, industry):
self.fact = industry # variables with self prefix become part of the object,
self.stock = stock
@property
def industry(self):
return self.fact
# a setter function, allows industry to be updated after initial object creation
@industry.setter
def industry(self, industry):
self.fact = industry
@property
def stock(self):
return self.stock
# a setter function, allows industry to be updated after initial object creation
@stock.setter
def stock(self, stock):
self.stock = stock
def __str__(self):
return json.dumps(self.read())
def create(self):
try:
# creates a person object from User(db.Model) class, passes initializers
db.session.add(self) # add prepares to persist person object to Users table
db.session.commit() # SqlAlchemy "unit of work pattern" requires a manual commit
return self
except IntegrityError:
db.session.remove()
return None
# CRUD read converts self to dictionary
# returns dictionary
def read(self):
return {
"id": self.id,
"industry": self.industry,
"stock": self.stock,
}
# CRUD update: updates user industry, knew, phone
# returns self
def update(self, industry="", stock=""):
"""only updates values with length"""
if len(industry) > 0:
self.industry = industry
if len(stock) > 0:
self.stock = stock
db.session.commit()
return self
# CRUD delete: remove self
# None
def delete(self):
db.session.delete(self)
db.session.commit()
return None
"""CRUD DONE"""
def initFacts():
"""Builds sample user/note(s) data"""
with app.app_context():
"""Create database and tables"""
db.create_all()
"""Tester data for table"""
u1 = Facts( industry='Creates your technology products, especially your cell phones', stock='Apple', )
u2 = Facts( industry='Creates computers, laptop software, and other products', stock='Microsoft', )
u3 = Facts( industry='Creates electric stock', stock='Tesla', )
u4 = Facts( industry='Creates American stock', stock='Ford', )
u5 = Facts( industry='Creates movies', stock='Disney', )
facts = [u1, u2, u3, u4, u5]
"""Builds sample user/note(s) data"""
for fact in facts:
try:
'''add a few 1 to 4 notes per user'''
fact.create()
except IntegrityError:
'''fails with bad or duplicate data'''
db.session.remove()
print(f"Records exist, duplicate email, or error:")