Quick Python Notes

... since I always forget.

Posted by Grego on December 21, 2014

Today I wrote a helper script in python briefly to help me do some bulk file downloading. Here are some notes because I always seem to forget python when I don’t use it for a while.

Today’s topics will include string formatting, dealing with json and fetching remote files from the web.

#String Formatting To format strings you can use the .format() method on a string. For example:

>>> print 'Hello, {}'.format('World!')
Hello, World!

From my quick reading the {} can be used to denote tokens that you’re replacing. Since you can pass multiple arguments to format(), it makes sense to have something like:

>>> print 'the quick brown {0} jumps over the lazy {1}'.format('fox', 'dog')
the quick brown fox jumps over the lazy dog

Why stop there when you can get even better? How about this:

>>> print '{base_url}/{image}'.format(base_url = 'http://sample.com', image = 'balloon.jpg')
http://sample.com/balloon.jpg

#URL Requests and JSON

The next step was to request some JSON from a URL and work with it. This isn’t at all what I was working on but I’ll give an example using the open weather api from openweatherapi.org.

import urllib
import json
url_fp = urllib.urlopen('http://api.openweathermap.org/data/2.5/weather?q=98004')
weather = json.loads(url_fp.read())

json’s .loads() will load json from a string. .read() will give us the string from the file point that urllib.urlopen() gives us. The json object becomes a simple dictionary:

>>> print weather
{u'clouds': {u'all': 40}, u'name': u'Bellevue', u'coord': {u'lat': 47.62, u'lon': -122.2}, u'sys': {u'country': u'United States of America', u'sunset': 1419207603, u'message': 0.022, u'type': 1, u'id': 2948, u'sunrise': 1419177268}, u'weather': [{u'main': u'Rain', u'id': 500, u'icon': u'10n', u'description': u'light rain'}, {u'main': u'Mist', u'id': 701, u'icon': u'50n', u'description': u'mist'}], u'cod': 200, u'base': u'cmc stations', u'dt': 1419154455, u'main': {u'pressure': 1002, u'temp_min': 283.15, u'temp_max': 285.15, u'temp': 284.45, u'humidity': 87}, u'id': 5786882, u'wind': {u'speed': 4.1, u'deg': 180}}

So …
To get the temperature:

>>> print "today's weather: {min}-{max}".format(min = weather['main']['temp_min'], max = weather['main']['temp_max'])
today's weather: 283.15-285.15

now of course this response gave us temperature in kelvin, but you get the idea. If you are interested, however in using this API with degrees celcius or fahrenheit you can pass the &units=metric or &units=imperial params to the server.

#Fetching Remote Files from the Web

In addition to fetching a url, urllib is also capable of fetching a file and saving it to the filesystem directly. Extremely handy:

local_splash_image = './images/balloon.png'
image_downloader = urllib.URLopener()
image_downloader.retrieve(splash_url,  local_splash_image)

This will retrieve the remote file splash_url and save it locally to the ./images/balloon.png file.