def _setup_call_character_ranges(self) -> List[Tuple[int, int]]: start: int stop: int code: str preceding_code: str setup_call: str character_index: int = 0 setup_calls_character_ranges: List[Tuple[int, int]] = [] parenthesis_imbalance: int = 0 for code, string_literal in grouper( re.split(STRING_LITERAL_RE, self._source), 2, None ): code_length: int = len(code or "") string_literal_length: int = len(string_literal or "") if code: ( setup_call_ranges, parenthesis_imbalance, ) = _get_setup_call_indices_and_parenthesis_imbalance( code, parenthesis_imbalance, offset=character_index ) if ( (setup_call_ranges) and (setup_calls_character_ranges) and (setup_calls_character_ranges[-1][-1] is None) ): # ...we left off inside a setup call setup_calls_character_ranges[-1] = ( setup_calls_character_ranges[-1][0], (code[: setup_call_ranges[0][0]].rindex(")") + 1), ) for start, stop in setup_call_ranges: setup_calls_character_ranges.append((start, stop)) character_index += code_length + string_literal_length return setup_calls_character_ranges
def _get_setup_call_start_indices_and_parenthesis_imbalance( code: str, parenthesis_imbalance: int = 0 ) -> Tuple[List[int], int]: setup_call_character_indices: List[int] = [] subsequent_setup_call_character_indices: List[int] = [] preceding_code: str setup_call: str code_length: int = len(code) preceding_code, setup_call = next( iter(grouper(_SETUP_CALL_PATTERN.split(code), 2, None)) # noqa ) if setup_call and re.match( r"^.*\b(def|class)[ ]+$", preceding_code, re.DOTALL ): preceding_code += setup_call setup_call = None preceding_code_length: int = len(preceding_code or "") setup_call_length: int = len(setup_call or "") if preceding_code: parenthesis_imbalance += _get_imbalance(preceding_code) if setup_call: if parenthesis_imbalance == 0: setup_call_character_indices.append(preceding_code_length) parenthesis_imbalance += _get_imbalance(setup_call) else: parenthesis_imbalance += _get_imbalance(setup_call) preceding_code += setup_call preceding_code_length += setup_call_length setup_call = None setup_call_length = 0 character_index: int = preceding_code_length + setup_call_length if character_index < code_length: ( subsequent_setup_call_character_indices, parenthesis_imbalance, ) = _get_setup_call_start_indices_and_parenthesis_imbalance( code[character_index:], parenthesis_imbalance ) return ( setup_call_character_indices + [ (character_index + subsequent_character_index) for subsequent_character_index in ( subsequent_setup_call_character_indices ) ], parenthesis_imbalance, )
def process_data(self, conf): super(Schedule, self).process_data(conf) data = conf['data'] triples = grouper(data, 3) labels, begin_dates, end_dates = zip(*triples) begin_dates = map(self.parse_date, begin_dates) end_dates = map(self.parse_date, end_dates) # reconstruct the triples in a new order reordered_triples = list(zip(begin_dates, end_dates, labels)) # because of the reordering, this will sort by begin_date # then end_date, then label. reordered_triples.sort() conf['data'] = reordered_triples
def process_data(self, conf): super(Schedule, self).process_data(conf) data = conf['data'] triples = grouper(3, data) labels, begin_dates, end_dates = zip(*triples) begin_dates = map(self.parse_date, begin_dates) end_dates = map(self.parse_date, end_dates) # reconstruct the triples in a new order reordered_triples = list(zip(begin_dates, end_dates, labels)) # because of the reordering, this will sort by begin_date # then end_date, then label. reordered_triples.sort() conf['data'] = reordered_triples
def make_rows(num_columns, seq): """ Make a sequence into rows of num_columns columns. >>> tuple(make_rows(2, [1, 2, 3, 4, 5])) ((1, 4), (2, 5), (3, None)) >>> tuple(make_rows(3, [1, 2, 3, 4, 5])) ((1, 3, 5), (2, 4, None)) """ # calculate the minimum number of rows necessary to fit the list in # num_columns Columns num_rows, partial = divmod(len(seq), num_columns) if partial: num_rows += 1 # break the seq into num_columns of length num_rows result = recipes.grouper(num_rows, seq) # result is now a list of columns... transpose it to return a list # of rows return zip(*result)