def write_bin_labels(mapper, dest, header='# bin labels:\n', fmt=
    """# bin {index:{max_iwidth}d} -- {label!s}
"""):
    """Print labels for all bins in ``mapper`` to the file-like object``dest``.
    
    If provided, ``header`` is printed prior to any labels. A number of expansions
    are available in ``header``:
      * ``mapper`` -- the mapper itself (from which most of the following can be obtained)
      * ``classname``  -- the class name of the mapper
      * ``nbins`` -- number of bins in the mapper 
      
    The ``fmt`` string specifies how bin labels are to be printed. A number of
    expansions are available in ``fmt``:
      * ``index`` -- the zero-based index of the bin
      * ``label`` -- the label of the bin
      * ``max_iwidth`` -- the maximum width (in characters) of the bin index, for pretty alignment
    """
    if header:
        dest.write(header.format(mapper=mapper, classname=mapper.__class__.
            __name__, nbins=mapper.bins))
    max_iwidth = len(str(mapper.nbins - 1))
    for ibin, label in enumerate(mapper.labels):
        dest.write(fmt.format(index=ibin, label=label, max_iwidth=max_iwidth))
