def _find_common(self, lineset1, lineset2): """find similarities in the two given linesets""" lines1 = lineset1.enumerate_stripped lines2 = lineset2.enumerate_stripped find = lineset2.find index1 = 0 min_lines = self.min_lines while index1 < len(lineset1): skip = 1 num = 0 for index2 in find( lineset1[index1] ): non_blank = 0 for num, ((_, line1), (_, line2)) in enumerate( izip(lines1(index1), lines2(index2))): if line1 != line2: if non_blank > min_lines: yield num, lineset1, index1, lineset2, index2 skip = max(skip, num) break if line1: non_blank += 1 else: # we may have reach the end num += 1 if non_blank > min_lines: yield num, lineset1, index1, lineset2, index2 skip = max(skip, num) index1 += skip
def _mk_index(self): """create the index for this set""" index = {} for line_no, line in enumerate(self._stripped_lines): if line: index.setdefault(line, []).append( line_no ) return index
def _loopvar_name(self, node, name): # filter variables according to node's scope # XXX used to filter parents but don't remember why, and removing this # fixes a W0631 false positive reported by Paul Hachmann on 2008/12 on # python-projects (added to func_use_for_or_listcomp_var test) #astmts = [stmt for stmt in node.lookup(name)[1] # if hasattr(stmt, 'ass_type')] and # not stmt.statement().parent_of(node)] astmts = [stmt for stmt in node.lookup(name)[1] if hasattr(stmt, 'ass_type')] # filter variables according their respective scope if not astmts or astmts[0].statement().parent_of(node): _astmts = [] else: _astmts = astmts[:1] for i, stmt in enumerate(astmts[1:]): if astmts[i].statement().parent_of(stmt): continue _astmts.append(stmt) astmts = _astmts if len(astmts) == 1: ass = astmts[0].ass_type() if isinstance(ass, (astng.For, astng.Comprehension, astng.GenExpr)) \ and not ass.statement() is node.statement(): self.add_message('W0631', args=name, node=node)
def _iter_sims(self): """iterate on similarities among all files, by making a cartesian product """ for idx, lineset in enumerate(self.linesets[:-1]): for lineset2 in self.linesets[idx+1:]: for sim in self._find_common(lineset, lineset2): yield sim
def set_column(self, col_index, col_data): """sets the 'col_index' column pre: type(col_data) == types.ListType len(col_data) == len(self.row_names) """ for row_index, cell_data in enumerate(col_data): self.data[row_index][col_index] = cell_data
def append_column(self, col_data, col_name): """Appends the 'col_index' column pre: type(col_data) == types.ListType len(col_data) == len(self.row_names) """ self.col_names.append(col_name) for row_index, cell_data in enumerate(col_data): self.data[row_index].append(cell_data)
def transpose(self): """Keeps the self object intact, and returns the transposed (rotated) table. """ transposed = Table() transposed.create_rows(self.col_names) transposed.create_columns(self.row_names) for col_index, column in enumerate(self.get_columns()): transposed.set_row(col_index, column) return transposed
def insert_column(self, index, col_data, col_name): """Appends col_data before 'index' in the table. To make 'insert' behave like 'list.insert', inserting in an out of range index will insert col_data to the end of the list pre: type(col_data) == types.ListType len(col_data) == len(self.row_names) """ self.col_names.insert(index, col_name) for row_index, cell_data in enumerate(col_data): self.data[row_index].insert(index, cell_data)
def _write_body(self): """Writes the table body """ self._stream.write('<tbody>\n') for row_index, row in enumerate(self._table.data): self._stream.write('<row>\n') row_name = self._table.row_names[row_index] # Write the first entry (row_name) self._stream.write(self.renderer.render_row_cell(row_name, self._table, self.style)) for col_index, cell in enumerate(row): self._stream.write(self.renderer.render_cell( (row_index, col_index), self._table, self.style)) self._stream.write('</row>\n') self._stream.write('</tbody>\n')
def _write_body(self): """Writes the table body """ self._stream.write('<tbody>\n') for row_index, row in enumerate(self._table.data): self._stream.write('<row>\n') row_name = self._table.row_names[row_index] # Write the first entry (row_name) self._stream.write( self.renderer.render_row_cell(row_name, self._table, self.style)) for col_index, cell in enumerate(row): self._stream.write( self.renderer.render_cell((row_index, col_index), self._table, self.style)) self._stream.write('</row>\n') self._stream.write('</tbody>\n')
def pprint(self): """returns a string representing the table in a pretty printed 'text' format. """ # The maximum row name (to know the start_index of the first col) max_row_name = 0 for row_name in self.row_names: if len(row_name) > max_row_name: max_row_name = len(row_name) col_start = max_row_name + 5 lines = [] # Build the 'first' line <=> the col_names one # The first cell <=> an empty one col_names_line = [' '*col_start] for col_name in self.col_names: col_names_line.append(col_name.encode('iso-8859-1') + ' '*5) lines.append('|' + '|'.join(col_names_line) + '|') max_line_length = len(lines[0]) # Build the table for row_index, row in enumerate(self.data): line = [] # First, build the row_name's cell row_name = self.row_names[row_index].encode('iso-8859-1') line.append(row_name + ' '*(col_start-len(row_name))) # Then, build all the table's cell for this line. for col_index, cell in enumerate(row): col_name_length = len(self.col_names[col_index]) + 5 data = str(cell) line.append(data + ' '*(col_name_length - len(data))) lines.append('|' + '|'.join(line) + '|') if len(lines[-1]) > max_line_length: max_line_length = len(lines[-1]) # Wrap the table with '-' to make a frame lines.insert(0, '-'*max_line_length) lines.append('-'*max_line_length) return '\n'.join(lines)
def pprint(self): """returns a string representing the table in a pretty printed 'text' format. """ # The maximum row name (to know the start_index of the first col) max_row_name = 0 for row_name in self.row_names: if len(row_name) > max_row_name: max_row_name = len(row_name) col_start = max_row_name + 5 lines = [] # Build the 'first' line <=> the col_names one # The first cell <=> an empty one col_names_line = [' ' * col_start] for col_name in self.col_names: col_names_line.append(col_name.encode('iso-8859-1') + ' ' * 5) lines.append('|' + '|'.join(col_names_line) + '|') max_line_length = len(lines[0]) # Build the table for row_index, row in enumerate(self.data): line = [] # First, build the row_name's cell row_name = self.row_names[row_index].encode('iso-8859-1') line.append(row_name + ' ' * (col_start - len(row_name))) # Then, build all the table's cell for this line. for col_index, cell in enumerate(row): col_name_length = len(self.col_names[col_index]) + 5 data = str(cell) line.append(data + ' ' * (col_name_length - len(data))) lines.append('|' + '|'.join(line) + '|') if len(lines[-1]) > max_line_length: max_line_length = len(lines[-1]) # Wrap the table with '-' to make a frame lines.insert(0, '-' * max_line_length) lines.append('-' * max_line_length) return '\n'.join(lines)
def __getitem__(self, indices): """provided for convenience""" rows, multirows = None, False cols, multicols = None, False if isinstance(indices, tuple): rows = indices[0] if len(indices) > 1: cols = indices[1] else: rows = indices # define row slice if isinstance(rows,str): try: rows = self.row_names.index(rows) except ValueError: raise KeyError("Row (%s) not found in table" % (rows)) if isinstance(rows,int): rows = slice(rows,rows+1) multirows = False else: rows = slice(None) multirows = True # define col slice if isinstance(cols,str): try: cols = self.col_names.index(cols) except ValueError: raise KeyError("Column (%s) not found in table" % (cols)) if isinstance(cols,int): cols = slice(cols,cols+1) multicols = False else: cols = slice(None) multicols = True # get sub-table tab = Table() tab.default_value = self.default_value tab.create_rows(self.row_names[rows]) tab.create_columns(self.col_names[cols]) for idx,row in enumerate(self.data[rows]): tab.set_row(idx, row[cols]) if multirows : if multicols: return tab else: return [item[0] for item in tab.data] else: if multicols: return tab.data[0] else: return tab.data[0][0]
def __getitem__(self, indices): """provided for convenience""" rows, multirows = None, False cols, multicols = None, False if isinstance(indices, tuple): rows = indices[0] if len(indices) > 1: cols = indices[1] else: rows = indices # define row slice if isinstance(rows, str): try: rows = self.row_names.index(rows) except ValueError: raise KeyError("Row (%s) not found in table" % (rows)) if isinstance(rows, int): rows = slice(rows, rows + 1) multirows = False else: rows = slice(None) multirows = True # define col slice if isinstance(cols, str): try: cols = self.col_names.index(cols) except ValueError: raise KeyError("Column (%s) not found in table" % (cols)) if isinstance(cols, int): cols = slice(cols, cols + 1) multicols = False else: cols = slice(None) multicols = True # get sub-table tab = Table() tab.default_value = self.default_value tab.create_rows(self.row_names[rows]) tab.create_columns(self.col_names[cols]) for idx, row in enumerate(self.data[rows]): tab.set_row(idx, row[cols]) if multirows: if multicols: return tab else: return [item[0] for item in tab.data] else: if multicols: return tab.data[0] else: return tab.data[0][0]
def __init__(self, variables, domains, constraints = None, printer=_default_printer): # encode unicode self._printer = printer for i, var in enumerate(variables): if type(var) == type(u''): variables[i] = var.encode() self._variables = variables # list of variable names self._domains = domains # maps variable name to domain object self._constraints = [] # list of constraint objects # self._queue = [] # queue of constraints waiting to be processed self._variableListeners = {} for var in self._variables: self._variableListeners[var] = [] assert self._domains.has_key(var) for constr in constraints or (): self.addConstraint(constr)
def visit_tryexcept(self, node): """check for empty except""" exceptions_classes = [] nb_handlers = len(node.handlers) for index, handler in enumerate(node.handlers): # single except doing nothing but "pass" without else clause if nb_handlers == 1 and is_empty(handler.body) and not node.orelse: self.add_message('W0704', node=handler.type or handler.body[0]) if handler.type is None: if nb_handlers == 1 and not is_raising(handler.body): self.add_message('W0702', node=handler.body[0]) # check if a "except:" is followed by some other # except elif index < (nb_handlers - 1): msg = 'empty except clause should always appears last' self.add_message('E0701', node=node, args=msg) else: try: excs = list(unpack_infer(handler.type)) except astng.InferenceError: continue for exc in excs: # XXX skip other non class nodes if exc is YES or not isinstance(exc, astng.Class): continue exc_ancestors = [ anc for anc in exc.ancestors() if isinstance(anc, astng.Class) ] for previous_exc in exceptions_classes: if previous_exc in exc_ancestors: msg = '%s is an ancestor class of %s' % ( previous_exc.name, exc.name) self.add_message('E0701', node=handler.type, args=msg) if (exc.name == 'Exception' and exc.root().name == 'exceptions' and nb_handlers == 1 and not is_raising(handler.body)): self.add_message('W0703', node=handler.type) exceptions_classes += excs
def _loopvar_name(self, node, name): # filter variables according to node's scope astmts = [stmt for stmt in node.lookup(name)[1] if hasattr(stmt, 'ass_type') and not stmt.statement().parent_of(node)] # filter variables according their respective scope if not astmts or astmts[0].statement().parent_of(node): _astmts = [] else: _astmts = astmts[:1] for i, stmt in enumerate(astmts[1:]): if astmts[i].statement().parent_of(stmt): continue _astmts.append(stmt) astmts = _astmts if len(astmts) == 1: ass = astmts[0].ass_type() if isinstance(ass, (astng.For, astng.ListCompFor, astng.GenExpr)) \ and not ass.statement() is node.statement(): self.add_message('W0631', args=name, node=node)
def repr_tree_defs(data, indent_str=None): """return a string which represents imports as a tree""" lines = [] nodes = data.items() for i, (mod, (sub, files)) in enumerate(sorted(nodes, key=lambda x: x[0])): if not files: files = '' else: files = '(%s)' % ','.join(files) if indent_str is None: lines.append('%s %s' % (mod, files)) sub_indent_str = ' ' else: lines.append('%s\-%s %s' % (indent_str, mod, files)) if i == len(nodes)-1: sub_indent_str = '%s ' % indent_str else: sub_indent_str = '%s| ' % indent_str if sub: lines.append(repr_tree_defs(sub, sub_indent_str)) return '\n'.join(lines)
def repr_tree_defs(data, indent_str=None): """return a string which represents imports as a tree""" lines = [] nodes = data.items() for i, (mod, (sub, files)) in enumerate(sorted(nodes, key=lambda x: x[0])): if not files: files = '' else: files = '(%s)' % ','.join(files) if indent_str is None: lines.append('%s %s' % (mod, files)) sub_indent_str = ' ' else: lines.append('%s\-%s %s' % (indent_str, mod, files)) if i == len(nodes) - 1: sub_indent_str = '%s ' % indent_str else: sub_indent_str = '%s| ' % indent_str if sub: lines.append(repr_tree_defs(sub, sub_indent_str)) return '\n'.join(lines)
def visit_tryexcept(self, node): """check for empty except """ exceptions_classes = [] nb_handlers = len(node.handlers) for index, handler in enumerate(node.handlers): exc_type = handler[0] stmt = handler[2] # single except doing nothing but "pass" without else clause if nb_handlers == 1 and is_empty(stmt) and not node.else_: self.add_message('W0704', node=exc_type or stmt) if exc_type is None: if nb_handlers == 1 and not is_raising(stmt): self.add_message('W0702', node=stmt.nodes[0]) # check if a "except:" is followed by some other # except elif index < (nb_handlers - 1): msg = 'empty except clause should always appears last' self.add_message('E0701', node=node, args=msg) else: try: excs = list(unpack_infer(exc_type)) except astng.InferenceError: continue for exc in excs: # XXX skip other non class nodes if exc is astng.YES or not isinstance(exc, astng.Class): continue exc_ancestors = [anc for anc in exc.ancestors() if isinstance(anc, astng.Class)] for previous_exc in exceptions_classes: if previous_exc in exc_ancestors: msg = '%s is an ancestor class of %s' % ( previous_exc.name, exc.name) self.add_message('E0701', node=exc_type, args=msg) if (exc.name == 'Exception' and exc.root().name == 'exceptions' and nb_handlers == 1 and not is_raising(stmt)): self.add_message('W0703', node=exc_type) exceptions_classes += excs
def __init__(self, variables, domains, constraints=None, printer=_default_printer): # encode unicode self._printer = printer for i, var in enumerate(variables): if type(var) == type(u''): variables[i] = var.encode() self._variables = variables # list of variable names self._domains = domains # maps variable name to domain object self._constraints = [] # list of constraint objects # self._queue = [] # queue of constraints waiting to be processed self._variableListeners = {} for var in self._variables: self._variableListeners[var] = [] assert self._domains.has_key(var) for constr in constraints or (): self.addConstraint(constr)
def test_enumerate(self): from logilab.common.compat import enumerate self.assertEquals(list(enumerate([])), []) self.assertEquals(list(enumerate('abc')), [(0, 'a'), (1, 'b'), (2, 'c')])