-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
109 lines (90 loc) · 3.25 KB
/
models.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
from run import app
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
class Base(db.Model):
__abstract__ = True
id = db.Column(db.Integer, primary_key=True)
class NumberRecord(Base):
__tablename__ = 'number_record'
# last time this object is requested by frontend
last_requested = db.Column(db.DateTime)
# 1 <= number <= 100
# is used to calculate value
number = db.Column(db.Integer)
# is the difference between sum of squares between 1 and number
# and square of sum of numbers between 1 and number
value = db.Column(db.Integer)
# number of times this object is requested
occurences = db.Column(db.Integer)
def sum_of_squares(self):
'''
Find sum of all squares from 1 to self.number
i.e. 1^2 + 2^2 + ... + self.number^2
Uses the formula found here:
www.trans4mind.com/personal_development/mathematics/series/sumNaturalSquares.htm
The formula is (n^3)/3 + (n^2)/2 + n/6 == sum of first n natural numbers squared
Returns None if self.number < 1
'''
if self.number < 1:
# Invalid
return None
formula = lambda num: int(pow(num, 3)/3.0 + pow(num, 2)/2.0 + num/6.0)
return formula(self.number)
def square_of_sum(self):
'''
Find square of sum of 1 to self.number
Returns None if self.number < 1
'''
if self.number < 1:
return None
formula = lambda num: pow((num*(num+1))/2, 2)
return formula(self.number)
def set_value(self):
'''
Sets self.value equal to the difference between
the square of the sum of all numbers up to self.number and
the sum of all squares up to self.number
If self.number < 1, set self.value to None
'''
if self.number < 1:
self.value = None
else:
self.value = self.square_of_sum() - self.sum_of_squares()
def to_json(self):
return {
'number': self.number,
'value': self.value,
'occurences': self.occurences,
'last_requested': self.last_requested.isoformat()
}
# To set up the database
if __name__ == '__main__':
import os
from subprocess import call
from config import Config
# Now to do some db setup
dummy_env = os.environ.copy()
# normally this sort of step would be done on identical environments, but that's not
# guaranteed here
dummy_env['PGPASSWORD'] = Config.DB_PASSWORD
call('dropdb %(database_name)s -U %(username)s' % {
'username': Config.DB_USER,
'database_name': Config.DATABASE_URI
},
shell=True, env=dummy_env)
call('dropdb %(database_name)s -U %(username)s' % {
'username': Config.DB_USER,
'database_name': Config.TEST_DATABASE_URI
},
shell=True, env=dummy_env)
call('createdb %(database_name)s -U %(username)s' % {
'username': Config.DB_USER,
'database_name': Config.DATABASE_URI
},
shell=True, env=dummy_env)
call('createdb %(database_name)s -U %(username)s' % {
'username': Config.DB_USER,
'database_name': Config.TEST_DATABASE_URI
},
shell=True, env=dummy_env)
db.create_all()