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:Place bear-500x650.jpeg|200px|thumb|In which you learn how to use Python and web APIs to meet the likes of these guys!]]
[[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 placebear API to fetch bear pictures?
* 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?  
** The requests module can do that using the <code>.json()</code> function!
** There's a module for that!




Line 65: Line 66:
; New programming concepts:
; New programming concepts:


* interpolate variables into a string using format
* interpolate variables into a string using % and %()s
* [http://docs.python-requests.org/en/latest/ requests]
* 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) using the json module
* parsing a string (turning the string into a data structure we can manipulate)




; How do we use an API to fetch pictures of bears?
; How do we use an API to fetch kitten pictures?


[http://placebear.com/ placebear.com]
[http://placekitten.com/ placekitten.com]
* API that takes specially crafted URLs and gives appropriately sized picture of bears
* API that takes specially crafted URLs and gives appropriately sized picture of kittens
* Exploring placebear in a browser:
* Exploring placekitten in a browser:
** visit the API documentation
** visit the API documentation
** bears of different sizes
** kittens of different sizes
** bears in greyscale or color
** kittens in greyscale or color
* Now we write a small program to grab an arbitrary square from placebear by asking for the size on standard in ([http://mako.cc/teaching/2015/cdsw-spring/placebear_input.py placebear_input.py])
* 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(), or, even easier, just do it directly with requests using the <code>.json()</code> function!
* 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 (e.g., with a program like [http://mako.cc/teaching/2015/cdsw-spring/parse_cdswjson.py parse_cdswjson.py])
* 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
[[Category:Spring_2015_series]]
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)