-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatastructures.py
183 lines (141 loc) · 6.57 KB
/
datastructures.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from sqlalchemy import Column, DateTime, String, Integer, ForeignKey, Float, asc, Index
from sqlalchemy.ext.declarative import declarative_base, DeclarativeMeta
from sqlalchemy.orm import relationship, sessionmaker
import datetime, json
# after pull:
# psql -U postgres -c "drop database pricetracker_database;"
# psql -U postgres -c "create database pricetracker_database with owner postgres encoding = 'UNICODE';"
# psql -U postgres -d pricetracker_database -f pricetracker_database.sql
#before commit:
# pg_dump -U postgres -O pricetracker_database > pricetracker_database.sql
# https://wiki-bsse.ethz.ch/display/ITDOC/Copy+PostgreSQL+database+between+computers
Base = declarative_base()
BaseSimple = declarative_base()
class Storage(Base):
__tablename__ = 'storage'
id = Column('id', Integer, primary_key=True)
company = Column('company', String, nullable=False)
name = Column('name', String)
keyword = Column('keyword', String)
date = Column('date', DateTime)
price = Column('price', Float)
def __init__(self, name=None, keyword=None, company=None, price=None, date=None):
self.name = name
self.keyword = keyword
self.company = company
self.price = price
self.date = date
class Product(Base):
__tablename__ = 'product'
id = Column('id', Integer, primary_key=True)
name = Column(String, nullable=False)
manufacturer = Column(String)
manufacturer_id = Column(String)
url_image = Column(String)
product_offered = relationship('ProductCompany', back_populates='product') # One to many with ProductCompany with backlinking
def __int__(self, name=None, manufacturer=None, manufacturer_id=None):
self.name = name
self.manufacturer = manufacturer
self.manufacturer_id = manufacturer_id
class Company(Base):
__tablename__ = 'company'
id = Column('id', Integer, primary_key=True)
name = Column(String, nullable=False)
url = Column(String)
scrape_url = Column(String, nullable=False)
stock = relationship('ProductCompany', back_populates='company') # One to many with ProductCompany with backlinking
def __init__(self, name=None, url=None, scrape_url=None):
self.name = name
self.url = url
self.scrape_url = scrape_url
class ProductCompany(Base):
__tablename__ = 'product_company'
id = Column('id', Integer, primary_key=True)
product_id = Column(Integer, ForeignKey('product.id'))
product = relationship(Product, back_populates='product_offered')
company_id = Column(Integer, ForeignKey('company.id'))
company = relationship(Company, back_populates='stock') # backlinking
tag = Column(String, nullable=False)
url = Column(String)
prices = relationship("Price") # One to many with prices
def __int__(self, tag=None, product=None, company=None):
self.tag = tag
self.product = product
self.company = company
class Price(Base):
__tablename__ = 'price'
id = Column('id', Integer, primary_key=True)
price = Column(Float, nullable=False)
date = Column(DateTime, nullable=False)
product_company_id = Column(Integer, ForeignKey('product_company.id'))
#price_changes = relationship("PriceChanges")
Index('price_date_index', date, product_company_id, unique=True)
def __init__(self, price=None, date=None):
self.price = price
self.date = date
class PriceChanges(Base):
__tablename__ = 'price_changes'
id = Column('id', Integer, primary_key=True)
date = Column(DateTime, nullable=False)
percent_change = Column(Float, nullable=False)
product_company_id = Column(Integer, ForeignKey('product_company.id'))
#product_company = relationship(Price, back_populates='product_company')
price_today_id = Column(Integer, ForeignKey('price.id'))
#price_today = relationship(Price, foreign_keys=[price_today_id]) # backlinking
price_yesterday_id = Column(Integer, ForeignKey('price.id'))
#price_yesterday = relationship(Price, foreign_keys=[price_yesterday_id]) # backlinking
def __init__(self, date = None, product_company_id = None, price_today_id = None, price_yesterday_id = None, percent_change = None):
#self.percent_change = price_today.price/price_yesterday.price
self.price_today_id = price_today_id
self.price_yesterday_id = price_yesterday_id
self.product_company_id = product_company_id
self.percent_change = percent_change
if date is None:
self.date = datetime.datetime.now()
else:
self.date = date
class PriceChangesSimple(BaseSimple):
__tablename__ = 'price_changes'
id = Column('id', Integer, primary_key=True)
date = Column(DateTime, nullable=False)
percent_change = Column(Float, nullable=False)
product_company_url = Column(String)
product_manufacturer = Column(String)
product_name = Column(String)
product_tag = Column(String)
product_url_image = Column(String)
price_today = Column(Float)
price_yesterday = Column(Float)
def __init__(self, date = None, product_company = None, product = None, price_today = None, price_yesterday = None):
#self.percent_change = price_today.price/price_yesterday.price
self.price_today = price_today.price
self.price_yesterday = price_yesterday.price
self.product_company_url = product_company.url
self.percent_change = 2 - self.price_today / self.price_yesterday
self.product_name = product.name
self.product_manufacturer = product.manufacturer
self.product_tag = product.manufacturer_id
self.product_url_image = product.url_image
if date is None:
self.date = datetime.datetime.now()
else:
self.date = date
class AlchemyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj.__class__, DeclarativeMeta):
# an SQLAlchemy class
fields = {}
for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata']:
data = obj.__getattribute__(field)
try:
if isinstance(data, datetime.date):
json.dumps(data.isoformat())
fields[field] = data.isoformat()
else:
json.dumps(data) # this will fail on non-encodable values, like other classes
fields[field] = data
except TypeError:
fields[field] = None
# a json-encodable dict
return fields
return json.JSONEncoder.default(self, obj)