JeopardyDatabase: Difference between revisions

From OpenHatch wiki
Content added Content deleted
imported>Jesstess
No edit summary
imported>Jesstess
No edit summary
Line 63: Line 63:
== Project steps ==
== Project steps ==


=== 1. Learn about HSV values ===
=== 1. Look at the layout of the Jeopardy database ===


Start sqlite with <code>sqlite3 jeopardy.db</code>. Then look at the tables in your database by running the following commands and the sqlite prompt:
Run the ColorWall effects again with


* <tt>.table</tt>, which will list the tables in the database
<pre>python run.py -a</pre>
* <tt>.schema category</tt>, which will show the organization of the <tt>category</tt> table, including the fields and the data types they store.
* <tt>.schema clue</tt>


Read these short documents:
The names of the effects are printed to the terminal as they are run. Pay particular attention to the first 4 effects:
* What is SQL? http://www.w3schools.com/sql/sql_intro.asp
* SolidColorTest
* What is a database schema? http://wiki.answers.com/Q/What_is_a_database_schema
* HueTest
* SaturationTest
* ValueTest


<b>Check your understanding</b>:
In all of these effects, a tuple <code>hsv</code> containing the hue, saturation, and value describing a color are passed to <code>self.wall.set_pixel</code> to change the color of a single pixel on the wall.
* What tables are in the database?

* What is a schema?
What are the differences between these tests? Given these difference and how they are expressed visually, how does varying hue, saturation, or value change a color?
* What fields are in the <code>category</code> table?

* What fields are in the <code>clue</code> table?
<b>Check your understanding</b>: what saturation and value would you guess firetruck red have?

<b>Step 1 resources</b>:
<ul>
<li>
Python tuples: http://www.tutorialspoint.com/python/python_tuples.htm
</li>
<li>
Using the <code>range</code> function to produce a sequence of numbers: http://docs.python.org/tutorial/controlflow.html#the-range-function
</li>
<li>
Using the <code>time</code> module to sleep (do nothing for a bit) inside your program: http://docs.python.org/library/time.html
</li>
</ul>





Revision as of 22:09, 24 July 2012

Project

Learn how to get data from a database in Python while writing parts of a Jeopardy game, using real Jeopardy data!

Goals

  • practice using the SQL database query language
  • practice getting data from a database in Python

Project setup

Install the project dependencies

Download and un-archive the Jeopardy database project skeleton code

Un-archiving will produce a JeopardyDatabase folder containing 3 Python files and one SQL database dump.

Create a SQLite database from the database dump

Inside JeopardyDatabase is a file called jeopardy.dump which contains a SQL database dump. We need to turn that database dump into a SQLite database.

Once you have SQLite installed, you can create a database from jeopardy.dump with:

sqlite3 jeopardy.db < jeopardy.dump

This creates a sqlite3 database called jeopardy.db

Test your setup

At a command prompt, start sqlite3 using the jeopardy.db database by running:

sqlite3 jeopardy.db

That should start a sqlite prompt that looks like this:

SQLite version 3.6.12
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>

At that sqlite prompt, type .tables and hit enter. That should display a list of the tables in this database:

sqlite> .tables
category  clue    
sqlite>

From a command prompt, navigate to the JeopardyDatabase directory and run

python jeopardy_categories.py

You should see a list of 10 jeopardy categories printed to the screen. If you don't, let a staff member know so you can debug this together.

Project steps

1. Look at the layout of the Jeopardy database

Start sqlite with sqlite3 jeopardy.db. Then look at the tables in your database by running the following commands and the sqlite prompt:

  • .table, which will list the tables in the database
  • .schema category, which will show the organization of the category table, including the fields and the data types they store.
  • .schema clue

Read these short documents:

Check your understanding:

  • What tables are in the database?
  • What is a schema?
  • What fields are in the category table?
  • What fields are in the clue table?


2. Examine Effect and the interface its subclasses provide

All of the effects inherit from the Effect class. Examine this class and its __init__ and run methods.

What is the purpose of the __init__ method?

What is the purpose of the run method?

Open up run.py and look at this chunk of code at the bottom of the file:

    for effect in effects_to_run:
	new_effect = effect(wall)
        print new_effect.__class__.__name__
        new_effect.run()

effects.py exports and Effects list at the bottom of the file. run.py goes through every effect in that list, creates a new instance of the effect, and invokes its run method.

Check your understanding: what would happen if you added an effect to the Effects list that didn't implement a run method? (Try it!)

Step 2 resources:


3. Examine the nested for loop in SolidColorTest

for x in range(self.wall.width):
    for y in range(self.wall.height):
	self.wall.set_pixel(x, y, hsv)

This code loops over every pixel in the ColorWall, setting the pixel to a particular hsv value. After that for loop is over, self.wall.draw() updates the display.

Check your understanding: what would happen if you moved the self.wall.draw() to inside the inner for loop, just under self.wall.set_pixel(x, y, hsv) in SaturationTest? (Try it!)

Tip: you can run individual tests by passing their names as command line arguments to run.py. For example, if you only wanted to run SaturationTest, you could:

python run.py SaturationTest


4. Implement a new effect called RainbowTest

It should run for 5 seconds, cycling through the colors in the rainbow, pausing for a moment at each color.

Remember to add your effect to the Effect list at the bottom of effects.py!

Test your new effect with

python run.py RainbowTest


5. Play with the randomness in Twinkle

Walk through Twinkle. Find explanations of the random.randint and random.uniform functions in the online documentation at http://docs.python.org/library/random.html.

Experiment with these functions at a Python prompt:

import random
random.randint(0, 1)
random.randint(0, 5)
random.uniform(-1, 1)

Then experiment with the numbers that make up the hue and re-run the effect:

python run.py Twinkle

Challenge: make Twinkle twinkle with shades of red.


6. Implement a new effect that involves randomness!

Remember to add your effect to the Effect list at the bottom of effects.py.


Bonus exercises

1. Checkerboard

Find and change the colors used in the Checkerboards effect, and re-run the effect:

python run.py Checkerboards

Then change the line

    if (x + y + i) % 2 == 0:

to

    if (x + y + i) % 3 == 0:

re-run the effect, and see what changed.

What other patterns can you create by tweaking the math for this effect?


2. Matrix

Find and change the color of the columns in the Matrix effect, and re-run the effect:

python run.py Matrix

Each column that we see on the wall corresponds to a Column object. Add some randomness to the color used by each column (the variable whose value you changed above) using the random.random function, re-run the effect, and see what happens.


3. Write more of your own effects!

You have color, time, randomness, letters, and more at your disposal. Go nuts!


Congratulations!

You've read, modified, and added code to a software project that makes art out of pixels. Keep practicing!