import six


def dict_to_string(dictionary, indent=4):
    """Serialize dict as string

    Parameters
    ----------
    dictionary: dict
    indent: int
        Number of spaces to indent per level

    Returns
    -------
    str
    """
    if len(dictionary) == 0:
        return "{}"
    indentation = " " * indent
    string = "{\n"
    for key, val in six.iteritems(dictionary):
        if isinstance(val, dict):
            if len(val) == 0:
                string += "{}{}: {{}}".format(indentation, repr(key))
            else:
                string += "{}{}: ".format(indentation, repr(key))
                dict_string_lines = dict_to_string(val).splitlines()
                string += dict_string_lines[0]
                for line in dict_string_lines[1:]:
                    string += "\n{}{}".format(
                        indentation, line
                    )
        else:
            string += "{}{}: {}".format(
                indentation, repr(key), repr(val)
            )
        string += ",\n"
    string += "}"
    return string