Skip to content

Instantly share code, notes, and snippets.

@iffy
Created September 21, 2011 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iffy/1230776 to your computer and use it in GitHub Desktop.
Save iffy/1230776 to your computer and use it in GitHub Desktop.
skool example
$ python
Python 2.6.6 (r266:84374, Aug 31 2010, 11:00:51)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from storm.locals import Store, create_database
>>> from skmodel import Student, Class, Grade
>>>
>>> # use test db
... db = create_database('sqlite:test.db')
>>> store = Store(db)
>>>
>>>
>>> # get everything
... everything = store.find((Student, Class, Grade),
... Student.id == Grade.student_id,
... Grade.class_id == Class.id)
>>>
>>> print everything.count()
1411
>>>
>>> # only fall semester
... just_fall = everything.find(Class.semester == u'Fall')
>>> print just_fall.count()
343
>>>
>>>
>>> # only 2002
... fall_2002 = just_fall.find(Class.year == 2002)
>>> print fall_2002.count()
53
>>>
>>> for student, cls, grade in fall_2002:
... print student.id, student.first, student.last, cls.name, cls.semester, cls.year, grade.grade
...
TUEOKPBY OndaVee Lobingier FREN 2060 Fall 2002 C-
QVJB0WK8 Charmon Demichelis HIST 1080 Fall 2002 B-
VI6HQC8A Caleen Salman HIST 1080 Fall 2002 A-
AQREKZKV Denica Fraile HCOLL 1060 Fall 2002 A-
CZREAGQG Wilden Prizzi FREN 1060 Fall 2002 D-
RHNYBPWD Valta Opalka IAS 4040 Fall 2002 A-
J5I8DRGJ Alinda Eclarinal I SYS 4020 Fall 2002 C
CNYNPMNG Loraleigh Riveros IHUM 2020 Fall 2002 A-
9KNV3IJT Brookelle Lommel HIST 5060 Fall 2002 A
RHNZRLZ6 Natalynn Pharo IHUM 5060 Fall 2002 C
TJ1W252Y Kaysional Tempest Chamness FREN 5020 Fall 2002 D-
3PF37DPJ Thressa Lavergne ICLND 4040 Fall 2002 A-
IJSEYOF5 Oleta Wilfong HLTH 2040 Fall 2002 C
PL0PKFYB Chaundalee Rudzik GREEK 2080 Fall 2002 D-
TJ1W252Y Kaysional Tempest Chamness GREEK 2080 Fall 2002 D-
RJNPLRHC Rynell Borello HLTH 4040 Fall 2002 F
SJJSHGV7 Zeth Gist GREEK 4060 Fall 2002 D
SZOQGK3J Marlynn Kormann GERM 2040 Fall 2002 D-
JDGPHGUB Cleanne Cader GERM 2040 Fall 2002 B-
IJSEYOF5 Oleta Wilfong ICLND 3080 Fall 2002 D+
J5I8DRGJ Alinda Eclarinal HIST 1020 Fall 2002 B
YOLPYIUJ Desa Husby HCOLL 4040 Fall 2002 B
9KNV3IJT Brookelle Lommel ICLND 1080 Fall 2002 D+
EQNRXAWL Talesha Ciccarello GERM 3040 Fall 2002 C-
EQNRXAWL Talesha Ciccarello FREN 1040 Fall 2002 C+
J1IG2POW Jaelyn Still GEOG 3040 Fall 2002 D
QVJB0WK8 Charmon Demichelis GERM 3020 Fall 2002 D+
TJ1W252Y Kaysional Tempest Chamness HCOLL 5080 Fall 2002 A-
YSIWDEQP Nicle Cobbs HONRS 3020 Fall 2002 B-
RHNZRLZ6 Natalynn Pharo HLTH 5040 Fall 2002 A-
YYCY5QQ0 Qylci Goley HUNG 1060 Fall 2002 C-
XTAH6DST Ailie Ann Klingbeil IHUM 2040 Fall 2002 B
YOLPYIUJ Desa Husby GEOL 2080 Fall 2002 C
FUENTC6N Symantha Wildner HLTH 3080 Fall 2002 C-
6KRS7XIU Canter Ray Roxburgh GREEK 3080 Fall 2002 B
YSIWDEQP Nicle Cobbs I SYS 1080 Fall 2002 C+
ZPNSODEG Danalee Higashida I SYS 1020 Fall 2002 F
03RQD1L8 RaDene Billingsby GERM 2080 Fall 2002 A-
CZREAGQG Wilden Prizzi IAS 4080 Fall 2002 F
YSBNVGGT Marvel Generoso HEB 2040 Fall 2002 B-
YYCY5QQ0 Qylci Goley GEOL 2040 Fall 2002 D+
9KNV3IJT Brookelle Lommel HEB 4080 Fall 2002 C-
6KRS7XIU Canter Ray Roxburgh HONRS 1040 Fall 2002 B+
J1IG2POW Jaelyn Still FREN 5040 Fall 2002 C
KW2MWEAV Caudell Stennis HLTH 3060 Fall 2002 F
6KRS7XIU Canter Ray Roxburgh IHUM 1060 Fall 2002 B-
YSBNVGGT Marvel Generoso IHUM 4040 Fall 2002 A-
XTAH6DST Ailie Ann Klingbeil HLTH 1060 Fall 2002 A
3PF37DPJ Thressa Lavergne I SYS 4060 Fall 2002 A-
9KNV3IJT Brookelle Lommel HCOLL 4060 Fall 2002 B-
M1L4TIJJ Carlan Skinsacos IAS 3080 Fall 2002 B
FUENTC6N Symantha Wildner HEB 5080 Fall 2002 F
KW2MWEAV Caudell Stennis GEOL 5080 Fall 2002 B+
>>>
"""
skool model
"""
from storm.locals import *
class Student(Storm):
"""
I am a student
"""
__storm_table__ = 'students'
id = Unicode(primary=True)
first = Unicode()
last = Unicode()
classes = ReferenceSet(id, 'Grade.student_id', 'Grade.class_id', 'Class.id')
grades = ReferenceSet(id, 'Grade.id')
class Class(Storm):
"""
I am an attempt to educate.
"""
__storm_table__ = 'classes'
id = Int(primary=True)
name = Unicode()
semester = Unicode()
year = Int()
class Grade(Storm):
"""
I am a metric of learning. (yeah right)
"""
__storm_table__ = 'grades'
__storm_primary__ = 'student_id', 'class_id'
student_id = Unicode()
class_id = Int()
grade = Unicode()
klass = Reference(class_id, 'Class.id')
student = Reference(student_id, 'Student.id')
def makeDatabase(store):
"""
Creates the schema for skool using the provided store.
"""
store.execute('''create table students (
id text primary key,
first text,
last text
)''')
store.execute('''create table classes (
id integer primary key,
name text,
semester text,
year integer
)''')
store.execute('''create table grades (
student_id text,
class_id integer,
grade text,
primary key (student_id, class_id)
)''')
from storm.locals import Store, create_database
from skmodel import Student, Class, Grade
# use test db
db = create_database('sqlite:test.db')
store = Store(db)
# get everything
everything = store.find((Student, Class, Grade),
Student.id == Grade.student_id,
Grade.class_id == Class.id)
print everything.count()
# only fall semester
just_fall = everything.find(Class.semester == u'Fall')
print just_fall.count()
# only 2002
fall_2002 = just_fall.find(Class.year == 2002)
print fall_2002.count()
for student, cls, grade in fall_2002:
print student.id, student.first, student.last, cls.name, cls.semester, cls.year, grade.grade
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment