<commit_before>from collections import defaultdict
import time
from heapq import heappush, heappop
from itertools import count
import sys

import graph_tool.all as gt
# can not import sub modules for some reasons!!
# e.e. gt.topology gives an error
#import graph_tool as gt
from blessed import Terminal

import utils as U
import err

term = Terminal()


class GraphGTError(Exception):
    pass


class GraphGT(object):

    def __init__(self, G=None, Type=None):

        # unused maxVert

        #self.maxVertices = 0

        self.G = (gt.Graph(directed=True) if G is None
                  else gt.Graph(G))
        # vertex_dict
        self.vd = {}#defaultdict(self.G.add_vertex)
        self.v_attr = self.G.new_vertex_property('object')
        # edge_dict
        self.edge_attr = self.G.new_edge_property('object')

    # TODO: Does't work!
    def shallow_copy(self):
        '''Something is wrong in this shallow copy.
        Gets errors when adding new edges even though add_missing=True'''
        H = GraphGT()
        H.G = gt.Graph(self.G)
        H.vd = self.vd
        H.edge_attr = self.edge_attr
        return H

    def add_edge(self, n1, n2, ci=None, pi=None):
        #print >> sys.stderr, 'gt:', n1, n2

        #v1 = self.vd[n1]
        #v2 = self.vd[n2]

        try:
            v1 = self.vd[n1]
        except KeyError:
            v1 = self.G.add_vertex()
            self.vd[n1] = v1
        try:
            v2 = self.vd[n2]
        except KeyError:
            v2 = self.G.add_vertex()
            self.vd[n2] = v2

        #v2 = self.vd[n2]

        self.v_attr[v1] = n1
        self.v_attr[v2] = n2

        #print v1, v2
        e = self.G.edge(v1, v2, add_missing=True)
        #TODO: The attributes of an existing edge will be updated.
        # Should this be the intended behavior?
        self.edge_attr[e] = {'ci': ci, 'pi': pi}

    def add_edges_from(self, iterable):
        for e in iterable:
            self.add_edge(*e)

    def get_path_attr_list(self, path, attrs):
        attr_map = defaultdict(list)
        for (n1, n2) in U.pairwise(path):
            v1 = self.vd[n1]
            v2 = self.vd[n2]
            e = self.G.edge(v1, v2, add_missing=False)
            for attr in attrs:
                attr_map[attr].append(self.edge_attr[e][attr])
        return attr_map

    def get_path_generator(self, *args):
        return GraphGT.get_all_path_generator(self, *args)

    @staticmethod
    def get_all_path_generator(
            G,<commit_msg>minor: fixed add_edge interface<commit_after>from collections import defaultdict
import time
from heapq import heappush, heappop
from itertools import count
import sys

import graph_tool.all as gt
# can not import sub modules for some reasons!!
# e.e. gt.topology gives an error
#import graph_tool as gt
from blessed import Terminal

import utils as U
import err

term = Terminal()


class GraphGTError(Exception):
    pass


class GraphGT(object):

    def __init__(self, G=None, Type=None):

        # unused maxVert

        #self.maxVertices = 0

        self.G = (gt.Graph(directed=True) if G is None
                  else gt.Graph(G))
        # vertex_dict
        self.vd = {}#defaultdict(self.G.add_vertex)
        self.v_attr = self.G.new_vertex_property('object')
        # edge_dict
        self.edge_attr = self.G.new_edge_property('object')

    # TODO: Does't work!
    def shallow_copy(self):
        '''Something is wrong in this shallow copy.
        Gets errors when adding new edges even though add_missing=True'''
        H = GraphGT()
        H.G = gt.Graph(self.G)
        H.vd = self.vd
        H.edge_attr = self.edge_attr
        return H

    def add_edge(self, n1, n2, **attr_dict_arg):#ci=None, pi=None
        attrs = {'ci': None, 'pi': None}
        #print >> sys.stderr, 'gt:', n1, n2

        #v1 = self.vd[n1]
        #v2 = self.vd[n2]

        try:
            v1 = self.vd[n1]
        except KeyError:
            v1 = self.G.add_vertex()
            self.vd[n1] = v1
        try:
            v2 = self.vd[n2]
        except KeyError:
            v2 = self.G.add_vertex()
            self.vd[n2] = v2

        #v2 = self.vd[n2]

        self.v_attr[v1] = n1
        self.v_attr[v2] = n2

        #print v1, v2
        e = self.G.edge(v1, v2, add_missing=True)
        #TODO: The attributes of an existing edge will be updated.
        # Should this be the intended behavior?
        #self.edge_attr[e] = {'ci': ci, 'pi': pi}
        attrs.update(attr_dict_arg)
        self.edge_attr[e] = attrs

    def add_edges_from(self, iterable):
        for e in iterable:
            self.add_edge(*e)

    def get_path_attr_list(self, path, attrs):
        attr_map = defaultdict(list)
        for (n1, n2) in U.pairwise(path):
            v1 = self.vd[n1]
            v2 = self.vd[n2]
            e = self.G.edge(v1, v2, add_missing=False)
            for attr in attrs:
                attr_map[attr].append(self.edge_attr[e][attr])
        return attr_map

    def get_path_generator(self, *args):
        return GraphGT.get_all_path_generator(self, *args)

    @staticmethod
    def get_all_path_generator(
            G,
{'fraction_non_alphanumeric': 0.10697674418604651, 'fraction_numerical': 0.01434108527131783, 'mean_word_length': 2.0466351829988194, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6063467', 'n_tokens_mistral': 1776, 'n_tokens_neox': 1737, 'n_words': 452}