I have set of imports that I like to have at the top of every API client I write, some are built in, but some are not. I’ll just quickly go over them before you get started on the fun stuff. This post is just to list off the modules you might need. If you’re on 64 bit OSes, you might need to take a look here for compatible packages, I know I did. For then entirety of the tutorials, I’ll be using Windows 7×64, with Python 2.7, so maybe you’ll have a different experience with Python 3, and on a different OS.*
First off, requests module is essential to everything we’ll be doing within the following tutorials. It handles all the hard-to-understand stuff, like POST, PUT, and GET requests, as well as cookies, so we don’t have to worry about it much at all; and ideal Python module.
Secondly, pprint is a nice thing to have, since we’ll be dealing a lot with dictionaries, and the regular print statement doesn’t print it off in the most readable of formats. Instead of printing everything in a single line, it linewraps, as well as uses separate lines where doing so would help with legibility. For example: a dictionary would be printed like this normally:
dictionary = {'a': 1, 'b': 2, 'c': 3}
but with pprint it’d look something more like this:
dictionary = {'a' : 1, 'b' : 2, 'c' : 3' }
which is amazing when you have huge dictionaries that you need to visually parse.
Thirdly, the json module is another essential one, as the APIs will often feed us JSON data. JSONs are essentially dictionaries writtien in Javascript. We’ll take the request data from the requests module, and feed it into the json.loads function and it’ll return a native python dict that we can manipulate like it’s not even a big deal. It’s pretty great.
Here’s an example of all three modules working together:
#imports from pprint import pprint import requests import json r = requests.get(r'http://www.reddit.com/user/tankorsmash/about/.json') #print r.text #raw text response as a string j = json.loads(r.text) #turn the json response into a python dict #print j #now it's a python dict pprint(j) #here's the final respone, printed out nice an readable format
There you have it, the basic python modules I’ll be using throughout the next few tutorials.
*thanks to EuphoriaForAll for the Python Version inclusion here
Did not know about the pprint module, looks very helpful. Thanks!
Happy to help man! It definitely makes debugging a ton easier with JSON files and other dicts. If you didn’t already know, you can import functions and objects under different names, so to make things a little easier on you, at the cost of some readability, you can can have:
So whenever you want to use the `pprint.pprint()` function, you can just call `pp()`.
Thank you for this intro – I’ve never heard of pprint before and it looks really nice!
Hey, no problem at all, I enjoyed writing it out! Did you have any issues with the API tutorials, if you’ve checked em out?
Thanks for this smashing tutorial!
Happy you liked it dude! If there’s anything else you’d like to learn about, let me know.
edit: Had to look for your blog, it’s http://fromthepoisonedwell.blogspot.ca/ not http://fromthepoisonell.blogspot.ca/ I’ll make sure to add it to my bookmarks. Be sure to submit your posts to /r/devblogs if you ever feel like it.
Keep the good stuff coming!
For a newbie lacking things to do, this was refreshing 😀
Haha nonsense, there’s always things to do!
I can’t thank you enough for introducing me to the requests module. That’s some real quality-of-life improvement!
By the way, you can do the JSON conversion with requests alone with: j = r.json()
Thanks for the kind words! That’s a good trick to know too, thanks!