Skip to content

Commit

Permalink
Overhaul the question generation process.
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-matera committed Dec 27, 2019
1 parent 0754dda commit cfcc920
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 245 deletions.
Empty file added flights.sqlite3
Empty file.
61 changes: 0 additions & 61 deletions gen_questions.py

This file was deleted.

12 changes: 12 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SOURCE = $(shell ls notebooks/*/*.py)
NOTEBOOKS = $(SOURCE:.py=.ipynb)
GENERATOR = $(abspath ./tools/gen_questions.py)
all: $(NOTEBOOKS)

%.ipynb %.py:
cd $(dir $*); python3 $(GENERATOR) $(notdir $*)

clean:
rm $(NOTEBOOKS)

.PYONY: clean
2 changes: 2 additions & 0 deletions notebooks/DatabaseConceptsBook/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
queen_anne_joins.ipynb
pets_joins.ipynb
41 changes: 2 additions & 39 deletions notebooks/DatabaseConceptsBook/pets_joins.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
"""
CIS-54 Course SQL Questions
# Pet Joins
"""

import sys
import inspect

from IPython.display import HTML, Markdown, display
import sqlite3
import pandas as pd

conn = sqlite3.connect('../Databases/pets.sqlite3')
db_url = ('sqlite:///pets.sqlite3')

class Question01:
"""
Expand All @@ -31,7 +24,6 @@ class Question01:
breed.minweight, breed.maxweight, breed.averagelifeexpectancy
from pet_3 join breed
on pet_3.petbreed = breed.breedname
limit 5;
"""

class Question02:
Expand All @@ -46,7 +38,6 @@ class Question02:
breed.minweight, breed.maxweight, breed.averagelifeexpectancy
from pet_3 left join breed
on pet_3.petbreed = breed.breedname
limit 5;
"""

class Question03:
Expand All @@ -62,7 +53,6 @@ class Question03:
(breed.minweight + breed.maxweight) / 2 as `Average Weight`
from pet_3 join breed
on pet_3.petbreed = breed.breedname
limit 5;
"""

class Question04:
Expand All @@ -79,31 +69,4 @@ class Question04:
from pet_3 join breed
on pet_3.petbreed = breed.breedname
where pet_3.petweight > `Average Weight`
limit 5;
"""


###########################################
q_num = 0

def get_question(module, name=None):
global q_num
if name is None:
questions = []
for name, member in inspect.getmembers(module):
if inspect.isclass(member) and name.startswith('Question'):
questions.append(member)
question = questions[q_num]
q_num += 1

else:
question = getattr(module, name)

hint = '*This preview is limited to five rows.*'
df = pd.read_sql_query(question.answer, conn)

return display(Markdown(question.__doc__), df, Markdown(hint))

if __name__ == '__main__':
get_question(sys.modules[__name__], 'Question1')

52 changes: 7 additions & 45 deletions notebooks/DatabaseConceptsBook/queen_anne_joins.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
"""
CIS-54 Course SQL Questions
# Queen Anne Joins
"""

import sys
import inspect

from IPython.display import HTML, Markdown, display
import sqlite3
import pandas as pd

conn = sqlite3.connect('../Databases/queen_anne.sqlite3')
db_url = 'sqlite:///queen_anne.sqlite3'

class Question01:
"""
Expand All @@ -22,31 +15,31 @@ class Question01:
- Vendor Company Name
"""
answer = "select item.itemdescription, item.itemcost, vendor.companyname from item, vendor where item.vendorid = vendor.vendorid limit 5"
answer = "select item.itemdescription, item.itemcost, vendor.companyname from item, vendor where item.vendorid = vendor.vendorid"

class Question02:
"""
## 2. Corporate Vendor Items
Update the last query to only include vendors that have a non-NULL company name.
"""
answer = "select item.itemdescription, item.itemcost, vendor.companyname from item, vendor where item.vendorid = vendor.vendorid and vendor.companyname is not NULL limit 5"
answer = "select item.itemdescription, item.itemcost, vendor.companyname from item, vendor where item.vendorid = vendor.vendorid and vendor.companyname is not NULL"

class Question03:
"""
## 3. Employee Sales
Write a query that lists all sales with the name of the salesperson that made them.
"""
answer = "select * from sale, employee where sale.employeeid = employee.employeeid limit 5"
answer = "select * from sale, employee where sale.employeeid = employee.employeeid"

class Question04:
"""
## 4. Sales Goals
Update the last query to show the total sales by each employee.
"""
answer = "select FirstName, LastName, sum(Total) as Total from sale, employee where sale.employeeid = employee.employeeid group by FirstName, LastName limit 5"
answer = "select FirstName, LastName, sum(Total) as Total from sale, employee where sale.employeeid = employee.employeeid group by FirstName, LastName"

class Question05:
"""
Expand All @@ -60,7 +53,6 @@ class Question05:
where customer.customerid = sale.customerid
group by FirstName, LastName
order by total desc
limit 5;
"""

class Question06:
Expand All @@ -69,7 +61,7 @@ class Question06:
Write a query that shows all **unsold** items.
"""
answer = """select * from item where itemid not in (select itemid from sale_item) limit 5;"""
answer = """select * from item where itemid not in (select itemid from sale_item)"""

class Question07:
"""
Expand All @@ -81,7 +73,6 @@ class Question07:
select firstname, lastname from customer
intersect
select contactfirstname, contactlastname from vendor
limit 5;
"""

class Question08:
Expand All @@ -96,7 +87,6 @@ class Question08:
select contactfirstname, contactlastname, email from vendor
union
select firstname, lastname, email from customer
limit 5;
"""

class Question09:
Expand All @@ -121,7 +111,6 @@ class Question09:
and sale.customerid = customer.customerid
and sale.saleid = sale_item.saleid
and sale_item.itemid = item.itemid
limit 5
;
"""

Expand All @@ -142,32 +131,5 @@ class Question10:
where employee.employeeid = sale.employeeid
and customer.customerid = sale.customerid
group by employee.employeeid, customer.customerid
limit 5
;
"""


###########################################
q_num = 0

def get_question(module, name=None):
global q_num
if name is None:
questions = []
for name, member in inspect.getmembers(module):
if inspect.isclass(member) and name.startswith('Question'):
questions.append(member)
question = questions[q_num]
q_num += 1

else:
question = getattr(module, name)

hint = '*This preview is limited to five rows.*'
df = pd.read_sql_query(question.answer, conn)

return display(Markdown(question.__doc__), df, Markdown(hint))

if __name__ == '__main__':
get_question(sys.modules[__name__], 'Question1')

2 changes: 2 additions & 0 deletions notebooks/Flights/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
simple_selects.ipynb
intermediate_selects.ipynb
Loading

0 comments on commit cfcc920

Please sign in to comment.