Display an image from a file in an IPython Notebook

from IPython.display import Image Image(filename=’test.png’)

In Python, how do I determine if an object is iterable?

try: some_object_iterator = iter(some_object) except TypeError, te: print some_object, ‘is not iterable’

try: _ = (e for e in my_object) except TypeError: print my_object, ‘is not iterable’

import collections

if isinstance(e, collections.Iterable): # e is iterable


Best way to parse a URL query string

from urlparse import urlparse, parse_qs URL=’https://someurl.com/with/query_string?i=main&mode=front&sid=12ab&enc=+Hello’ parsed_url = urlparse(URL) parse_qs(parsed_url.query) {‘i’: [‘main’], ‘enc’: [’ Hello ‘], ‘mode’: [‘front’], ‘sid’: [‘12ab’]}

modue six, urllib.parse

string format

content = u'''
        <td> 响应时间平均值 </td>
        <td> {mean:.4f} </td>
        <td> 响应时间75% </td>
        <td> {seventy_five:.4f} </td>
        <td> 响应时间95% </td>
        <td> {ninety_five:.4f} </td>
        <td> 可用性 </td>
        <td> {ava} </td>
    'mean': df.mean()[0],
    'seventy_five': df.quantile(.75)[0],
    'ninety_five': df.quantile(.95)[0],
    'ava': ava

Using % and .format() for great good!

How can I use Python to get the system hostname?

import socket

import platform


Python: defaultdict of defaultdict?

defaultdict(lambda : defaultdict(int))

Python: defaultdict of defaultdict

get random string

Generating strings from (for example) lowercase characters:

import random, string

def randomword(length):
   return ''.join(random.choice(string.lowercase) for i in range(length))

>>> randomword(10)
>>> randomword(10)

get random string