def parse_slab_stats(slab_stats):
    """Convert output from memcached's `stats slabs` into a Python dict.

    Newlines are returned by memcached along with carriage returns
    (i.e. '\r\n').

    >>> parse_slab_stats(
            "STAT 1:chunk_size 96\r\nSTAT 1:chunks_per_page 10922\r\nSTAT "
            "active_slabs 1\r\nSTAT total_malloced 1048512\r\nEND\r\n")
    {
        'slabs': {
            1: {
                'chunk_size': 96,
                'chunks_per_page': 10922,
                # ...
            },
        },
        'active_slabs': 1,
        'total_malloced': 1048512,
    }
    """
    stats_dict = {'slabs': defaultdict(lambda: {})}

    for line in slab_stats.splitlines():
        if line == 'END':
            break
        # e.g.: "STAT 1:chunks_per_page 10922"
        cmd, key, value = line.split(' ')
        if cmd != 'STAT':
            continue
        # e.g.: "STAT active_slabs 1"
        if ":" not in key:
            stats_dict[key] = int(value)
            continue
        slab, key = key.split(':')
        stats_dict['slabs'][int(slab)][key] = int(value)

    return stats_dict
Convert output from memcached's `stats slabs` into a Python dict.

    Newlines are returned by memcached along with carriage returns
    (i.e. '\r\n').

    >>> parse_slab_stats(
            "STAT 1:chunk_size 96\r\nSTAT 1:chunks_per_page 10922\r\nSTAT "
            "active_slabs 1\r\nSTAT total_malloced 1048512\r\nEND\r\n")
    {
        'slabs': {
            1: {
                'chunk_size': 96,
                'chunks_per_page': 10922,
                # ...
            },
        },
        'active_slabs': 1,
        'total_malloced': 1048512,
    }