class SymbolMap(object): def __init__(self, min_v): self._list = SortedCollection((), lambda x: x[0]) self._min_vaddr = min_v def add_symbol(self, start, length, name): tuple = (start, length, name) self._list.insert(tuple) def find(self, addr): try: tuple = self._list.find_le(addr) if addr < tuple[0] + tuple[1]: return tuple[2] return None except ValueError: return None def copy(self): ret = SymbolMap() ret._list = self._list.copy() return ret def __str__(self): return "SymbolMap: " + self._list.__str__() def __repr__(self): return self.__str__()
class SymbolMap(object): def __init__(self, min_v): self._list = SortedCollection((), lambda x : x[0]) self._min_vaddr = min_v def add_symbol(self, start, length, name): tuple = (start, length, name) self._list.insert(tuple) def find(self, addr): try: tuple = self._list.find_le(addr) if addr < tuple[0] + tuple[1]: return tuple[2] return None except ValueError: return None def copy(self): ret = SymbolMap() ret._list = self._list.copy() return ret def __str__(self): return "SymbolMap: " + self._list.__str__() def __repr__(self): return self.__str__()
class MmapState(object): def __init__(self): self._list = SortedCollection((), lambda x : x[0]) def add_map(self, start, length, pgoff, name): tuple = (start, length, pgoff, name) self._list.insert(tuple) def find(self, addr): try: tuple = self._list.find_le(addr) if addr < tuple[0] + tuple[1]: return tuple return None except ValueError: return None def copy(self): ret = MmapState() ret._list = self._list.copy() return ret def __str__(self): return "MmapState: " + self._list.__str__() def __repr__(self): return self.__str__()
class ElvesReady: def __init__(self): self.training_elf_list = SortedCollection(key=attrgetter('rating')) self.high_performance_elf_list = [] def get_elf_with_best_fit_rating(self, rating): return self.training_elf_list.find_ge(rating) def add_elf(self, elf): if elf.rating > 3.95: self.high_performance_elf_list.append(elf) else: self.training_elf_list.insert(elf) def add_elves(self, elves): for elf in elves: self.add_elf(elf) def remove_from_training_list(self, elf): self.training_elf_list.remove(elf) def remove_from_high_performance_list(self, elf): self.high_performance_elf_list.remove(elf) #todo: remove elves that are chosen.
class MmapState(object): def __init__(self): self._list = SortedCollection((), lambda x: x[0]) def add_map(self, start, length, pgoff, name): map_tuple = (start, length, pgoff, name) self._list.insert(map_tuple) def find(self, addr): try: map_tuple = self._list.find_le(addr) if addr < map_tuple[0] + map_tuple[1]: return map_tuple return None except ValueError: return None def copy(self): ret = MmapState() ret._list = self._list.copy() return ret def __str__(self): return 'MmapState: ' + self._list.__str__() def __repr__(self): return self.__str__()
def reconstruct_for_user_perfect_predictions(user): ordered_visits = get_history_ordered_visits_for_user(user) ordered_visits = exclude_bad_visits(ordered_visits) tab_focus_times = get_tab_focus_times_for_user(user) tab_focus_times_sortedcollection = SortedCollection( tab_focus_times, key=itemgetter('start')) output = [] ordered_visits_len = len(ordered_visits) for idx, visit in enumerate(ordered_visits): if idx + 1 == ordered_visits_len: # last visit, TODO needs to be reconstructed continue next_visit = ordered_visits[idx + 1] visit_time = visit['visitTime'] next_visit_time = next_visit['visitTime'] url = visit['url'] next_url = next_visit['url'] time_difference = next_visit_time - visit_time if time_difference <= 0: continue fraction_active = fraction_active_between_times( tab_focus_times_sortedcollection, visit_time, next_visit_time) end_time = min(visit_time + 60 * 1000.0, next_visit_time) if fraction_active > 0.5: end_time = next_visit_time output.append({ 'url': url, 'start': visit_time, 'active': visit_time, 'end': end_time }) output = merge_contiguous_spans(output) return output
def find_overlapping_bins(arr1, arr2): ''' Function to calculate overlaps for every bin in array 1 against every bin in array 2. Accepts two arrays of tuples as inputs. Where each tuple is the start and end site of a bin within that array. Returns a list of Overlap objects for each bin in arr1. Each overlap object contains the indices of all the bins in arr2 that a given arr1 bin overlaps with along with the amount of overlap represented as tuples. ''' arr2_sc = SortedCollection(arr2, key=(lambda x: x[0])) overlapping_bins = [] for bin in arr1: overlaps = Overlap(bin) bin_beg, bin_end = bin # Find the closest bin in array2 try: closest_bin = arr2_sc.find_le(bin_beg) indx = arr2.index(closest_bin) except ValueError: closest_bin = arr2[0] indx = 0 overlap_amt = get_overlap_amount(bin, closest_bin) if overlap_amt > 0: overlaps.add_bin(indx, overlap_amt) # Check bins after the closest for overlaps flag = True while flag: indx += 1 try: next_bin = arr2[indx] overlap_amt = get_overlap_amount(bin, next_bin) if overlap_amt > 0: overlaps.add_bin(indx, overlap_amt) else: flag = False except IndexError: flag = False overlapping_bins.append(overlaps) return overlapping_bins
def extract_tofill_dataset_from_user(user): training_samples = [] training_labels = [] training_weights = [] from_domains = [] to_domains = [] #ordered_visits = get_history_ordered_visits_for_user(user) #ordered_visits = exclude_bad_visits(ordered_visits) #ordered_visits = get_idealized_history_from_logs_for_user(user) ordered_visits = get_idealized_history_from_logs_urlchanged_for_user(user) ordered_visits_len = len(ordered_visits) tab_focus_times = get_tab_focus_times_for_user(user) ref_start_time = max(get_earliest_start_time(tab_focus_times), get_earliest_start_time(ordered_visits)) ref_end_time = min(get_last_end_time(tab_focus_times), get_last_end_time(ordered_visits)) ref_start_time = max( ref_start_time, 1458371950000) # march 19th. may have had some data loss prior to that ref_end_time = max(ref_end_time, 1458371950000) tab_focus_times_sortedcollection = SortedCollection( tab_focus_times, key=itemgetter('start')) for idx, visit in enumerate(ordered_visits): if idx + 1 == ordered_visits_len: # last visit, we probably should reconstruct this TODO continue next_visit = ordered_visits[idx + 1] visit_time = visit['visitTime'] next_visit_time = next_visit['visitTime'] if visit_time < ref_start_time: continue if next_visit_time > ref_end_time: continue if visit_time >= next_visit_time: continue fraction_active = fraction_active_between_times( tab_focus_times_sortedcollection, visit_time, next_visit_time) label = int(fraction_active > 0.5) visit_gap = log(next_visit_time - visit_time) weight = next_visit_time - visit_time training_samples.append([visit_gap]) training_labels.append(label) training_weights.append(weight) # should we try weight or log(weight) from_domain = url_to_domain(visit['url']) to_domain = url_to_domain(next_visit['url']) from_domains.append(from_domain) to_domains.append(to_domain) return { 'samples': training_samples, 'labels': training_labels, 'weights': training_weights, 'fromdomains': from_domains, 'todomains': to_domains, }
def compute_path(self, orig, goal): orig = map(int, orig) goal = tuple(goal) queue = SortedCollection(key=itemgetter(0)) queue.insert((0, orig, 0)) pred = {} pred[tuple(orig)] = None while len(queue) > 0: score, (px, py), steps = queue[0] queue.remove_first() if (px, py) == goal: # follow trail back greedily pos = (px, py) plan = [pos] while pred[pos]: plan.append(pred[pos]) pos = pred[pos] return plan[::-1][1:] for dx, dy in CognitiveMonkey.Directions: nx, ny = px + dx, py + dy if (nx, ny) in pred: continue if nx < 0 or ny < 0 or nx >= self.world.height or ny >= self.world.width: continue if self.explore_map[nx][ny] == CognitiveMonkey.Obstacle: continue pred[(nx, ny)] = (px, py) nscore = steps + 1 + Collisions.distance((nx, ny), goal) queue.insert((nscore, (nx, ny), steps + 1)) return []
def __init__(self, min_v): self._list = SortedCollection((), lambda x : x[0]) self._min_vaddr = min_v
def __init__(self): self._list = SortedCollection((), lambda x : x[0])
def __init__(self): self.training_elf_list = SortedCollection(key=attrgetter('rating')) self.high_performance_elf_list = []
def __init__(self, min_v): self._list = SortedCollection((), lambda x: x[0]) self._min_vaddr = min_v
def __init__(self): self._list = SortedCollection((), lambda x: x[0])
#Stats sts = self.get_stats() s += 'Estadísticas:\n' s += 'PS: {0}\n'.format(sts[0]) s += 'Ataque: {0}\n'.format(sts[1]) s += 'Defensa: {0}\n'.format(sts[2]) s += 'Ataque especial: {0}\n'.format(sts[3]) s += 'Defensa especial: {0}\n'.format(sts[4]) s += 'Velocidad: {0}\n'.format(sts[5]) return s pokemon_list = list() pokemon_sorted_id = SortedCollection(key=lambda poke: poke.id) pokemon_sorted_name = SortedCollection(key=lambda poke: poke.name) type_list = list() type_sorted_name = SortedCollection(key=lambda ptype: ptype.name) ability_list = list() ability_sorted_name = SortedCollection(key=lambda ability: ability.name) move_list = list() move_sorted_name = SortedCollection(key=lambda move: move.name) def insert_pokemon(pokemon): pokemon_list.append(pokemon)