def test_dfs_before(self): g = self.make_linear(10) inc = [] dec = [] def before(id): inc.append(id) def after(id): dec.append(id) g.dfs(enter_vertex=before, leave_vertex=after) assert inc == [0,1,2,3,4,5,6,7,8,9] assert inc == list(reversed(dec)) assert all(a < b for a, b in izip(inc[:-1], inc[1:])) assert all(a > b for a, b in izip(dec[:-1], dec[1:]))
def test_dfs_before(self): g = self.make_linear(10) inc = [] dec = [] def before(id): inc.append(id) def after(id): dec.append(id) g.dfs(enter_vertex=before, leave_vertex=after) assert inc == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] assert inc == list(reversed(dec)) assert all(a < b for a, b in izip(inc[:-1], inc[1:])) assert all(a > b for a, b in izip(dec[:-1], dec[1:]))
def trim_first_paragraph(self): doc = self.document() count = doc.blockCount() cursor = QtGui.QTextCursor(doc) cursor.select(QtGui.QTextCursor.LineUnderCursor) sel = cursor.selection().toPlainText() if all(sel, lambda c: c == ' '): cursor.removeSelectedText() cursor = QtGui.QTextCursor(doc) cursor.deleteChar()
def trim_first_paragraph(self): doc = self.document() count = doc.blockCount() cursor = QtGui.QTextCursor(doc) cursor.select(QtGui.QTextCursor.LineUnderCursor) sel = cursor.selection().toPlainText() if all(c == ' ' for c in sel): cursor.removeSelectedText() cursor = QtGui.QTextCursor(doc) cursor.deleteChar()
def addStatePorts(module, state_dict): """ addStatePorts(module: Module, state_dict: dict) -> None Convert all SetxxxToyyy methods of module into input ports Keyword arguments: module --- Module state_dict --- the State method signatures returned by vtk_parser """ klass = get_description_class(module.vtkClass) registry = get_module_registry() for name in state_dict.iterkeys(): for mode in state_dict[name]: # Creates the port Set foo to bar field = 'Set'+name+'To'+mode[0] if field in disallowed_state_ports: continue if not registry.has_input_port(module, field): registry.add_input_port(module, field, [], True) # Now create the port Set foo with parameter if hasattr(klass, 'Set%s'%name): setterMethod = getattr(klass, 'Set%s'%name) setterSig = get_method_signature(setterMethod) # if the signature looks like an enum, we'll skip it, it shouldn't # be necessary if len(setterSig) > 1: prune_signatures(module, 'Set%s'%name, setterSig) for ix, setter in enumerate(setterSig): n = resolve_overloaded_name('Set' + name, ix, setterSig) tm = typeMap(setter[1][0]) if len(setter[1]) == 1 and is_class_allowed(tm): registry.add_input_port(module, n, tm, setter[1][0] in typeMapDict) else: classes = [typeMap(i) for i in setter[1]] if all(is_class_allowed(x) for x in classes): registry.add_input_port(module, n, classes, True)
def addOtherPorts(module, other_list): """ addOtherPorts(module: Module, other_list: list) -> None Convert all other ports such as Insert/Add.... into input/output Keyword arguments: module --- Module other_dict --- any other method signatures that is not Algorithm/SetGet/Toggle/State type """ klass = get_description_class(module.vtkClass) registry = get_module_registry() for name in other_list: if name[:3] in ['Add','Set'] or name[:6]=='Insert': if name in disallowed_other_ports: continue method = getattr(klass, name) signatures = get_method_signature(method) if len(signatures) > 1: prune_signatures(module, name, signatures) for (ix, sig) in enumerate(signatures): ([result], params) = sig types = [] if params: for p in params: t = typeMap(p) if not t: types = None break else: types.append(t) else: types = [[]] if types: if not all(is_class_allowed(x) for x in types): continue n = resolve_overloaded_name(name, ix, signatures) if len(types)<=1: registry.add_input_port(module, n, types[0], types[0] in typeMapDictValues) else: registry.add_input_port(module, n, types, True) else: if name in disallowed_other_ports: continue method = getattr(klass, name) signatures = get_method_signature(method) if len(signatures) > 1: prune_signatures(module, name, signatures) for (ix, sig) in enumerate(signatures): ([result], params) = sig types = [] if params: types = [typeMap(p) for p in params] else: types = [] if not all(is_class_allowed(x) for x in types): continue if types==[] or (result==None): n = resolve_overloaded_name(name, ix, signatures) registry.add_input_port(module, n, types, not (n in force_not_optional_port))
def addSetGetPorts(module, get_set_dict, delayed): """ addSetGetPorts(module: Module, get_set_dict: dict) -> None Convert all Setxxx methods of module into input ports and all Getxxx methods of module into output ports Keyword arguments: module --- Module get_set_dict --- the Set/Get method signatures returned by vtk_parser """ klass = get_description_class(module.vtkClass) registry = get_module_registry() for name in get_set_dict.iterkeys(): if name in disallowed_set_get_ports: continue getterMethod = getattr(klass, 'Get%s'%name) setterMethod = getattr(klass, 'Set%s'%name) getterSig = get_method_signature(getterMethod) setterSig = get_method_signature(setterMethod) if len(getterSig) > 1: prune_signatures(module, 'Get%s'%name, getterSig, output=True) for order, getter in enumerate(getterSig): if getter[1]: debug("Can't handle getter %s (%s) of class %s: Needs input to " "get output" % (order+1, name, klass)) continue if len(getter[0]) != 1: debug("Can't handle getter %s (%s) of class %s: More than a " "single output" % (order+1, name, str(klass))) continue class_ = typeMap(getter[0][0]) if is_class_allowed(class_): registry.add_output_port(module, 'Get'+name, class_, True) if len(setterSig) > 1: prune_signatures(module, 'Set%s'%name, setterSig) for ix, setter in enumerate(setterSig): if setter[1]==None: continue n = resolve_overloaded_name('Set' + name, ix, setterSig) if len(setter[1]) == 1 and is_class_allowed(typeMap(setter[1][0])): registry.add_input_port(module, n, typeMap(setter[1][0]), setter[1][0] in typeMapDict) else: classes = [typeMap(i) for i in setter[1]] if all(is_class_allowed(x) for x in classes): registry.add_input_port(module, n, classes, True) # Wrap SetFileNames for VisTrails file access if file_name_pattern.match(name): registry.add_input_port(module, 'Set' + name[:-4], (File, 'input file'), False) # Wrap SetRenderWindow for exporters elif name == 'RenderWindow': # cscheid 2008-07-11 This is messy: VTKCell isn't # registered yet, so we can't use it as a port # However, we can't register VTKCell before these either, # because VTKCell requires vtkRenderer. The "right" way would # be to add all modules first, then all ports. However, that would # be too slow. # Workaround: delay the addition of the port by storing # the information in a list if registry.has_module('edu.utah.sci.vistrails.spreadsheet', 'SpreadsheetCell'): delayed.add_input_port.append((module, 'SetVTKCell', VTKCell, False)) # Wrap color methods for VisTrails GUI facilities elif name == 'DiffuseColor': registry.add_input_port(module, 'SetDiffuseColorWidget', (Color, 'color'), True) elif name == 'Color': registry.add_input_port(module, 'SetColorWidget', (Color, 'color'), True) elif name == 'AmbientColor': registry.add_input_port(module, 'SetAmbientColorWidget', (Color, 'color'), True) elif name == 'SpecularColor': registry.add_input_port(module, 'SetSpecularColorWidget', (Color, 'color'), True) elif name == 'EdgeColor': registry.add_input_port(module, 'SetEdgeColorWidget', (Color, 'color'), True) elif name == 'Background' : registry.add_input_port(module, 'SetBackgroundWidget', (Color, 'color'), True) elif name == 'Background2' : registry.add_input_port(module, 'SetBackground2Widget', (Color, 'color'), True)
try: exec(self.function) in {}, d except Exception, e: return [module.default_value] * count def evaluate(i): try: v = d['value'](i) if v == None: return module.default_value return v except Exception, e: return str(e) return [evaluate(i) for i in xrange(self.size)] result = get() if not all(result, module.validate): show_warning('Failed Validation', 'One of the <i>%s</i>\'s user defined ' 'functions has failed validation, ' 'which usually means it generated a ' 'value of a type different ' 'than that specified by the ' 'parameter. Parameter Exploration ' 'aborted.' % param_info.type) return None return result def getValuesString(self): """ getValuesString() -> str Return a string representation of the parameter list
def keyPressEvent(self, e): """keyPressEvent(e) -> None Handle user input a key at a time. Notice that text might come more than one keypress at a time if user is a fast enough typist! """ text = e.text() key = e.key() # NB: Sometimes len(str(text)) > 1! if text.length() and all( ord(x) >= 32 and ord(x) < 127 for x in str(text)): # exit select mode and jump to end of text cursor = self.textCursor() if self.selectMode or cursor.hasSelection(): self.selectMode = False cursor.movePosition(QtGui.QTextCursor.End) cursor.clearSelection() self.setTextCursor(cursor) self.__insertText(text) return if e.modifiers() & QtCore.Qt.MetaModifier and key == self.eofKey: self.parent().closeSession() if e.modifiers() & QtCore.Qt.ControlModifier: if key == QtCore.Qt.Key_C or key == QtCore.Qt.Key_Insert: self.copy() elif key == QtCore.Qt.Key_V: cursor = self.textCursor() cursor.movePosition(QtGui.QTextCursor.End) cursor.clearSelection() self.setTextCursor(cursor) self.paste() elif key == QtCore.Qt.Key_A: self.selectAll() self.selectMode = True else: e.ignore() return if e.modifiers() & QtCore.Qt.ShiftModifier: if key == QtCore.Qt.Key_Insert: cursor = self.textCursor() cursor.movePosition(QtGui.QTextCursor.End) cursor.clearSelection() self.setTextCursor(cursor) self.paste() else: e.ignore() return # exit select mode and jump to end of text cursor = self.textCursor() if self.selectMode or cursor.hasSelection(): self.selectMode = False cursor.movePosition(QtGui.QTextCursor.End) cursor.clearSelection() self.setTextCursor(cursor) if key == QtCore.Qt.Key_Backspace: if self.point: QtGui.QTextEdit.keyPressEvent(self, e) self.point -= 1 self.line.remove(self.point, 1) elif key == QtCore.Qt.Key_Delete: QtGui.QTextEdit.keyPressEvent(self, e) self.line.remove(self.point, 1) elif key == QtCore.Qt.Key_Return or key == QtCore.Qt.Key_Enter: if self.reading: self.reading = 0 else: self.__run() elif key == QtCore.Qt.Key_Tab: self.__insertText(text) elif key == QtCore.Qt.Key_Left: if self.point: QtGui.QTextEdit.keyPressEvent(self, e) self.point -= 1 elif key == QtCore.Qt.Key_Right: if self.point < self.line.length(): QtGui.QTextEdit.keyPressEvent(self, e) self.point += 1 elif key == QtCore.Qt.Key_Home: cursor = self.textCursor() cursor.movePosition(QtGui.QTextCursor.StartOfLine) cursor.setPosition(cursor.position() + 4) self.setTextCursor(cursor) self.point = 0 elif key == QtCore.Qt.Key_End: QtGui.QTextEdit.keyPressEvent(self, e) self.point = self.line.length() elif key == QtCore.Qt.Key_Up: if len(self.history): if self.pointer == 0: self.pointer = len(self.history) self.pointer -= 1 self.__recall() elif key == QtCore.Qt.Key_Down: if len(self.history): self.pointer += 1 if self.pointer == len(self.history): self.pointer = 0 self.__recall() else: e.ignore()
def keyPressEvent(self, e): """keyPressEvent(e) -> None Handle user input a key at a time. Notice that text might come more than one keypress at a time if user is a fast enough typist! """ text = e.text() key = e.key() # NB: Sometimes len(str(text)) > 1! if text.length() and all(ord(x) >= 32 and ord(x) < 127 for x in str(text)): # exit select mode and jump to end of text cursor = self.textCursor() if self.selectMode or cursor.hasSelection(): self.selectMode = False cursor.movePosition(QtGui.QTextCursor.End) cursor.clearSelection() self.setTextCursor(cursor) self.__insertText(text) return if e.modifiers() & QtCore.Qt.MetaModifier and key == self.eofKey: self.parent().closeSession() if e.modifiers() & QtCore.Qt.ControlModifier: if key == QtCore.Qt.Key_C or key == QtCore.Qt.Key_Insert: self.copy() elif key == QtCore.Qt.Key_V: cursor = self.textCursor() cursor.movePosition(QtGui.QTextCursor.End) cursor.clearSelection() self.setTextCursor(cursor) self.paste() elif key == QtCore.Qt.Key_A: self.selectAll() self.selectMode = True else: e.ignore() return if e.modifiers() & QtCore.Qt.ShiftModifier: if key == QtCore.Qt.Key_Insert: cursor = self.textCursor() cursor.movePosition(QtGui.QTextCursor.End) cursor.clearSelection() self.setTextCursor(cursor) self.paste() else: e.ignore() return # exit select mode and jump to end of text cursor = self.textCursor() if self.selectMode or cursor.hasSelection(): self.selectMode = False cursor.movePosition(QtGui.QTextCursor.End) cursor.clearSelection() self.setTextCursor(cursor) if key == QtCore.Qt.Key_Backspace: if self.point: QtGui.QTextEdit.keyPressEvent(self, e) self.point -= 1 self.line.remove(self.point, 1) elif key == QtCore.Qt.Key_Delete: QtGui.QTextEdit.keyPressEvent(self, e) self.line.remove(self.point, 1) elif key == QtCore.Qt.Key_Return or key == QtCore.Qt.Key_Enter: if self.reading: self.reading = 0 else: self.__run() elif key == QtCore.Qt.Key_Tab: self.__insertText(text) elif key == QtCore.Qt.Key_Left: if self.point: QtGui.QTextEdit.keyPressEvent(self, e) self.point -= 1 elif key == QtCore.Qt.Key_Right: if self.point < self.line.length(): QtGui.QTextEdit.keyPressEvent(self, e) self.point += 1 elif key == QtCore.Qt.Key_Home: cursor = self.textCursor() cursor.movePosition(QtGui.QTextCursor.StartOfLine) cursor.setPosition(cursor.position() + 4) self.setTextCursor(cursor) self.point = 0 elif key == QtCore.Qt.Key_End: QtGui.QTextEdit.keyPressEvent(self, e) self.point = self.line.length() elif key == QtCore.Qt.Key_Up: if len(self.history): if self.pointer == 0: self.pointer = len(self.history) self.pointer -= 1 self.__recall() elif key == QtCore.Qt.Key_Down: if len(self.history): self.pointer += 1 if self.pointer == len(self.history): self.pointer = 0 self.__recall() else: e.ignore()
try: exec(self.function) in {}, d except Exception, e: return [module.default_value] * count def evaluate(i): try: v = d['value'](i) if v == None: return module.default_value return v except Exception, e: return str(e) return [evaluate(i) for i in xrange(self.size)] result = get() if not all(module.validate(x) for x in result): show_warning('Failed Validation', 'One of the <i>%s</i>\'s user defined ' 'functions has failed validation, ' 'which usually means it generated a ' 'value of a type different ' 'than that specified by the ' 'parameter. Parameter Exploration ' 'aborted.' % param_info.type) return None return result def getValuesString(self): """ getValuesString() -> str Return a string representation of the parameter list