Fork me on GitHub

Eleventh Class Notes

by Elliott Hauser

30 Sep 2013

Announcements

  • Thanks to Kyle for posting a project idea: web scraping/content analysis. You'll see some of Grant's work on Gutenberg might be useful for the latter part of that.
  • Github pages. http://{your-username}.github.io/fall2013 is different than how Github.com will display markdown snippets. Check the github.io site to see if it's building right
  • Mini-activity: Merge from master into your repository.

Questions?

Code Review

Let's look at some code.

Codingbat

Anyone have a codingbat solution that you're particularly proud of? Let's see it!

Alex's Extra-curricular Code

A python script to generate SQL Insert statements for a comic book database. Very cool!

Grant's Extra Credit

Grant's Extra Credit exercise, explained by the man himself.

File I/O

Fire up Ubuntu. Open up two Terminal windows.

There's a built-in function called open() that we can use to deal with files in python.

In one Terminal, create a text file:

> nano names.txt

Elliott
George
Sally

(In nano, press Ctrl-O to save, Ctrl-X to exit)

We can see what's in that file by typing cat names.txt.

> cat names.txt
Elliott
George
Sally
>

Now let's use the other Terminal window. Fire up python by typing (you guessed it), python

> python
 [some text will be displayed here]
>>>

The >>> is the python prompt, letting you know Python is ready to do your bidding. Let's read in our text file and print it out:

# I read in the text file to a variable I chose to call "text_file"
>>> text_file = open('names.txt')

This stores the way to get to the file in the text_file variable. If we want to see what's inside of it, we have to tell Python to read the file contents.

# the text_file variable is just a pointer
>>> text_file
<open file 'names.txt', mode 'r' at 0x3564f0>

# but we can also read in the contents of the file
>>> text_file.read()
Elliott
George
Sally

Program time

You'll find the following file here. Visit that link (in Ubuntu) and save it to your home folder (the one containing the Documents, Downloads, Music etc folders).

Complete the two exercises and submit your code by the end of class or midnight.

def add_names(list_of_names, file):
  """
  Opens and adds a list of names to the end of a file, each on its own line
  """
  # We open a file in 'a' mode, for appending to it.
  names_file = open(file, 'a')

  # For each line in the list, we print that to the file. 
  # This assumes one file per line.
  for name in list_of_names:
    print >> names_file, name

  # Close the file so the changes are visible.
  names_file.close()


# Exercise: make new_names customizible:
new_names = ['John', 'Sarah', 'Taj']

# Exercise: make the file name used here customizible:
add_names(new_names, 'names.txt')
Elliott Hauser is a PhD Student in information science at UNC Chapel Hill. He's hacking education as one of the cofounders of Coursefork.org. Find Elliott Hauser on Twitter, Github, and on the web.
comments powered by Disqus