def _direct_left_corner(elem: iAntlr4GramElem, dlc: SortedSet, allow_eps_in_sel=False): """ Collect all possible symbols which can appear on the beggining """ if isinstance(elem, Antlr4Symbol): dlc.add(elem.symbol) return False elif isinstance(elem, Antlr4Iteration): _direct_left_corner( elem.body, dlc, allow_eps_in_sel=allow_eps_in_sel) return not elem.positive elif isinstance(elem, Antlr4Sequence): for e in elem: if not _direct_left_corner( e, dlc, allow_eps_in_sel=allow_eps_in_sel): break return True elif isinstance(elem, Antlr4Selection): for e in elem: can_be_eps = _direct_left_corner( e, dlc, allow_eps_in_sel=allow_eps_in_sel) if not allow_eps_in_sel: assert not can_be_eps, elem elif isinstance(elem, Antlr4Option): _direct_left_corner( elem.body, dlc, allow_eps_in_sel=allow_eps_in_sel) return True elif isinstance(elem, (Antlr4Indent, Antlr4Newline)): return True else: raise TypeError(elem)
def leaf_iter(self, node=None, node_list=None): """ returns an iterator over the leafs which cover a node (or nodes) Args: node (int): node in self node_list (list): nodes in self Yields: leaf (int): node in self which is a descendant leaf of node (or some node in node_list) """ assert (node is None) != (node_list is None), 'node xor node_list' assert node in self.nodes, 'node not found' if node_list is None: node_list = [node] node_list = SortedSet(node_list) while node_list: # get largest node node = node_list.pop() neighbor_list = list(self.neighbors(node)) if not neighbor_list: # n is a leaf yield node else: node_list |= set(neighbor_list)
def __init__(self, probability_gain_feature_dependency=0.5, probability_lose_feature_dependency=0.25, probability_gain_system_dependency=0.1, probability_lose_system_dependency=0.25, probability_new_bug=0.5, probability_debug_known=0.9, probability_debug_unknown=0.01, probability_failure_on_demand=0.01, test_effectiveness=0.5, test_efficiency=0.5 ): self.probability_gain_feature_dependency = probability_gain_feature_dependency self.probability_lose_feature_dependency = probability_lose_feature_dependency self.probability_gain_system_dependency = probability_gain_system_dependency self.probability_lose_system_dependency = probability_lose_system_dependency self.probability_new_bug = probability_new_bug self.probability_debug_known = probability_debug_known self.probability_debug_unknown = probability_debug_unknown self.probability_failure_on_demand = probability_failure_on_demand self.test_effectiveness = test_effectiveness self.test_efficiency = test_efficiency self.features = SortedSet(key=lambda f: f.logical_name) self.successful_operations = list()
def __init__(self, master: typing.Optional[object], versions: SortedSet, persist: typing.Optional[bool] = False, keep: typing.Optional[bool] = False, last_used: typing.Optional[str] = None, *args, **kwargs): # assign tkinter-compatible interface items to placeholder to placate PyCharm _ = master _ = args _ = kwargs self._can_continue = None self._child_names = SortedSet(versions) self._child_objects = None self._chosen_version = None self._do_not_ask_again = False self._keep_while_available = keep self._last_used = last_used self._return_code = None self._persist = persist self._index = 0 if persist: self.persist_action() if keep: self.keep_action() if last_used is not None: last_used_version_object = LooseVersion(last_used) self._index = self._child_names.index(last_used_version_object) \ if last_used_version_object in self._child_names else 0 self.select_action()
def get_distinct_values(dataset, attributes, votes_attributes, users_attributes, position_attribute, user_variation_scope=None): vote_id_attributes = votes_attributes[0] users_id_attributes = users_attributes[0] range_nb_attributes = range(len(attributes)) votes_map_details = {} users_map_details = {} votes_map_meps = {} users_map_votes = {} #users_map_votes_agreement_index={} users_map_details_has_key = users_map_details.has_key votes_map_details_has_key = votes_map_details.has_key arr_distinct_values = [SortedSet() for i in range_nb_attributes] for d in dataset: for i, attr in enumerate(attributes): obj_attr_value = d[attr['name']] values = set() if hasattr(obj_attr_value, '__iter__'): values = {v for v in obj_attr_value} else: values = {obj_attr_value} if (attr['name'] not in users_attributes) or ( (attr['name'] in users_attributes) and (d[users_id_attributes] in user_variation_scope)): arr_distinct_values[i] |= values d_vote_id = d[vote_id_attributes] d_user_id = d[users_id_attributes] if (not users_map_details_has_key(d_user_id)): users_map_details[d_user_id] = { key: d[key] for key in users_attributes } users_map_votes[d_user_id] = set([]) #users_map_votes_agreement_index[d_user_id]=[] users_map_votes[d_user_id] |= {d_vote_id} ###########AGREEMENT_INDEX##################### #users_map_votes_agreement_index[d_user_id] += [(d_vote_id,d['AgreementIndex'])] ###########AGREEMENT_INDEX##################### if (not votes_map_details_has_key(d_vote_id)): votes_map_details[d_vote_id] = { key: d[key] for key in votes_attributes } votes_map_meps[d_vote_id] = SortedSet() votes_map_meps[d_vote_id] |= {d_user_id} return votes_map_details, votes_map_meps, users_map_details, users_map_votes, arr_distinct_values
class VoteDatabase(object): def __init__(self): self.batches = SortedSet(key=lambda b: b.id) pass def record(self, vote_batch): self.batches.add(vote_batch) @property def votes(self): return reduce(lambda a, b: a.votes.union(b.votes), self.batches, set())
def _bugs(self): covered_bugs = reduce(lambda a, b: a.union(b), map(lambda c: frozenset(c.bugs), self.chunks), set()) result = SortedSet(key=lambda b: b.fully_qualified_name) for bug in SortedSet(covered_bugs, key=lambda b: b.fully_qualified_name): rand = Random() bug_test_hash = hash((self.fully_qualified_name, bug.fully_qualified_name)) rand.seed(bug_test_hash) p = rand.random() if p <= self.effectiveness: result.add(bug) return result
def __init__(self): self.data = {} self.city_indexed_data = {} self.star_ranking_indexed_data = {} self.hotel_name_indexed_data = {} self.search_terms = SortedSet() self.indexed_search_terms = {}
def custom_prop_names(self): """Read-only property containing a SortedSet-like sequence of custom property names attached to this node. Default implementation returns an empty SortedSet. Subclasses should override this. The returned object may or may not automatically update when new custom properties are added. The caller should generally avoid modifying the returned object directly. """ return SortedSet()
def index_search_terms(word, hotel_details): ''' :param word: :param hotel_details: ''' try: if DataStore().indexed_search_terms.get(word) is not None: DataStore().indexed_search_terms[word.lower()].append(hotel_details.hotel_id) else: hotel_names = SortedSet() hotel_names.add(word.lower()) word_len = len(word) DataStore().indexed_search_terms[word.lower()[0:(word_len if word_len <= 1 else 2)]] = word except Exception, ex: logger.exception(ex) raise ex
class EffectSet(_EffectSetImpl): def __init__(self, *, elms=None): self._data = SortedSet( elms, key=operator.attrgetter('typ', 'cost') ) @property def data(self): return self._data.copy()
def chunk_indexes(self): """ The indexes to chunks in the sorted set of chunks of this tests's parent feature that this tests touches. """ shuffled_indexes = range(0, self.feature.size) shuffler = Random(self.logical_name) shuffler.shuffle(shuffled_indexes) result = SortedSet() rand = Random(self.logical_name) for chunk_index in shuffled_indexes: p = rand.random() if p <= self.efficiency**(len(result)): result.add(chunk_index) return result
def _bugs(self): covered_bugs = reduce(lambda a, b: a.union(b), map(lambda c: frozenset(c.bugs), self.chunks), set()) result = SortedSet(key=lambda b: b.fully_qualified_name) for bug in SortedSet(covered_bugs, key=lambda b: b.fully_qualified_name): rand = Random() bug_test_hash = hash( (self.fully_qualified_name, bug.fully_qualified_name)) rand.seed(bug_test_hash) p = rand.random() if p <= self.effectiveness: result.add(bug) return result
def build_mask(self, node_list): # init empty mask assert self.ref.shape is not None, 'ref must have shape' mask = Mask(np.zeros(self.ref.shape), ref=self.ref).astype(int) # sort nodes from biggest (value + space) to smallest node_set = SortedSet(node_list) while node_set: node = node_set.pop() # remove all the nodes which would be covered by node node_set -= set(nx.descendants(self, node)) # record position of node for ijk in self.get_pc(node=node): mask[ijk] = node return mask
def chunk_indexes(self): """ The indexes to chunks in the sorted set of chunks of this tests's parent feature that this tests touches. """ shuffled_indexes = range(0, self.feature.size) shuffler = Random(self.logical_name) shuffler.shuffle(shuffled_indexes) result = SortedSet() rand = Random(self.logical_name) for chunk_index in shuffled_indexes: p = rand.random() if p <= self.efficiency ** (len(result)): result.add(chunk_index) return result
def __init__(self, position, environment): self.state = State(position, position) self.environment = environment self.heuristic = manhattan_distance self.frontier = SortedSet(key = self.frontier_order) self.explored = set() self.reset()
def get_terminals(rules): terminals = SortedSet() def collect_terminals(obj: iAntlr4GramElem): if isinstance(obj, Antlr4Symbol) and obj.is_terminal: terminals.add(obj.symbol) for r in rules: r.walk(collect_terminals) return terminals
def __new__(cls, *args, **kwargs): """Initialise Channel Object.""" blank_chan_dict = { 'sid': None, 'c': None, 't': None, 'xmltv_id': None, } # pylint: disable=attribute-defined-outside-init new_obj = super(Channel, cls).__new__(cls, *args, **kwargs) new_obj._chan_dict: Dict[str, Any] = blank_chan_dict new_obj._sources: CSRC = CSRC.no_source new_obj.programmes: SortedSet = SortedSet() return new_obj
def _set_aux_resources(self): """ @todo: Check how to improve """ # Generate an aux structure to speedup the allocation process resource_types = self.resource_manager.resource_types self.aux_resources = {} for res_type in resource_types: if not (res_type in self.aux_resources): self.aux_resources[res_type] = {} for node in self.sorted_keys: n_res = self.avl_resources[node][res_type] if n_res == 0: # This works similar to trim_nodes continue if not (n_res in self.aux_resources[res_type]): self.aux_resources[res_type][n_res] = SortedSet() self.aux_resources[res_type][n_res].add(node)
def _find_sat_nodes(self, req_resources): sat_nodes = {} # fitting_nodes = {} for t_res, n_res in req_resources.items(): if n_res == 0: continue if not (t_res in sat_nodes): sat_nodes[t_res] = SortedSet(key=self._natural_keys) for n, nodes in self.aux_resources[t_res].items(): if n >= n_res: sat_nodes[t_res].update(nodes) #=========================================================== # for node in nodes: # if not (node in fitting_nodes): # fitting_nodes[node] = {} # fitting_nodes[node][t_res] = n // n_res #=========================================================== # nodes = list(reduce(set.intersection, (set(val) for val in sat_nodes.values()))) # tot_fitting_reqs = sum([min(fitting_nodes[n].values()) for n in nodes]) nodes = reduce(SortedSet.intersection, sat_nodes.values()) return nodes # , tot_fitting_reqs
def channel_from_json(json_) -> Channel: """Create a channel from JSON data. Args: str: A string of JSON data. Returns: Channel: A channel object. """ chan = Channel.__new__(Channel) data = json.loads(json_, object_hook=skyq_json_decoder_hook) if not data.get('__type__') == '__channel__': raise ValueError('Incorrect type metadata in JSON payload.') chan._chan_dict = data['attributes'] chan._sources = data['sources'] chan.programmes = SortedSet() for prog in data['programmes']: chan.programmes.add( Programme(channel_xml_id=data['attributes']['xmltv_id'], **prog['attributes'])) return chan
def direct_left_corner(rule, allow_eps_in_sel=False): """ direct left corner: The Symbol X is a direct left corner of a nonterminal A, if there is an A-production with X as the left-most symbol on the right-hand side. We define the left-corner relation to be the reflexive transitive closure of the direct-left-corner relation and we define theproper-left-corner relation to be the transitive closure ofthe direct-left-corner relation. (A nonterminal is left re-cursive if it is a proper left corner of itself.) (A nonterminal is directly left recursive if it is a direct left corner of itself and a nonterminal is indirectly left recursiveif it is left recursive, but not directly left recursive.) :note: | in rules is not taken in account """ dlc = SortedSet() can_be_eps = _direct_left_corner( rule.body, dlc, allow_eps_in_sel=allow_eps_in_sel) if not allow_eps_in_sel: assert not can_be_eps, rule return dlc
def simple_visual(data_set): events = prepare_events(data_set) state = SortedSet() # (point, event, [skey]) all_lines = LinesCollection(data_set, color='gray') yield Scene([], [all_lines]) def get_neighbours(segment): index = state.index(segment) return ( state[index - 1].segment if index > 0 else None, #above state[index + 1].segment if index < len(state) - 1 else None ) #bellow def check_if_intersection_exists(segment, neighbour, orientation): if neighbour and segment: point = get_intersection_point(segment, neighbour, orientation) return point def check_intersections(state, skey): above, below = get_neighbours(skey) return check_if_intersection_exists(skey.segment, above, BELOW) or \ check_if_intersection_exists(skey.segment, below, ABOVE) for point, event, [skey] in iter_events(events): if event == START: state.add(skey) ipoint = check_intersections(state, skey) elif event == END: above, below = get_neighbours(skey) state.remove(skey) ipoint = check_if_intersection_exists(above, below, ABOVE) yield Scene([ PointsCollection([point], color='red'), ], [ all_lines, LinesCollection([x.segment for x in state], color='blue') ]) if ipoint: break yield Scene([ PointsCollection([ipoint], color='green'), ], [all_lines])
class Actor(): state = None environment = None heuristic = None frontier = None explored = None c = 0 f = 0 actions = [] def __init__(self, position, environment): self.state = State(position, position) self.environment = environment self.heuristic = manhattan_distance self.frontier = SortedSet(key = self.frontier_order) self.explored = set() self.reset() def update(self, action): self.state = action self.state.parent = None def move(self, position): self.state.target = position def can_act(self): return len(self.actions) def reset(self): self.actions = [] self.frontier.clear() self.explored.clear() self.explored.add(self.state) self.c = 0 self.f = 0 def frontier_order(self, state): return -state.path_cost def act(self): # Recalculate when bumping #if sensors[BUMPED] == 1: # del self.actions # No action is needed if we are at the target if self.state.goal(): return None result = True if not self.can_act(): # Reset values self.reset() # Think result = self.think(self.state) # If a route is already calculated, return next action if (result): return self.actions.pop() else: print "No solution found" return None def think(self, state): # Define the initial frontier self.expand_frontier(state) frontier_size = 1 while frontier_size: self.c += 1 # Get lowest valued frontier state state = self.frontier.pop() # Check for goal if state.goal(): self.recreate_actions(state) return True # Add current state to explored self.explored.add(state.as_tuple()) # Expand frontier self.expand_frontier(state) frontier_size = len(self.frontier) # DEBUG """s = '' for i in self.frontier: s += "{}:({},{}) ".format(i.cost, i.row(), i.column()) print s""" # DEBUG return False def expand_frontier(self, state): for row in (-1, 0, 1): for col in (-1, 0, 1): # Only allow adjacent non-diagonal moves #if row != 0 and col != 0: # continue # Get the new position position = Position(state.row() + row, state.column() + col) # Rule out invalid positions if position.row() < 0 or position.column() < 0 or \ position.row() >= self.environment.height or position.column() >= self.environment.width: return p = position.as_tuple() # If not an obstacle and not explored, then add to frontier if p not in self.environment.obstacles and p not in self.explored: self.f += 1 # Create the new state new_state = State(position, state.target, state.cost + 1, state) # Update state path cost new_state.path_cost = new_state.cost + self.heuristic(new_state) # Add to frontier self.frontier.add(new_state) def recreate_actions(self, state): while state is not None: self.actions.append(state) state = state.parent
def algo_visual(data_set): events = prepare_events(data_set) state = SortedSet() # (point, event, [skey]) intersections = [] points = [] all_lines = LinesCollection(data_set, color='gray') yield Scene([], [all_lines]) def get_neighbours(segment): index = state.index(segment) return ( state[index - 1].segment if index > 0 else None, #above state[index + 1].segment if index < len(state) - 1 else None ) #bellow def swap_on(point, segments): above, below = segments state.remove(above) state.remove(below) above.op = below.op = point state.add(above) state.add(below) def add_intersection_if_exists(segment, neighbour, orientation): if neighbour and segment: point = get_intersection_point(segment, neighbour, orientation) if point: inter = tuple(sorted([segment, neighbour])) if inter not in intersections: intersections.append(inter) points.append(point) events.put( (point, INTERSECT, [Key(segment), Key(neighbour)])) def add_intersections(state, skey): above, below = get_neighbours(skey) add_intersection_if_exists(skey.segment, above, BELOW) add_intersection_if_exists(skey.segment, below, ABOVE) for point, event, segments in iter_events(events): if event == START: [skey] = segments state.add(skey) add_intersections(state, skey) above, below = get_neighbours(skey) yield Scene([ PointsCollection(points[:], color='green'), PointsCollection([point], color='red'), ], [ all_lines, LinesCollection([x.segment for x in state], color='blue'), *([LinesCollection([above], color='red')] if above else []), *([LinesCollection([below], color='green')] if below else []) ]) elif event == END: [skey] = segments above, below = get_neighbours(skey) state.remove(skey) add_intersection_if_exists(above, below, ABOVE) yield Scene([ PointsCollection(points[:], color='green'), PointsCollection([point], color='red'), ], [ all_lines, LinesCollection([x.segment for x in state], color='blue'), *([LinesCollection([above], color='red')] if above else []), *([LinesCollection([below], color='green')] if below else []), ]) else: above, below = segments swap_on(point, segments) add_intersections(state, above) add_intersections(state, below) yield Scene([ PointsCollection(points[:], color='green'), PointsCollection([point], color='red'), ], [ all_lines, LinesCollection([x.segment for x in state], color='blue'), LinesCollection([below.segment], color='red'), LinesCollection([above.segment], color='green') ]) yield Scene([PointsCollection(points[:], color='green')], [all_lines])
def __init__(self): self.batches = SortedSet(key=lambda b: b.id) pass
class VersionChoiceDialog(object): @property def chosen_version(self): return self._chosen_version @property def do_not_ask_again(self): return self._do_not_ask_again @do_not_ask_again.setter def do_not_ask_again(self, value: bool): self._do_not_ask_again = value @property def keep_while_available(self): return self._keep_while_available @keep_while_available.setter def keep_while_available(self, value: bool): self._keep_while_available = value @property def persist(self): return self._persist @persist.setter def persist(self, value: bool): self._persist = value @property def return_code(self): return self._return_code @return_code.setter def return_code(self, value: str): self._return_code = value def persist_action(self): if self.persist is not None: self.persist ^= True if self.persist: self.keep_while_available = False self.do_not_ask_again = False else: self.keep_while_available = None self.do_not_ask_again = None def update_persist_state(self): if self.keep_while_available or self.do_not_ask_again: self.persist = None else: self.persist = False def keep_action(self): if self.keep_while_available is not None: self.keep_while_available ^= True self.update_persist_state() def noprompt_action(self): if self.do_not_ask_again is not None: self.do_not_ask_again ^= True def select_action(self): self._chosen_version = list(self._child_names)[self._index] if self.chosen_version.endswith(':KEEP'): self._can_continue = False self.persist = None self.keep_while_available = None self.do_not_ask_again = None else: self._can_continue = True self.persist = False self.persist = False self.do_not_ask_again = False def cancel_action(self): self.return_code = 'cancel' def accept_action(self): if self._can_continue: self.return_code = 'accept' def __init__(self, master: typing.Optional[object], versions: SortedSet, persist: typing.Optional[bool] = False, keep: typing.Optional[bool] = False, last_used: typing.Optional[str] = None, *args, **kwargs): # assign tkinter-compatible interface items to placeholder to placate PyCharm _ = master _ = args _ = kwargs self._can_continue = None self._child_names = SortedSet(versions) self._child_objects = None self._chosen_version = None self._do_not_ask_again = False self._keep_while_available = keep self._last_used = last_used self._return_code = None self._persist = persist self._index = 0 if persist: self.persist_action() if keep: self.keep_action() if last_used is not None: last_used_version_object = LooseVersion(last_used) self._index = self._child_names.index(last_used_version_object) \ if last_used_version_object in self._child_names else 0 self.select_action()
def bugs(self): bug_sets = map(lambda c: frozenset(c.bugs), self.chunks) return reduce(lambda a, b: a.union(b), bug_sets, SortedSet(key=lambda bug: bug.fully_qualified_name))
def tests(self): test_sets = map(lambda f: frozenset(f.tests), self.features) return reduce(lambda a, b: a.union(b), test_sets, SortedSet(key=lambda test: test.fully_qualified_name))
def chunks(self): chunk_sets = map(lambda f: frozenset(f.chunks), self.features) return reduce(lambda a, b: a.union(b), chunk_sets, SortedSet(key=lambda c: c.fully_qualified_name))
def __init__(self, *, elms=None): self._data = SortedSet( elms, key=operator.attrgetter('typ', 'cost') )
class SoftwareSystem(object): def __init__(self, probability_gain_feature_dependency=0.5, probability_lose_feature_dependency=0.25, probability_gain_system_dependency=0.1, probability_lose_system_dependency=0.25, probability_new_bug=0.5, probability_debug_known=0.9, probability_debug_unknown=0.01, probability_failure_on_demand=0.01, test_effectiveness=0.5, test_efficiency=0.5 ): self.probability_gain_feature_dependency = probability_gain_feature_dependency self.probability_lose_feature_dependency = probability_lose_feature_dependency self.probability_gain_system_dependency = probability_gain_system_dependency self.probability_lose_system_dependency = probability_lose_system_dependency self.probability_new_bug = probability_new_bug self.probability_debug_known = probability_debug_known self.probability_debug_unknown = probability_debug_unknown self.probability_failure_on_demand = probability_failure_on_demand self.test_effectiveness = test_effectiveness self.test_efficiency = test_efficiency self.features = SortedSet(key=lambda f: f.logical_name) self.successful_operations = list() def add_feature(self, logical_name, size): feature = Feature(self, logical_name, size) self.features.add(feature) return feature def get_feature(self, logical_name): result = filter(lambda f: f.logical_name == logical_name, self.features) if len(result) is 0: return None else: return result[0] def get_chunk(self, fully_qualified_name): result = filter(lambda chunk: chunk.fully_qualified_name == fully_qualified_name, self.chunks) if len(result) is 0: return None else: return result[0] @property def chunks(self): chunk_sets = map(lambda f: frozenset(f.chunks), self.features) return reduce(lambda a, b: a.union(b), chunk_sets, SortedSet(key=lambda c: c.fully_qualified_name)) @property def chunk_names(self): return map(lambda c: c.fully_qualified_name, self.chunks) @property def chunk_contents(self): return map(lambda c: c.local_content, self.chunks) @property def tests(self): test_sets = map(lambda f: frozenset(f.tests), self.features) return reduce(lambda a, b: a.union(b), test_sets, SortedSet(key=lambda test: test.fully_qualified_name)) @property def bugs(self): bug_sets = map(lambda c: frozenset(c.bugs), self.chunks) return reduce(lambda a, b: a.union(b), bug_sets, SortedSet(key=lambda bug: bug.fully_qualified_name)) def operate(self, random, limit=sys.maxint): current_operations = list() self.successful_operations.append(current_operations) if len(self.features) == 0: return while len(current_operations) < limit: next_feature = random.choice(self.features) next_feature.operate(random) current_operations.append(next_feature) @property def last_trace(self): """ :return : the last sequence of successful operations called by operate. """ last_trace_index = len(self.successful_operations) - 1 return None if last_trace_index < 0 else self.successful_operations[last_trace_index] @property def mean_operations_to_failure(self): total_operations = reduce(lambda x, y: x + y, map(lambda l: len(l), self.successful_operations), 0) if len(self.successful_operations) is 0: return 0 else: return total_operations / len(self.successful_operations) def __str__(self): result = [] for feature in self.features: result.append(" ") result.append(repr(feature)) result.append("[\n") for chunk in feature.chunks: result.append(" ") result.append(str(chunk)) result.append("\n") result.append("]\n") result.append("[\n") for test in self.tests: result.append(" ") result.append(str(test)) result.append("\n") result.append("]") return "".join(result)