def dictsort(d, case=False, by='key'):
    """Sort a dict and yield (key, value) pairs."""
    pos = {'key': 0, 'value': 1}[by]
    fn = lambda x: x[pos] if case else x[pos].lower()
    return sorted(d.items(), key=fn)
