Best Python code snippet using hypothesis
funcs.py
Source:funcs.py  
1import time2import os3import numpy as np4import elasticsearch as es5from elasticsearch import Elasticsearch6from gensim.summarization import keywords7from collections import Counter8import gexf9import redis10from igraph import *11121314'''15Main class representing similarity graph extracted from seeds16    17    Attributes: integer k : min # common lists to create edge18                string encoding the desired output in the dictionary of seeds19                list of seeds20                dictionary of edges with valences21                set of vertices ids22                dictionary of frequency of visits of each vertice23                dictionary of Louvain communities membership24                dictionary of list ids of each community25                dictionary of keywords of lists in each community26                dictionary of information about vertices (frequency, name, cluster membership)27    28    Methods:__init__ : Either connects to redis to grow the graph from the seeds or reads29                        a precomputed graph30            totxt : writes the edges of the graph and their valences (dict of dicts)31                    writes the frequency of visits of each vertice by the random walk32            louvain_communities : Either computes the communities using Louvain algo and33                            connect to redis to grab list ids in communities or reads34                            precomputed files35            clusters_kws : Connects to elasticsearch and calculate word frequency inside36                            each cluster37            to_gexf : export graph in gexf format38            get_users_info : connects to elasticsearch to get screennames of vertices and39                            summarizes information about users in gaph40'''41class graph:42    K = 043    champ = ''44    seeds = []45    edges = {}46    vertices = set()47    vertices_frequency = {}48    vertices_communities = {}49    communities_lists = {}50    communities_kws = {}51    user_names_info = {}525354    def __init__(self, *args, **kwargs):55        K = kwargs['K']56        champ = kwargs['champ']57        dic_champs= kwargs['dic_champs']58        unwanted_lists = kwargs['unwanted_lists']59        self.K = K60        self.champ = champ61        seeds = dic_champs[champ]62        self.seeds = seeds63        if args[0] == 0:64            print('Initialisation {} : {} seeds k = {}'.format(champ,len(seeds),K))65            reduced_graphs = {}66            nodes_intensity = {}67            for seed in seeds:68                print('Starting with {}'.format(seed))69                d, dc1 = get_corona(seed, unwanted_lists, K)70                d2 = get_corona_2(dc1, unwanted_lists, K)71                final_dict, dic, dic_i = merge_dict(d, d2, seed)72                size = len(final_dict.keys())73                matrix = build_matrix(final_dict, size)74                rp, dic_paths = get_paths(matrix, dic_i[seed[7:]], dic)75                rg = reduced_graph(dic_paths.keys(), final_dict, dic)76                reduced_graphs[seed] = rg77                nodes_intensity[seed] = dic_paths78            79            self.edges = merge_graphs(reduced_graphs, seeds)80            #self.vertices = final_graph.keys()81            final_intensities = merge_intensity(nodes_intensity, seeds)82            self.vertices = set(final_intensities.keys())83            self.vertices_frequency = final_intensities84        else:85            print('Reading graph {} : k = {}'.format(champ, K))86            self.edges, self.vertices_frequency = read_graph_freq(champ, K)87            self.vertices = set(self.edges.keys())8889    def totxt(self):90        write_dict(self.edges, self.vertices_frequency, self.champ, self.K)9192    def louvain_communities(self, done = False):93        if done:94            dic_clusters, dic_lists_clusters = read_com(self.champ, self.K)95        else:96            l_edges, weights = edgelist(self.edges)97            l_vertices = self.vertices98            l_vertices = list(set(l_vertices))99            l_vertices = sorted([int(t) for t in l_vertices])100            l_vertices = [str(t) for t in l_vertices]101            #print('1')102            g = Graph(directed = False)103            g.add_vertices(l_vertices)104            #print('2')105            g.add_edges(l_edges)106            g.es['weight'] = weights107            #print('3')108            p = g.community_multilevel(weights = weights)109            dic_lists_clusters = {}110            dic_clusters = {}111            r = redis.StrictRedis(host='localhost')112            for i,cl in enumerate(p):113                lists = set()114                ids = set()115                for v in cl:116                    v = g.vs[v]['name']117                    ids.add(v)118                    for listname in r.smembers('userId:' + v):119                        lists.add(listname.decode('utf-8'))120                dic_lists_clusters[i] = lists121                dic_clusters[i] = ids122123            write_clusters(dic_clusters, dic_lists_clusters, self.champ, self.K)124            #print(g.modularity(p))125        self.vertices_communities = dic_clusters126        self.communities_lists = dic_lists_clusters127        return dic_clusters, dic_lists_clusters128        #print([g.vs[t]['name'] for t in p[0]], p[1])129        #print(q)130        #print(g.vcount())131        #print(g.ecount())132133    def clusters_kws(self, done = False):134        if done:135            c_kws = read_kws(self.champ, self.K)136        else:137            es_t = Elasticsearch([{'local': 'localhost'}])138            c_kws = {}139            d_names = {}140            for k, v in self.communities_lists.items():141                #maybe a list to take into account # of occurences142                names = set()143                #print(str(k))144                for l in v:145                    nm = get_list_name(es_t,l)146                    names.add(nm)147                d_names[k] = names148                txt = ' '.join(list(names))149                wds = min(5,len(names))150                #print('--------------doing kws')151                #kws = keywords(txt, words = wds)152                ws = txt.split(' ')153                unwanted_words = ['/','et','&', '-', '***NoListName***', 'de', ',', '_', '.', 'the', 'of', 'les', 'le', 'la', 'and', 'list','my']154                ws = [x.strip() for x in ws if len(x)>0]155                ws = [x.lower() for x in ws]156                ws = [x for x in ws if x not in unwanted_words]157                kws = Counter(ws).most_common(wds)158                print(kws)159                print('\n')160                c_kws[k] = kws161                #print('--------------done with kws')162                #c_kws[k] = [t.encode('ascii', 'ignore') for t in kws.split('\n')]163            write_names(d_names, self.champ, self.K)164            write_kws(c_kws, self.champ, self.K)165        self.communities_kws = c_kws166        return c_kws167168    def to_gexf(self):169        edges = set()170        champ = self.champ171        k = self.K172        gexf_file = gexf.Gexf('Graph similarity',champ + str(k))173        gph = gexf_file.addGraph('gr',"undirected", "static")174        intensAtt = gph.addNodeAttribute("intensity","0.0","float")175        clussAtt = gph.addNodeAttribute("cluster","100","int")176177        with open('../data/graph_{}_{}/user_names_info.txt'.format(champ, str(k)), 'r') as f:178            for line in f:179                line = line[:-1]180                line = line.split(',')181                n = gph.addNode(line[0], line[1])182                #, attributes={'intensity':line[2], 'cluster':line[3]})183                n.addAttribute(intensAtt, line[2])184                n.addAttribute(clussAtt, line[3])185186        with open('../data/graph_{}_{}/graph.txt'.format(champ,str(k)), 'r') as f:187            eid = 0188            for line in f:189                line = line[:-1]190                line = line.split(';')191                u1 = line[0]192                for u2, val in [el.split(',') for el in line [1:]]:193                    if (u1, u2) not in edges and (u2, u1) not in edges:194                        eid += 1195                        edges.add((u1, u2))196                        gph.addEdge(eid, u1, u2, weight=val)197198        file = open('./graph_{}_{}/graphe.gexf'.format(champ, str(k)), 'wb')199        gexf_file.write(file)200201    def get_users_info(self, done=False):202        if done:203            dic_names_info = read_user_info(self.champ, self.K)204        else:205            dic_names_freq = {}206            es_t = Elasticsearch([{'local': 'localhost'}])207            for k, v in self.vertices_frequency.items():208                try:209                    name = es_t.search(index='twitter', body={'query': {'match': {'userId': k}}})['hits']['hits'][0]['_source']['screenName'].encode('ascii', 'ignore')210                except:211                    name = '**noName**'212                dic_names_freq[k] = [name, v]213            dic_names_cluster = {}214            for k, v in self.vertices_communities.items():215                for u in v:216                    dic_names_cluster[u] = k217            dic_names_info = {}218            for k, v in dic_names_freq.items():219                dic_names_info[k] = v + [dic_names_cluster[k]]220        self.user_names_info = dic_names_info221        write_user_info(dic_names_info, self.champ, self.K)222        return dic_names_info223224    def print_cluster_info(self):225        for k in self.vertices_communities.keys():226            kws = ''227            for kw in self.communities_kws[k]:228                kws = kws + ',' + kw229            print(str(k) + ',' + str(len(self.vertices_communities[k])) + kws)230231    def get_desired_vertices(self, d_c):232        directory = '../data/graph_{}_{}/'.format(self.champ, self.K)233        with open(directory + 'desired_vertices.txt', 'w') as f:234            for k,v in self.vertices_communities.items():235                if k in d_c:236                    for uid in v:237                        f.write(uid + '\n')238239240241def get_vertices_lists(champ, k, biglist):242    users = set()243    directory = '../data/graph_{}_{}/'.format(champ,k)244    with open(directory + 'desired_vertices.txt', 'r') as f:245        for line in f:246            users.add(line[:-1])247    r = redis.StrictRedis(host='localhost')248    dic_users_lists = lists_of_users(users, r, biglist)249    with open(directory + 'vertices_lists.txt', 'w') as f:250        for k,v in dic_users_lists.items():251            f.write(k)252            for li in v:253                f.write(',' + li)254            f.write('\n')255256# Write file with frequencies of visit, screenames and clusters of users257def write_user_info(dico, champ, k):258    directory = '../data/graph_{}_{}/'.format(champ, k)259    with open(directory + 'user_names_info.txt', 'w') as f:260        for k, v in dico.items():261            f.write(k + ',' + v[0] + ',' + str(v[1]) + ',' + str(v[2]) + '\n')262263264# Reads info about users and stores it in dictionary of lists265def read_user_info(champ, k):266    dic_names_info = {}267    directory = '../data/graph_{}_{}/'.format(champ, k)268    try:269        with open(directory + 'user_names_info.txt', 'r') as f:270            for line in f:271                line = line[:-1]272                line = line.split(',')273                dic_names_info[line[0]] = [line[1], line[2], line[3]]274        return dic_names_info275    except:276        print('No such file user_names_info')277        return False278279280# Write names of lists of users by clusters281def write_names(dic, champ, k):282    directory = '../data/graph_{}_{}/'.format(champ, k)283    with open(directory + 'cluster_names.txt', 'w') as f:284        for k, v in dic.items():285            f.write(str(k))286            for name in v:287                f.write(',' + name)288            f.write('\n')289290291# Query elasticsearch to get listname292def get_list_name(es_con, listid):293    try:294        name = es_con.search(index='twitter', body={"query": {"match": {'listId':listid}}})['hits']['hits'][0]['_source']['listName'].encode('ascii', 'ignore')295    except:296        name = '***NoListName***'297    if name != '':298        if name[-1] == '\n':299            name = name[:-1]300    return name301302303# Write keywords of listnames by clusters304def write_kws(dic, champ, k):305    directory = '../data/graph_{}_{}/'.format(champ, k)306    with open(directory + 'clusters_kws.txt', 'w') as f:307        for k, v in dic.items():308            f.write(str(k))309            for kw in v:310                f.write(',' + kw[0])311            f.write('\n')312313314# Read file of keywords of clusters and store in dictionary315def read_kws(champ, k):316    c_kws = {}317    directory = '../data/graph_{}_{}/'.format(champ, k)318    try:319        with open(directory + 'clusters_kws.txt', 'r') as f:320            for line in f:321                line = line.split(',')322                kws = []323                for kw in line[1:]:324                    if kw[-1] == '\n':325                        kw = kw[:-1]326                    kws.append(kw)327                c_kws[int(line[0])] = kws328        return c_kws329    except:330        print('No such file')331        return False332333334# Writes file with clusters membership of users 335#        file with lists occuring in each cluster336def write_clusters(dic_cl, dic_l_cl, champ, k):337    directory = '../data/graph_{}_{}/'.format(champ, k)338    if not os.path.exists(directory):339        os.makedirs(directory)340    with open(directory + 'clusters.txt', 'w') as f:341        for k, v in dic_cl.items():342            f.write(str(k))343            for u in v:344                f.write(',' + u)345            f.write('\n')346    with open(directory + 'lists_clusters.txt', 'w') as f:347        for k, v in dic_l_cl.items():348            f.write(str(k))349            for u in v:350                f.write(',' + u)351            f.write('\n')352353354# Returns a set of tuples representing edges and a lists of corresponding weights355def edgelist(dic_edges):356    el = set()357    w = []358    for k, v in dic_edges.items():359        #print('0')360        for n, val in v.items():361            if tuple([k, n]) not in el and tuple([n, k]) not in el:362                el.add(tuple([k, n]))363                w.append(val)364    return el, w365366367# Write a file representing the graph and the frequency of visits of each vertice368def write_dict(dic, dic_intensity, champ, k):369    directory = '../data/graph_{}_{}/'.format(champ, k)370    if not os.path.exists(directory):371        os.makedirs(directory)372    with open(directory + 'graph.txt', 'w') as f:373        for key, val in dic.items():374            f.write(str(key))375            for k, v in val.items():376                f.write(';' + str(k) + ',' + str(v))377            f.write('\n')378    with open(directory + 'intensity.txt', 'w') as f:379        for key, val in dic_intensity.items():380            f.write(str(key) + ',' + str(val) + '\n')381382383# Returns aggregate dictionary containing the graphs obtained from each seed384def merge_graphs(dic_graphs, seeds):385    full_dic = dic_graphs[seeds[0]].copy()386    for _ in range(1, len(seeds)):387        for user, dic_neis in dic_graphs[seeds[_]].items():388            if user in full_dic.keys():389                full_dic[user] = dict(set(full_dic[user].items()) | set(dic_neis.items()))390            else:391                full_dic[user] = dic_neis392    return full_dic393394395# Aggregation of frequency of visits of vertices starting from the different seeds396def merge_intensity(dic_imp,seeds):397    full_dic = dic_imp[seeds[0]].copy()398    for _ in range(1,len(seeds)):399        for user, imp in dic_imp[seeds[_]].items():400            if user in full_dic.keys():401                full_dic[user] += imp402            else:403                full_dic[user] = imp404    return full_dic405        406407# Filters out lists containing more than 'max_members' users408def biglists(max_members):409    big_lists = set()410    with open('../lists_cardinality.csv','r') as f:411        next(f)412        for line in f:413            line = line.split(',')414            if int(line[2][:-1]) > max_members:415                big_lists.add(line[1][7:])416    return big_lists417418419# Return list ids containing given user 420def lists_of_user(user, r_con, biglists):421    print('Getting lists of seed.')422    lists = set(r_con.smembers(user))423    return {t.decode('utf-8') for t in lists if t.decode('utf-8') not in biglists}424425426# Return user ids belonging to a set of lists427def users_in_set_of_lists(lists, r_con, n):428    print('Getting users belonging to set of lists.')429    pipe = r_con.pipeline()430    if n == 1:431        for l in lists:432            pipe.smembers('listId:' + l)433    else:434        for l in lists:435            pipe.smembers('listId:' + l.decode('utf-8'))436    users = {j for i in pipe.execute() for j in i}437    return {t.decode('utf-8') for t in users}438439440# Return list ids containing each user of given set441def lists_of_users(users, r_con, biglists):442    print('Getting lists of set of users.')443    pipe = r_con.pipeline()444    list_users = []445    for u in users:446        list_users.append(u)447        pipe.smembers('userId:'+u)448    list_lists = pipe.execute()449    dic_corona_lists = {}450    for i,n in enumerate(list_users):451        dic_corona_lists[n] = set([t for t in list_lists[i] if t.decode('utf-8') not in biglists])452    return dic_corona_lists453454455# Forces the symmetry of a dictionary of edges456def sym_dict(dic):457    dico = {}458    for k,v in dic.items():459        for user,val in v.items():460            if k not in dico.keys():461                dico[k] = {}462            dico[k][user] = val463            if user not in dico.keys():464                dico[user] = {}465            dico[user][k] = val466    return dico            467468469# Computes intersections between sets of lists to get edges and valences470def intersection_pairs_users(dic_users,k):471    print('Calculating common lists between users.')472    dic_corona_links = {}473    for u, lu in dic_users.items():474        dic_nei_vals = {}475        for v, lv in dic_users.items():476            if v != u:477                inter = len(lu & lv)478                if inter >= k:479                    dic_nei_vals[v] = inter480        if dic_nei_vals != {}:481            dic_corona_links[u] = dic_nei_vals482    return sym_dict(dic_corona_links)483484485# Computes intersections between sets of lists to get edges and weights of the graph486def intersection_pairs_users_2(dic_users_1, dic_users_2, k):487    print('Calculating common lists between users.')488    dic_corona_links = {}489    for u, lu in dic_users_1.items():490        dic_nei_vals = {}491        for v, lv in dic_users_2.items():492            inter = len(lu & lv)493            if inter >= k:494                dic_nei_vals[v] = inter495        if dic_nei_vals != {}:496            dic_corona_links[u] = dic_nei_vals497    return sym_dict(dic_corona_links)498499500# Main function to get neighbours of a seed and connections between them501def get_corona(seed, blists, k):502    print('Corona I: ')503    START = time.clock()504    r = redis.StrictRedis(host='localhost')505    starting_lists = lists_of_user(seed, r, blists)506    #print('Seed is in {} lists'.format(len(starting_lists)))507    users_corona = users_in_set_of_lists(starting_lists, r, 1)508    #print('N users in corona and start : {}'.format(len(users_corona)))509    dic_corona_lists = lists_of_users(users_corona, r, blists)510    dic_corona_links = intersection_pairs_users(dic_corona_lists,k)511    print('Getting corona I took {} seconds\n'.format(time.clock()-START))512    try:513        del dic_corona_lists[seed[7:]]514    except:515        print("seed not in corona lists")516    print("test 1", len(dic_corona_links))517    print('Is seed in nodes of graph corona I : ',seed[7:] in dic_corona_links)518    return dic_corona_links, dic_corona_lists519520521# Main function to get neighbours of the neighbours of the seeds522def get_corona_2(lists_corona_1, blists, k):523    print('Corona II: ')524    START = time.clock()525    users_corona_1 = set(lists_corona_1.keys())526    starting_lists = set()527    for val in lists_corona_1.values():528        starting_lists = starting_lists | val529    r = redis.StrictRedis(host='localhost')530    users_corona = users_in_set_of_lists(starting_lists, r, 2)531    users_corona_2 = {t for t in users_corona if t not in users_corona_1}532    #print('Starting with {} users'.format(len(users_corona_2)))533    dic_corona_2 = lists_of_users(users_corona_2, r , blists)534    dic_corona_links_2 = intersection_pairs_users_2(lists_corona_1, dic_corona_2, k)535    print('Getting corona II took {} seconds'.format(time.clock()-START))536    print("test 2", len(dic_corona_links_2))  537    return dic_corona_links_2538539540# Create intermediate graph from one seed541def merge_dict(dic1, dic2, seed):542    nodes = set(dic1.keys()) | set(dic2.keys())543    print('Is seed in nodes of graph : ', seed[7:] in nodes)544    dic_nodes = {n:v for n,v in zip(range(len(nodes)),nodes)}545    dic_nodes_i = {n:v for n,v in zip(nodes,range(len(nodes)))}546    final_dict = {}547    for node in nodes:548        n_dict = {}549        if node in dic1.keys():550            for n,v in dic1[node].items():551                n_dict[dic_nodes_i[n]] = v552        if node in dic2.keys():553            for n,v in dic2[node].items():554                n_dict[dic_nodes_i[n]] = v555        final_dict[dic_nodes_i[node]] = n_dict556    return final_dict, dic_nodes, dic_nodes_i557558559# Return np matrix representing the graph560def build_matrix(dic, sz):561    matrix = np.zeros((sz, sz))562    for k, v in dic.items():563        for k2, val in v.items():564            matrix[int(k), int(k2)] = val565    return matrix566567568# Estimate the frequency of visits of each vertice with a rw starting from the seed569def get_paths(matrix, seed_ind, dic_users):570    paths2 = np.dot(matrix, matrix[seed_ind,:].T)571    v = np.ones(paths2.size)572    paths = np.dot(matrix, v) + paths2573    paths = (paths - np.mean(paths)) / np.std(paths)574    dic = {}575    for i, p in np.ndenumerate(paths):576        if p > 0:577            dic[dic_users[i[0]]] = p578    return paths, dic579580581# Eliminate vertices visited by the rw less than average582def reduced_graph(selected_users, dic, dic_users):583    red_graph = {}584    for k, v in dic.items():585        if dic_users[k] in selected_users:586            dico = {}587            for k2, val in v.items():588                if dic_users[k2] in selected_users:589                    dico[dic_users[k2]] = val590            red_graph[dic_users[k]] = dico591    return red_graph592593594# Read file representing the grph and the importance of nodes595def read_graph_freq(champ, k):596    graph = {}597    intensities = {}598    directory = '../data/graph_{}_{}/'.format(champ, k)599    try:600        with open(directory + 'graph.txt', 'r') as f:601            for line in f:602                line = line[:-1]603                line = line.split(';')604                dic = {}605                for pair in line[1:]:606                    n, val = pair.split(',')607                    dic[n] = int(val)608                graph[line[0]] = dic609        with open(directory + 'intensity.txt', 'r') as f:610            for line in f:611                v, imp = line.split(',')612                if imp[-1] == '\n':613                    imp = imp[:-1]614                intensities[v] = float(imp)615    except:616        print('No such file')617        return False618    return graph, intensities619620621def read_graph_uinfo(champ, k):622    graph = {}623    user_info = {}624    directory = '../data/graph_{}_{}/'.format(champ, k)625    try:626        with open(directory + 'graph.txt', 'r') as f:627            for line in f:628                line = line[:-1]629                line = line.split(';')630                dic = {}631                for pair in line[1:]:632                    n, val = pair.split(',')633                    dic[n] = int(val)634                graph[line[0]] = dic635        with open(directory + 'user_names_info.txt', 'r') as f:636            for line in f:637                line = line[:-1]638                v, name, imp, clu = line.split(',')639                user_info[v] =[name, float(imp)]640    except:641        print('No such file')642        return False643    return graph, user_info644645646def read_desired_users_and_lists(champ, k):647    desired_users = set()648    users_lists = {}649    directory = '../data/graph_{}_{}/'.format(champ, k)650    with open(directory + 'desired_vertices.txt', 'r') as f:651        for line in f:652            desired_users.add(line[:-1])653    with open(directory + 'vertices_lists.txt', 'r') as f:654        for line in f:655            line = line[:-1].split(',')656            users_lists[line[0]] = line[1:]657    return desired_users, users_lists658659660661662# Read files reprensenting node membership to clusters and lists of each cluster663def read_com(champ, k):664    clusters = {}665    lists_on_clusters = {}666    directory = '../data/graph_{}_{}/'.format(champ, k)667    try:668        with open(directory + 'clusters.txt', 'r') as f:669            for line in f:670                line = line[:-1]671                line = line.split(',')672                users = set()673                for user in line[1:]:674                    users.add(user)675                clusters[int(line[0])] = users676        with open(directory + 'lists_clusters.txt', 'r') as f:677            for line in f:678                line = line[:-1]679                line = line.split(',')680                lists = set()681                for l in line[1:]:682                    lists.add(l)683                lists_on_clusters[int(line[0])] = lists684    except:685        print('No such file')686        return False687    return clusters, lists_on_clusters688689690# Return direct neighborhood691def getReducedNeighbor(user, blids, k):692    START = time.clock()693    neighbor_dict = {}694    r = redis.StrictRedis(host='localhost')695    lists = [t for t in r.smembers(user) if t.decode('utf-8') not in blids]696    if len(lists) == 0:697        print('User {} is isolated.'.format(user))698        return neighbor_dict699    else:700        print('User {} is in {} lists.'.format(user, len(lists)))701        pipe = r.pipeline()702        for l in lists:703            listId = 'listId:' + l.decode('utf-8')704            pipe.smembers(listId)705        neighs = list(set([j for i in pipe.execute() for j in i]))706        pipe = r.pipeline()707        for n in neighs:708            ne = 'userId:' + n.decode('utf-8')709            pipe.sinter(user,ne)710        valences = pipe.execute()711        valences = [len([x for x in t if x not in blids]) for t in valences]712        print(valences[0])713        for u,val in zip(neighs, valences):714            if val > k:715                us = 'userId:' + u.decode('utf-8')716                neighbor_dict[us] = val717        print('Finding neighbors of {} took {} seconds.\n'.format(user,time.clock() - START))718        return neighbor_dict719720721# Alt grow gaph722def getGraph(seed, blists, k):723    dict_of_dicts = {}724    dict_of_dicts[seed] = getReducedNeighbor(seed, blists, k)725    keys = dict_of_dicts[seed].keys()726    print('Seed has {} neighbors.'.format(len(keys)))727    for el in keys:728        dict_of_dicts[el] = getReducedNeighbor(el, blists, k)729        print('User {} has {} neighbors.'.format(el,len(dict_of_dicts[el].keys())))730    dict_of_dicts = sym_dict(dict_of_dicts)731    dic_nodes = {n:v for n,v in zip(range(len(keys)+1),[seed]+list(keys))}732    dic_nodes_i = {n:v for n,v in zip([seed]+list(keys),range(len(keys)+1))}733    final_dict = {}734    for node,neis in dict_of_dicts.items():735        n_dict = {}736        for k,v in neis.items():737            n_dict[dic_nodes_i[k]] = v738        final_dict[dic_nodes_i[node]] = n_dict739    return final_dict, dic_nodes, dic_nodes_i740741742def construct_reduced_graph(c_p, uinfo, desired_users):743    l_edges, weights = edgelist(c_p)744    l_vertices = list(desired_users)745    l_vertices = sorted([int(t) for t in l_vertices])746    l_vertices = [str(t) for t in l_vertices]747    net = Graph(directed = False)748    net.add_vertices(l_vertices)749    w = []750    edges = []751    for i,edge in enumerate(l_edges):752        if edge[0] in desired_users and edge[1] in desired_users:753            #net.add_edge(edge)754            w.append(weights[i])755            edges.append(edge)756    net.add_edges(edges)757    net.es['weight'] = w758    p = net.community_multilevel(weights = w)759    user_full_info = {}760    for i,cl in enumerate(p):761        for v in cl:762            v = net.vs[v]['name']763            user_full_info[v] = uinfo[v] + [i]764    return edges, w, user_full_info765#def cut_graph():766767768def get_keywords(uinfo, ulists, champ, k):769    directory = '../data/graph_{}_{}/'.format(champ,k)770    es_t = Elasticsearch([{'local':'localhost'}])771    clu_lists = {}772    for k,v in ulists.items():773        clu = uinfo[k][2]774        if clu not in clu_lists.keys():775            clu_lists[clu] = set()776        else:777            clu_lists[clu] = clu_lists[clu] | set(v)778    c_kws = {}779    for k,v in clu_lists.items():780        names = set()781        for l in v:782            nm = get_list_name(es_t, l)783            names.add(nm)784        txt = ' '.join(list(names))785        wds = min(5, len(names))786        ws = txt.split(' ')787        unwanted_words = ['/','et','&', '-', '***NoListName***', 'de', ',', '_', '.', 'the', 'of', 'les', 'le', 'la', 'and', 'list','my']788        ws = [x.strip() for x in ws if len(x)>0]789        ws = [x.lower() for x in ws]790        ws = [x for x in ws if x not in unwanted_words]791        kws = Counter(ws).most_common(wds)792        print(kws)793        print('\n')794        c_kws[k] = kws795    with open(directory + 'reduced_clusters_kws.txt', 'w') as f:796        for k,v in c_kws.items():797            f.write(str(k))798            for pair in v:799                f.write(',{},{}'.format(pair[0],pair[1]))800            f.write('\n')801    return c_kws 802803804805def write_gexf(r_g, w, uinfo, champ, k):806    edges = set()807    file = open('../data/graph_{}_{}/graphe_reduit.gexf'.format(champ, k), 'wb')808    gexf_file = gexf.Gexf('Reduced Graph similarity',champ + str(k))809    gph = gexf_file.addGraph("undirected", "static", 'gr')810    intensAtt = gph.addNodeAttribute("intensity","0.0","float")811    clusAtt = gph.addNodeAttribute("cluster","100","int")812    for k,v in uinfo.items():813        n = gph.addNode(k,v[0])814        n.addAttribute(intensAtt, str(v[1]))815        n.addAttribute(clusAtt, str(v[2]))816    eid = 0817    for (k,v) in r_g:818        if (k,v) not in edges and (v,k) not in edges:819            edges.add((k,v))820            gph.addEdge(eid, k, v, weight=w[eid])821        eid += 1
...Password_UI.py
Source:Password_UI.py  
...85        self.upperEntry.current(0)86        self.lowerEntry.current(0)87        self.digitsEntry.current(0)88        self.dropMenu.current(1)89    def starting_lists(self, event):90        """91        Creates a list of the range of the users desired password length. And, updates the value of comboboxs92        initialized to the list created. Function also creates a combobox to represent the desire of special characters,93        places comboboxs in appropriate grib position, and binds the selection of a value to update other lists.94        :param event: None [Event is required in order to be assigned to keystroke]95        :return: None96        """97        # Setting initialized variable user_num to desired user length of password98        self.user_num.set(self.lengthEntry.get())99        # Creating lists for combobox user options100        values_list = [num for num in range(0, int(self.user_num.get()) + 1)]101        options = ['Yes', 'No']102        # Updating values of initialized comboboxes103        self.upperEntry = ttk.Combobox(self, values=values_list, state='readonly', justify='center')...test_conjecture_int_list.py
Source:test_conjecture_int_list.py  
...31    result.sort()32    return slice(*result)33class IntListRules(RuleBasedStateMachine):34    @initialize(ls=st.lists(INTEGERS))35    def starting_lists(self, ls):36        self.model = list(ls)37        self.target = IntList(ls)38    @invariant()39    def lists_are_equivalent(self):40        if hasattr(self, "model"):41            assert isinstance(self.model, list)42            assert isinstance(self.target, IntList)43            assert len(self.model) == len(self.target)44            assert list(self.target) == self.model45    @rule(n=INTEGERS)46    def append(self, n):47        self.model.append(n)48        self.target.append(n)49    @rule(i=valid_index() | valid_slice())...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
