CS 205

Data Driven Discovery

09/01/2015

Today's Plan

  • git schema - making sure your setup is ok
  • demo_welcome
  • structure of a python program
  • Textbook! + exercises
  • Our first data structures
    • Lists (Ch 8)
    • Dictionaries (Ch 9)
  • JSON
  • demo_RandomPairs

Reset your git

# Ensure you are on your master branch
git checkout master

# Grab the latest release files
git fetch release

# Reset your repo to the latest release files
#  !!!: will delete any work you did
#       (but there should be none)
git reset --hard release/master

# Push your local changes to your private repo
git push origin master

Python Basics

A generic Python file (anything.py) looks like:


  1. Import library and functions
    • from libraries you install
    • from local modules you create
  2. Define additional functions you need
    • all functions must be defined before they are called
  3. Execute the functions to accomplish your goals

Python Resource

Textbook: Python for Informatics
[ http://do1.dr-chuck.com/py4inf/EN-us/book.pdf ]

See chapters 3, 4, and 5. Review of ideas, new syntax.


Today: emphasis on lists, dictionaries

Exercise: http://repl.it/BEmk/3

Python List:

Python’s implementation of an array:

  • collection of values
  • indexed via small integers

# Example:
pastas = ['spaghetti', 'macaroni', 'penne']
print pastas[2]
pastas.append('farfalle')
# What happens for these?
mess = [ 'you', 8, ['breakfast', 'lunch', 'dinner']]
mess[2][0]
mess[2][0][4]

Python List?

A list (array) is a pairing of integer indices to data values:
  a = ['bat', 'cat', 'hat', 'rat', 'vat', 'yacht']
--------------------------------------------------
index:   0      1      2      3      4       5
value: 'bat'  'cat'  'hat'  'rat'  'vat'  'yacht'

What if we wanted to generalize the pairing to indices of any type?
index: 'bate' 'gato' 'sombrero' 'rata' 'tina' 'yate'
value: 'bat'  'cat'  'hat'      'rat'  'vat'  'yacht'

Python Dictionary

Python’s implementation of an associative array
  • collection of key:value pairs
  • indexed via keys of any type (often strings)
  • very useful in many contexts

What if we wanted to generalize the pairing to indices of any type?

index:  656565656  |  656787787
value:   'Bert'    |   'Ernie'

Python Dictionary Examples

UIN2name = {656565656:'Bert', 656787787:'Ernie'}
UIN2name[656123123] = 'Cookie' #adds to the dictionary
print UIN2name
print UIN2name[656565656]
First, a JSON file (res/data.json):
[
  {
    "new": "res/packers2.jpg",
    "orig": "res/ssfans.jpg"
  }
]
  • How would you describe it?
  • How many objects does it represent?

JSON is a standard interface commonly used between programs. We use it as output from python, and input to JavaScript.

JSON

The python code that produced data.json:
import json
orig_name = "packerfy2.jpg"
new_name = "ssfans.jpg"
outData = [{'orig': 'res/' + orig_name,
            'new': 'res/' + new_name}]

f = open('res/data.json', 'w')
f.write(json.dumps(outData,indent=4))
What does json.dumps() do?

Challenges 1 & 2

Write a piece of code to create the following data.json:
# Challenge 1

[
  {
    "lunch": "JJ",
    "breakfast": "eggs",
    "dinner": "soup"
  },
  {
    "lunch": "salad",
    "breakfast": "toast",
    "dinner": "pizza"
  }
]
# Challenge 2
{
  data: [
    {
      "lunch": "JJ",
      "breakfast": "eggs",
      "dinner": "soup"
    },
    {
      "lunch": "salad",
      "breakfast": "toast",
      "dinner": "pizza"
    }
  ]
}

Challenges 3

Write a piece of code to create the following data.json:
{
  "data":[
    {
      "inputs": {
        "left": "kitty",
        "right": "puppy",
      },
      "result": "catdog",
    }
  ]
}

Add an exercise

  1. add exName directory in workbook
  2. add py, res, web directories in workbook/exName
  3. in workbook/exName
    • /py, add compute.py containing function do_compute()
    • /web, add index.html and vis.js
    • /res, add resource files
  4. add project.json in workbook/exName


(shortcut: copy exName of some other exercise that has similar features to the one you’re creating.)

Closing Words

Come to class EVERY TIME

Bring your laptop EVERY TIME