Editing DS4UX (Spring 2016)/Day 5 lecture

From CommunityData
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 28: Line 28:
* Web APIs: like a website your programs can visit (you:a website::your program:a web API)
* Web APIs: like a website your programs can visit (you:a website::your program:a web API)


=== Download the Week 5 lecture scripts and data ===


<big>'''[http://jtmorgan.net/ds4ux/week5/lecture.zip Click here to download the week 5 lecture scripts and data examples]'''</big>
# The ".zip" extension on the above file indicates that it is a compressed Zip archive. We need to "extract" its contents.
# Start up your terminal, navigate to the new directory you have unpacked called <code>lecture</code>


=== How do we use an API to fetch datasets? ===
=== How do we use an API to fetch datasets? ===
Line 58: Line 53:
** kittens of different sizes
** kittens of different sizes
** kittens in greyscale or color
** kittens in greyscale or color
* Now we are ready to walk through a small program (<code>placekitten_input.py</code>) that grabs a random picture from placekitten.com of a user-specified height and width, and saves that image as a .jpg image on your computer.
* Now we write a small program to grab an arbitrary square from placekitten by asking for the size on standard in.
 
 


== Lecture 2: Using data from APIs in Python ==
== Lecture 2: Using data from APIs in Python ==
Line 75: Line 72:


* what is json: useful for more structured data
* what is json: useful for more structured data
* import json; json.load(), json.loads()
* import json; json.loads()
* like Python (except no single quotes)
* like Python (except no single quotes)
* simple lists, dictionaries
* simple lists, dictionaries
* can reflect more complicated data structures
* can reflect more complicated data structures
* Example file at FIXME
* You can parse data directly with <code>.json()</code> on a <code>requests</code> call
* You can parse data directly with <code>.json()</code> on a <code>requests</code> call
* You can parse JSON files with <code>json.load()</code>
* You can parse JSON ''strings'' with <code>json.loads()</code> (the 's' is for 'string')
* Example JSON file: <code>family.json</code>
* script to parse this file into a CSV: <code>family_json_to_csv.py</code>
* script to parse and print a ''JSON string'' version of this example file: <code>parse_json.py</code>


=== Using other APIs ===
=== Using other APIs ===
Please note that all contributions to CommunityData are considered to be released under the Attribution-Share Alike 3.0 Unported (see CommunityData:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:

Cancel Editing help (opens in new window)