import copy


def _convert_dicttype(dictionary, newtype=dict, convert_nested=True, method='deepcopy'):
    """Packs a nested dictionary to a one-level dictionary with tuple keys.

    Parameters
    ----------
    dictionary: dict
        A dictionary or dictionary subtype, optionally with nested dictionaries
    newtype: type
        The new dictionary (sub)type
    convert_nested: bool
        Whether nested dictionaries should be converted as well. Defaults to True
    method: str
        The method used to copy values. One of 'deepcopy', 'copy' and 'assign'. Defaults to 'deepcopy'

    Returns
    -------
    object
        The converted dictionary type
    """
    ret = newtype()
    for key, value in dictionary.items():
        if convert_nested and isinstance(value, dict):
            ret[key] = _convert_dicttype(value, newtype=newtype, convert_nested=convert_nested, method=method)
        else:
            if method == 'deepcopy':
                ret[key] = copy.deepcopy(value)
            elif method == 'copy':
                ret[key] = copy.copy(value)
            elif method == 'assign':
                ret[key] = value
            else:
                raise ValueError()
    return ret