<commit_before>from devilry.core.utils import OrderedDict


def group_assignmentgroups(assignment_groups):
    return group_nodes(assignment_groups, 2)

def group_assignments(assignments):
    return group_nodes(assignments, 1)


def group_nodes(nodes, tree_height):

    dict = OrderedDict()

    for node in nodes:
        list = make_node_list(Node(node), tree_height)
        
        if not dict.has_key(list.get_name()):
            dict[list.get_name()] = list
        else:
            dict[list.get_name()].merge(list)

    return dict.itervalues()


def make_node_list(child_node, list_count):
    
    parent = Node(child_node.node.parentnode) 
    parent.add_child(child_node)

    if list_count == 0:
        return parent
    else:
        list_count -= 1
        return make_node_list(parent, list_count)


class Node(object):

    def __init__(self, node):
        self.children = OrderedDict()
        self.node = node
            
    def __unicode__(self):
        
        if hasattr(self.node, 'long_name'):
            return self.node.long_name
        else:
            return self.node.__unicode__()

    def get_name(self):
        return self.__unicode__()

    def add_child(self, child_node):
        
        if not hasattr(child_node.node, 'short_name'):
            self.children[child_node] = child_node
        else:
            if not self.children.has_key(child_node.get_name()):
                self.children[child_node.get_name()] = child_node
            else:
                self.children[child_node.get_name()].merge(child_node)

    def merge(self, list):
        for n in list:
            self.add_child(n)

    def __iter__(self):
        return iter(self.children.values())

def print_tree(node):
    for child in node:
         print child.get_name()<commit_msg>Display candidates when student is member of multiple AssignmentGroups<commit_after>from devilry.core.utils import OrderedDict


def group_assignmentgroups(assignment_groups):
    return group_nodes(assignment_groups, 2)

def group_assignments(assignments):
    return group_nodes(assignments, 1)


def group_nodes(nodes, tree_height):
    dict = OrderedDict()

    for node in nodes:
        list = make_node_list(Node(node), tree_height)
        
        if not dict.has_key(list.get_name()):
            dict[list.get_name()] = list
        else:
            dict[list.get_name()].merge(list)

    return dict.itervalues()


def make_node_list(child_node, list_count):
    parent = Node(child_node.node.parentnode) 
    parent.add_child(child_node)

    if list_count == 0:
        return parent
    else:
        list_count -= 1
        return make_node_list(parent, list_count)


class Node(object):

    def __init__(self, node):
        self.children = OrderedDict()
        self.node = node
        self.display_group = False
     
    def __unicode__(self):
        if hasattr(self.node, 'long_name'):
            return self.node.long_name
        else:
            if self.display_group:
                return self.node.parentnode.long_name + " (" + self.node.get_candidates() + ")"
            else:
                return self.node.parentnode.long_name

    def get_name(self):
        return self.__unicode__()

    def add_child(self, child_node):
        # Assignment group doesn't have short_name
        if not hasattr(child_node.node, 'short_name'):
            # Makes sure the candidates are shown if a student 
            # is part of more than one AssignmentGroup
            if len(self.children) != 0:
                child_node.display_group = True

                # Contains only one, set display_group to True for than element as well.
                if len(self.children) == 1:
                    self.children.values()[0].display_group = True

            self.children[child_node] = child_node
        else:
            if not self.children.has_key(child_node.get_name()):
                self.children[child_node.get_name()] = child_node
            else:
                self.children[child_node.get_name()].merge(child_node)

    def merge(self, list):
        for n in list:
            self.add_child(n)

    def __iter__(self):
        return iter(self.children.values())

def print_tree(node):
    for child in node:
         print child.get_name()
{'fraction_non_alphanumeric': 0.09416414787258777, 'fraction_numerical': 0.0025575447570332483, 'mean_word_length': 2.2889908256880735, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3606317', 'n_tokens_mistral': 1319, 'n_tokens_neox': 1264, 'n_words': 282}