Editing Community Data Science Workshops (Spring 2015)/Day 2 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 1: | Line 1: | ||
[[File: | [[File:Highfivekitten.jpeg|200px|thumb|In which you learn how to use Python and web APIs to meet the likes of her!]] | ||
== Lecture Outline == | == Lecture Outline == | ||
Line 12: | Line 12: | ||
* What did we learn in Session 1? | * What did we learn in Session 1? | ||
* New data types: Set and Tuple! | |||
* What is an API? | * What is an API? | ||
* How do we use one to fetch interesting datasets? | * How do we use one to fetch interesting datasets? | ||
* How do we write programs that use the internet? | * How do we write programs that use the internet? | ||
* How can we use the | * How can we use the placekitten API to fetch kitten pictures? | ||
* Introduction to structured data (JSON) | * Introduction to structured data (JSON) | ||
* How do we use APIs in general? | * How do we use APIs in general? | ||
Line 38: | Line 39: | ||
** Structured data (usually in the JSON format) | ** Structured data (usually in the JSON format) | ||
* How do you understand (i.e. parse) the data? | * How do you understand (i.e. parse) the data? | ||
** | ** There's a module for that! | ||
Line 65: | Line 66: | ||
; New programming concepts: | ; New programming concepts: | ||
* interpolate variables into a string using | * interpolate variables into a string using % and %()s | ||
* | * requests | ||
* open files and write to them | * open files and write to them | ||
* parsing a string (turning the string into a data structure we can manipulate) | * parsing a string (turning the string into a data structure we can manipulate) | ||
; How do we use an API to fetch pictures | ; How do we use an API to fetch kitten pictures? | ||
[http:// | [http://placekitten.com/ placekitten.com] | ||
* API that takes specially crafted URLs and gives appropriately sized picture of | * API that takes specially crafted URLs and gives appropriately sized picture of kittens | ||
* Exploring | * Exploring placekitten in a browser: | ||
** visit the API documentation | ** visit the API documentation | ||
** | ** kittens of different sizes | ||
** | ** kittens in greyscale or color | ||
* Now we write a small program to grab an arbitrary square from | * Now we write a small program to grab an arbitrary square from placekitten by asking for the size on standard in: [http://mako.cc/teaching/2014/cdsw-autumn/placekitten_raw_input.py placekitten_raw_input.py] | ||
Line 85: | Line 86: | ||
* what is json: useful for more structured data | * what is json: useful for more structured data | ||
* import json; 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 http://mako.cc/cdsw.json | * Example file at http://mako.cc/cdsw.json | ||
* download it and parse it | * download it and parse it: [http://mako.cc/teaching/2014/cdsw-autumn/parse_cdswjson.py parse_cdswjson.py] | ||
Line 108: | Line 109: | ||
* [http://mako.cc/teaching/2014/cdsw-autumn/lecture2-web_apis.pdf Slides (PDF)] — For viewing | * [http://mako.cc/teaching/2014/cdsw-autumn/lecture2-web_apis.pdf Slides (PDF)] — For viewing | ||
* [http://mako.cc/teaching/2014/cdsw-autumn/lecture2-web_apis.odp Slides (ODP Libreoffice Slides Format)] — For editing and modification | * [http://mako.cc/teaching/2014/cdsw-autumn/lecture2-web_apis.odp Slides (ODP Libreoffice Slides Format)] — For editing and modification | ||