Skip to content

Instantly share code, notes, and snippets.

@cournape
Created November 16, 2012 13:29
Show Gist options
  • Save cournape/4087369 to your computer and use it in GitHub Desktop.
Save cournape/4087369 to your computer and use it in GitHub Desktop.
baby name exercise

""" This exercise allows you to take a brief look at DB capabilities of python and the SQL Alchemy library

this directory contains the names.zip file, which contains lists of baby names given in the US, one list per year. Each list is a csv file of the following format:

name, sex, number of names

List available at: http://www.ssa.gov/oact/babynames/limits.html

Question 1

After unzipping the file, write a function that can parse the file and return a list of (name, sex, number). Use namedtuples for each row.

Question 2

Using the DB API from python, create a table that will contain contain all the information of one csv file. You may use the following SQL statement to create a table:

CREATE TABLE babies (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(100), sex VARCHAR(10), age INTEGER)

Once the table is created, insert the data obtained from the call to the function written in question 1.

Use the database to answer the following questions:

  • how many names have more than 5000 occurences in 2005 ?
  • how many babies were born in the US in 2005 (assuming every born baby is included in the csv file) ?

Question 3

Using SQL Alchemy, create a table object that represents the previous babies table. Use this table to answer the same questions as in question 2

Question 4

Use the ORM capabilities of SQLAlchemy to build a Baby object that contains a name, an age and a sex. Use the ORM to answer the same questions as in question 2

Bonus question

Try to do the same exercises, but including every year (you need to add a column to include the year in your SQL table). """

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment