import copy


def copy_node_features(copy_from,copy_to):
    """
    Copy features from one node to another. Use copy.deepcopy to make sure
    complex features are preserved. This copies features from copy_from
    and wipes them out on copy_to. Any features unique to copy_to are
    preserved. Note this works on the object itself, not a copy.

    copy_from: ete3 TreeNode object with features to copy
    copy_to: ete3 TreeNode object with features to load in

    returns copy_to with newly copied features
    """

    for f in copy_from.features:
        try:
            copy_to.add_feature(f,copy.deepcopy(copy_from.__dict__[f]))
        except KeyError:
            pass

    return copy_to