import struct
import base64


def _serialize_linear_biases(linear, nodelist):
    """Serializes the linear biases.

    Args:
        linear: a interable object where linear[v] is the bias
            associated with v.
        nodelist (list): an ordered iterable containing the nodes.

    Returns:
        str: base 64 encoded string of little endian 8 byte floats,
            one for each of the biases in linear. Ordered according
            to nodelist.

    Examples:
        >>> _serialize_linear_biases({1: -1, 2: 1, 3: 0}, [1, 2, 3])
        'AAAAAAAA8L8AAAAAAADwPwAAAAAAAAAA'
        >>> _serialize_linear_biases({1: -1, 2: 1, 3: 0}, [3, 2, 1])
        'AAAAAAAAAAAAAAAAAADwPwAAAAAAAPC/'

    """
    linear_bytes = struct.pack('<' + 'd' * len(linear), *[linear[i] for i in nodelist])
    return base64.b64encode(linear_bytes).decode('utf-8')