def do_npath(self, line): ''' finds no-weighted path between two artists''' start_name, end_name = line.split(',') aid_start = self.asearch(start_name) aid_end = self.asearch(end_name) if aid_start and aid_end: path = db.artist_path(aid_start, aid_end, weighted=False, skipset=self.skipset) if path: self.print_path(path) else: print "no path between", db.artist_name(aid_start), db.artist_name(aid_end)
def path(self, src=None, dest=None, src_id=None, dest_id=None, skips=None, save='True', _=None): log_api_call('path') results = get_results() if src_id: a1 = aget(src_id) src = '' else: a1 = asearch(src) save = save.lower() == 'true' if not a1: return seal_results(results, 'ERROR', "can't find " + src) if dest_id: a2 = aget(dest_id) dest = '' else: a2 = asearch(dest) if not a2: return seal_results(results, 'ERROR', "can't find " + dest) skipset = None if skips: try: skipset = set([sid for sid in skips.split(',')]) results['skips'] = db.artist_get(list(skipset)) except ValueError: return seal_results(results, 'ERROR', "bad skiplist " + skips) else: results['skips'] = [] results['src'] = a1 results['dest'] = a2 path = db.artist_path(a1['id'], a2['id'], skipset=skipset, save=save) results['path'] = path gc.collect(0) self.paths += 1 if self.paths % 100 == 0: gc.collect() util.summary_memory_usage() util.gc_info() print return seal_results(results)
def do_random_path(self, line): ''' picks two artists at random and finds a path between them''' count = 1 if len(line) > 0: count = int(line) paths = 0 no_path = 0 sum_time = 0 sum_length = 0 max_length = 0 max_time = 0 for i in xrange(count): aid1 = db.artist_random() aid2 = db.artist_random() print "random path between", aid1, db.artist_name(aid1), 'and', aid2, db.artist_name(aid2) start = time.time() path = db.artist_path(aid1, aid2, skipset=self.skipset) delta = time.time() - start if path: self.print_path(path, 0) paths += 1.0 lpath = len(path['links']) sum_length += lpath sum_time += delta if lpath > max_length: max_length = lpath if delta > max_time: max_time = delta else: print "no path between", aid1, db.artist_name(aid1), aid2, db.artist_name(aid2) no_path += 1 print print 'paths:', paths print 'no paths:', no_path if paths > 0: avg_time = sum_time / paths avg_length = sum_length / paths print 'avg length:', avg_length print 'avg time:', avg_time print 'max length:', max_length print 'max time:', max_time