Chicago Python Workshop/Chicago Python Workshop 1/Saturday projects/Wordplay: Difference between revisions

From OpenHatch wiki
Content added Content deleted
imported>Aisha
(Created page with "== Project == Use the Wordplay API to write the basic parts of a Twitter client. See what your friends are tweeting, get trending topics, search tweets, and more! ==Goals== ...")
 
imported>Codersquid
 
(5 intermediate revisions by one other user not shown)
Line 1: Line 1:
== Project ==
== Project ==


# Review relevant material from lecture (e.g. <tt>for</tt> loops)
Use the Wordplay API to write the basic parts of a Twitter client. See what your friends are tweeting, get trending topics, search tweets, and more!
# Go over <tt>words1.py</tt> through <tt>words6.py</tt> as a class

# Demo <tt>scrabble_cheater.py</tt>
==Goals==
# Create fake_scrabble.py where you use scrabble_cheater as a module to return all possible words that could be placed against a letter.

## For this project, your script should run like this: <code>python fake_scrabble.py <RACK> <BOARD_LETTERS></code>
* Have fun playing with data from Twitter.
## You can assume that there is plenty of space between letters in BOARD_LETTERS on the board.
* See how easy it is to programmatically gather data from social websites that have APIs.
## You should also modify scrabble_cheater.py to return what it prints out, and put the surrounding code under a function definition.
* Get experience with command line option parsing and passing data to a Python script.
# If you have more time
* Get experience reading other people's code.
## Once you select a word, refresh the rack with new letters, and then add to the front and back letters of the word on the BOARD_LETTERS.

==Project steps==

=== 1. Read through and understand <code>search</code> ===

# Run <code>python twitter_api.py --search</code> with various search terms, e.g.
#* <code>python twitter_api.py --search=Python</code>
#* <code>python twitter_api.py --search="Red Sox"</code>
# Read through the <code>search</code> function in <code>twitter_functions.py</code>.
# Trace through the logic in <code>twitter_api.py</code> that turns the <code>--search</code> command line option into a call to <code>search</code>.
# Find <code>GetSearch</code> in the Twitter library code at http://code.google.com/p/python-twitter/source/browse/twitter.py. What other options could we have passed to <code>GetSearch</code>?

<b>Check your understanding</b>: What does <code>api.GetSearch</code> return?


=== 2. Read through and understand <code>trendingTopics</code> ===

# Run <code>python twitter_api.py --trending-topics</code>
# Read through the <code>trendingTopics</code> function in <code>twitter_functions.py</code>.
# Trace through the logic in <code>twitter_api.py</code> that turns the <code>--trending-topics</code> command line option into a call to <code>search</code>.
# Find <code>GetTrendsWoeid</code> in the Twitter library code at http://code.google.com/p/python-twitter/source/browse/twitter.py. How many trending topics does that method return?

<b>Check your understanding</b>: What are the differences between the <code>optparse</code> logic for <code>--search</code> and <code>--trending-topics</code>?
You could refer to the <code>optparse</code> module for parsing command-line options: http://docs.python.org/library/optparse.html

=== 3. Implement <code>userTweets</code> ===

<ol>
<li>Using the <code>search</code> and <code>trendingTopics</code> functions as a reference, implement <code>userTweets</code> in <code>twitter_functions.py</code>.

This function should print recent tweets by the username provided on the command line.

You may find the <code>twitter.Api()</code> function [http://code.google.com/p/python-twitter/source/browse/twitter.py#2628|<code>GetUserTimeline()</code>] helpful.

Currently, the code in <code>twitter_functions.py</code> is empty, with only a comment, and a <code>pass</code> statement that allows us to define this function without actually doing anything.

<pre>def userTweets(username):
"""
Print recent tweets by `username`.

You may find the twitter.Api() function GetUserTimeline() helpful.

To test this function, at the command line run:
python twitter_api.py -u <username>
For example,
python twitter_api.py -u bostonpython
"""
pass</pre>

</li>
<li>We've already written some <code>optparse</code> logic for <code>userTweets</code> in <code>twitter_api.py</code>. Read through that logic. In what ways is it the same/different from the logic for <code>search</code>?
<li>Test your function. You can do this with:

<pre>python twitter_api.py -u <username></pre>

For example,

<pre>python twitter_api.py -u Orbitz</pre>
or,
<pre>python twitter_api.py -u zoro_tools</pre>
</li>

You should see 20 tweets by the provided username. If you have a Twitter account, try running it on yourself, tweeting something new, and running it again!
</ol>

<b>Check your understanding</b>: What are the two ways to pass command line arguments for <code>userTweets</code>?

=== 4. Implement <code>trendingTweets</code> ===

<ol>
<li>Using the <code>search</code> and <code>trendingTopics</code> functions as a reference, implement <code>trendingTweets</code> in <code>twitter_functions.py</code>.

This function should print a couple of recent tweets from each of the currently trending topics.

<li>We've already written some <code>optparse</code> logic for <code>trendingTweets</code> in <code>twitter_api.py</code>. Read through that logic. In what ways is it the same/different from the logic for <code>search</code>?
<li>Test your function. You can do this with:

<pre>python twitter_api.py -w</pre>
</li>

You should see recent tweets from all of the currently trending topics. Depending on how you implemented your function, you might see a chunk of tweets from each trending topic in turn, or tweets from each topic interleaved.
</ol>

<b>Check your understanding</b>: What is the purpose of <code>CHICAGO_WOEID</code> in <code>twitter_functions.py</code>?

== Bonus exercises ==

If you have time, try out some of these extra exercises.

=== 1. Customize how tweets are printed by <code>search</code> ===

The tweets printed by <code>search</code> could look much nicer and have more useful metadata!

Customize how tweets are displayed. Look at the <code>Status</code> and <code>User</code> classes in http://code.google.com/p/python-twitter/source/browse/twitter.py for inspiration; options include displaying:
* the sender of the tweet
* the URL for the tweet
* how many followers the sender has
* the location of the sender
* if it was a retweet

and more!

=== 2. Print trending topics by location ===

Extend <code>trendingTopics</code> so that a Yahoo! Where On Earth ID (WOEID) can be specified on the command line and trending topics for that location will be displayed instead of using the hardcoded <code>CHICAGO_WOEID</code> to only display results for Chicago.

You can look up WOEIDs by location at http://sigizmund.info/woeidinfo/

===Congratulations!===
===Congratulations!===


You've read, modified, and added code to a software project that makes it easy to get useful information from Twitter. Keep practicing!
You've read, modified, and added code to a software project that makes it easy to get useful information from words. Keep practicing!


[[File:Fireworks.png|150px]]
[[File:Fireworks.png|150px]]

Latest revision as of 17:30, 28 November 2012

Project

  1. Review relevant material from lecture (e.g. for loops)
  2. Go over words1.py through words6.py as a class
  3. Demo scrabble_cheater.py
  4. Create fake_scrabble.py where you use scrabble_cheater as a module to return all possible words that could be placed against a letter.
    1. For this project, your script should run like this: python fake_scrabble.py <RACK> <BOARD_LETTERS>
    2. You can assume that there is plenty of space between letters in BOARD_LETTERS on the board.
    3. You should also modify scrabble_cheater.py to return what it prints out, and put the surrounding code under a function definition.
  5. If you have more time
    1. Once you select a word, refresh the rack with new letters, and then add to the front and back letters of the word on the BOARD_LETTERS.

Congratulations!

You've read, modified, and added code to a software project that makes it easy to get useful information from words. Keep practicing!