def load_url(self, from_reload=False): self.closeContext() self.domain_editor.set_domain(None) url = self.combo.currentText() if not self.is_valid_url(url): self.table = None self.commit() return if url not in self.recent: self.recent.insert(0, url) prev_table = self.table with self.progressBar(3) as progress: try: self._html = None self.webview.setUrl(url) wait(until=lambda: self._html is not None) progress.advance() # Wait some seconds for discrete labels to have loaded via AJAX, # then re-query HTML. # *Webview.loadFinished doesn't guarantee it sufficiently try: wait(until=lambda: False, timeout=1200) except TimeoutError: pass progress.advance() html = self.webview.html() except Exception as e: log.exception("Couldn't load data from: %s", url) self.Error.net_error(try_(lambda: e.args[0], '')) self.table = None else: self.Error.clear() self.Information.clear() self.table = None try: table = self.table = self.table_from_html(html) except DataEmptyError: self.Information.response_data_empty() except DataIsAnalError: self.Error.data_is_anal() except Exception as e: log.exception('Parsing error: %s', url) self.Error.parse_error(try_(lambda: e.args[0], '')) else: self.openContext(table.domain) self.combo.setTitleFor(self.combo.currentIndex(), table.name) def _equal(data1, data2): NAN = float('nan') return (try_(lambda: data1.checksum(), NAN) == try_(lambda: data2.checksum(), NAN)) self._orig_table = self.table self.apply_domain_edit() if not (from_reload and _equal(prev_table, self.table)): self.commit()
def load_url(self, from_reload=False): url = self.combo.currentText() if not url: return if url not in self.recent: self.recent.insert(0, url) prev_table = self.table try: with self.progressBar(3) as progress: progress.advance() table = Table.from_url(url) progress.advance() except Exception as e: log.exception("Couldn't load data from: %s", url) self.Error.error(try_(lambda: e.args[0], '')) self.table = None else: self.Error.clear() self.table = table self.combo.setTitleFor(self.combo.currentIndex(), table.name) self.set_info() def _equal(data1, data2): NAN = float('nan') return (try_(lambda: data1.checksum(), NAN) == try_(lambda: data2.checksum(), NAN)) if not (from_reload and _equal(prev_table, self.table)): self.commit()
def commit(self): data = self.data if data is not None: data = data.copy() data.set_interpolation(self.interpolation, self.multivariate) self.Outputs.timeseries.send(data) self.Outputs.interpolated.send(try_(lambda: data.interp()) or None)
def load_url(self, from_reload=False): url = self.combo.currentText() if not url: return prev_table = self.table try: with self.progressBar(3) as progress: progress.advance() table = Table.from_url(url) progress.advance() except Exception as e: log.exception("Couldn't load data from: %s", url) self.Error.error(try_(lambda: e.args[0], '')) self.table = None else: self.Error.clear() self.table = table self.combo.setTitleFor(self.combo.currentIndex(), table.name) self.set_info() def _equal(data1, data2): NAN = float('nan') return (try_(lambda: data1.checksum(), NAN) == try_(lambda: data2.checksum(), NAN)) if not (from_reload and _equal(prev_table, self.table)): self.commit()
def commit(self): data = self.data if data is not None: data = data.copy() data.set_interpolation(self.interpolation, self.multivariate) self.send(Output.TIMESERIES, data) self.send(Output.INTERPOLATED, try_(lambda: data.interp()) or None)
def set_info(self): data = self.table if data is None: self.data_info.setText('No spreadsheet loaded.') return text = "{}\n\n{} instance(s), {} feature(s), {} meta attribute(s)\n".format( data.name, len(data), len(data.domain.attributes), len(data.domain.metas)) text += try_(lambda: '\nFirst entry: {}' '\nLast entry: {}'.format(data[0, self.DATETIME_VAR], data[-1, self.DATETIME_VAR]), '') self.data_info.setText(text)
def set_info(self): data = self.table if data is None: self.data_info.setText('No spreadsheet loaded.') return text = "{}\n\n{} instance(s), {} feature(s), {} meta attribute(s)\n".format( data.name, len(data), len(data.domain.attributes), len(data.domain.metas)) text += try_(lambda: '\nFirst entry: {}' '\nLast entry: {}'.format(data[0, 'Timestamp'], data[-1, 'Timestamp']), '') self.data_info.setText(text)
def set_data(self, data): self.data = data self.graph.clear() self.closeContext() model = self.model colormodel = self.colormodel self.sample = None self.selected_attrs = None self.color_attr = None N_SAMPLE = 2000 if data is not None and len(data) and len(data.domain): self.sample = slice( None) if len(data) <= N_SAMPLE else np.random.choice( np.arange(len(data)), N_SAMPLE, replace=False) model.set_domain(data.domain) colormodel.set_domain(data.domain) self.color_attr = try_(lambda: data.domain.class_vars[0].name, None) selected_attrs = (model.data(model.index(i, 0)) for i in range( min(self.OPTIMIZATION_N_DIMS[1], model.rowCount()))) self.selected_attrs = [ attr for attr in selected_attrs if isinstance(attr, str) ] else: model.set_domain(None) colormodel.set_domain(None) self.Information.dataset_sampled( shown=False if data is None else len(data) > N_SAMPLE) self.openContext(data.domain) self.update_plot() self.commit()
def _equal(data1, data2): NAN = float('nan') return (try_(lambda: data1.checksum(), NAN) == try_(lambda: data2.checksum(), NAN))
def report_abstract_model(model, view=None): columns = [ i for i in range(model.columnCount()) if not view or not view.isColumnHidden(i) ] rows = [ i for i in range(model.rowCount()) if not view or not view.isRowHidden(i) ] has_horizontal_header = ( try_(lambda: not view.horizontalHeader().isHidden()) or try_(lambda: not view.header().isHidden())) has_vertical_header = try_( lambda: not view.verticalHeader().isHidden()) def item_html(row, col): def data(role=Qt.DisplayRole, orientation=Qt.Horizontal if row is None else Qt.Vertical): if row is None or col is None: return model.headerData(col if row is None else row, orientation, role) return model.data(model.index(row, col), role) selected = (view.selectionModel().isSelected( model.index(row, col)) if view and row is not None and col is not None else False) fgcolor = data(Qt.ForegroundRole) fgcolor = fgcolor.color().name() if fgcolor else 'black' bgcolor = data(Qt.BackgroundRole) bgcolor = bgcolor.color().name() if bgcolor else 'transparent' if bgcolor.lower() == '#ffffff': bgcolor = 'transparent' font = data(Qt.FontRole) weight = 'bold' if font and font.bold() else 'normal' alignment = data(Qt.TextAlignmentRole) or Qt.AlignLeft halign = ('left' if alignment & Qt.AlignLeft else 'right' if alignment & Qt.AlignRight else 'center') valign = ('top' if alignment & Qt.AlignTop else 'bottom' if alignment & Qt.AlignBottom else 'middle') return ('<{tag} style="' 'color:{fgcolor};' 'border:{border};' 'background:{bgcolor};' 'font-weight:{weight};' 'text-align:{halign};' 'vertical-align:{valign};">{text}</{tag}>'.format( tag='th' if row is None or col is None else 'td', border='1px solid black' if selected else '0', text=data() or '', weight=weight, fgcolor=fgcolor, bgcolor=bgcolor, halign=halign, valign=valign)) stream = [] if has_horizontal_header: stream.append('<tr>') stream.extend(item_html(None, col) for col in columns) stream.append('</tr>') for row in rows: stream.append('<tr>') if has_vertical_header: stream.append(item_html(row, None)) stream.extend(item_html(row, col) for col in columns) stream.append('</tr>') return ''.join(stream)
def report_abstract_model(model, view=None): columns = [ i for i in range(model.columnCount()) if not view or not view.isColumnHidden(i) ] rows = [ i for i in range(model.rowCount()) if not view or not view.isRowHidden(i) ] has_horizontal_header = try_(lambda: not view.horizontalHeader( ).isHidden()) or try_(lambda: not view.header().isHidden()) has_vertical_header = try_( lambda: not view.verticalHeader().isHidden()) def item_html(row, col): def data( role=Qt.DisplayRole, orientation=Qt.Horizontal if row is None else Qt.Vertical, ): if row is None or col is None: return model.headerData(col if row is None else row, orientation, role) return model.data(model.index(row, col), role) selected = (view.selectionModel().isSelected( model.index(row, col)) if view and row is not None and col is not None else False) fgcolor = data(Qt.ForegroundRole) fgcolor = (QBrush(fgcolor).color().name() if isinstance( fgcolor, (QBrush, QColor)) else "black") bgcolor = data(Qt.BackgroundRole) bgcolor = (QBrush(bgcolor).color().name() if isinstance( bgcolor, (QBrush, QColor)) else "transparent") if bgcolor.lower() == "#ffffff": bgcolor = "transparent" font = data(Qt.FontRole) weight = "font-weight: bold;" if font and font.bold() else "" alignment = data(Qt.TextAlignmentRole) or Qt.AlignLeft halign = ("left" if alignment & Qt.AlignLeft else "right" if alignment & Qt.AlignRight else "center") valign = ("top" if alignment & Qt.AlignTop else "bottom" if alignment & Qt.AlignBottom else "middle") return ('<{tag} style="' "color:{fgcolor};" "border:{border};" "background:{bgcolor};" "{weight}" "text-align:{halign};" 'vertical-align:{valign};">{text}</{tag}>'.format( tag="th" if row is None or col is None else "td", border="1px solid black" if selected else "0", text=data() or "", weight=weight, fgcolor=fgcolor, bgcolor=bgcolor, halign=halign, valign=valign, )) stream = [] if has_horizontal_header: stream.append("<tr>") if has_vertical_header: stream.append("<th></th>") stream.extend(item_html(None, col) for col in columns) stream.append("</tr>") for row in rows[:row_limit]: stream.append("<tr>") if has_vertical_header: stream.append(item_html(row, None)) stream.extend(item_html(row, col) for col in columns) stream.append("</tr>") return "".join(stream)
def handle_exception(cls, exc): etype, evalue, tb = exc exception = traceback.format_exception_only(etype, evalue)[-1].strip() stacktrace = ''.join(traceback.format_exception(etype, evalue, tb)) def _find_last_frame(tb): if not tb: return None while tb.tb_next: tb = tb.tb_next return tb err_locals, err_module, frame = None, None, _find_last_frame(tb) if frame: err_module = '{}:{}'.format( frame.tb_frame.f_globals.get('__name__', frame.tb_frame.f_code.co_filename), frame.tb_lineno) err_locals = OrderedDict(sorted(frame.tb_frame.f_locals.items())) err_locals = try_(lambda: pformat(err_locals), try_(lambda: str(err_locals))) def _find_widget_frame(tb): while tb: if isinstance(tb.tb_frame.f_locals.get('self'), OWWidget): return tb tb = tb.tb_next widget_module = widget_class = widget = workflow = None frame = _find_widget_frame(tb) if frame is not None: widget = frame.tb_frame.f_locals['self'] # type: OWWidget widget_class = widget.__class__ widget_module = '{}:{}'.format(widget_class.__module__, frame.tb_lineno) if widget is not None: try: workflow = widget.signalManager.parent() if not isinstance(workflow, Scheme): raise TypeError except Exception: workflow = None packages = ', '.join(sorted(get_installed_distributions())) machine_id = QSettings().value('error-reporting/machine-id', '', type=str) # If this exact error was already reported in this session, # just warn about it # or if computer not connected to the internet if (err_module, widget_module) in cls._cache or not internet_on(): QMessageBox(QMessageBox.Warning, 'Error Encountered', 'Error encountered{}:<br><br><tt>{}</tt>'.format( (' in widget <b>{}</b>'.format(widget_class.name) if widget_class else ''), stacktrace.replace('\n', '<br>').replace(' ', ' ')), QMessageBox.Ignore).exec() return F = cls.DataField data = OrderedDict() data[F.EXCEPTION] = exception data[F.MODULE] = err_module if widget_class is not None: data[F.WIDGET_NAME] = widget_class.name data[F.WIDGET_MODULE] = widget_module if workflow is not None: fd, filename = mkstemp(prefix='ows-', suffix='.ows.xml') os.close(fd) with open(filename, "wb") as f: try: workflow.save_to(f, pretty=True, pickle_fallback=True) except Exception: pass data[F.WIDGET_SCHEME] = filename with open(filename, encoding='utf-8') as f: data['_' + F.WIDGET_SCHEME] = f.read() data[F.VERSION] = VERSION_STR data[F.ENVIRONMENT] = 'Python {} on {} {} {} {}'.format( platform.python_version(), platform.system(), platform.release(), platform.version(), platform.machine()) data[F.INSTALLED_PACKAGES] = packages data[F.MACHINE_ID] = machine_id or str(uuid.getnode()) data[F.STACK_TRACE] = stacktrace if err_locals: data[F.LOCALS] = err_locals cls(data=data).exec()
def handle_exception(cls, exc): (etype, evalue, tb), canvas = exc exception = traceback.format_exception_only(etype, evalue)[-1].strip() stacktrace = "".join(traceback.format_exception(etype, evalue, tb)) def _find_last_frame(tb): if not tb: return None while tb.tb_next: tb = tb.tb_next return tb err_locals, err_module, frame = None, None, _find_last_frame(tb) if frame: err_module = "{}:{}".format( frame.tb_frame.f_globals.get( "__name__", frame.tb_frame.f_code.co_filename ), frame.tb_lineno, ) err_locals = OrderedDict(sorted(frame.tb_frame.f_locals.items())) err_locals = try_( lambda: pformat(err_locals), try_(lambda: str(err_locals)) ) def _find_widget_frame(tb): while tb: if isinstance(tb.tb_frame.f_locals.get("self"), OWWidget): return tb tb = tb.tb_next widget_module, widget, frame = None, None, _find_widget_frame(tb) if frame: widget = frame.tb_frame.f_locals["self"].__class__ widget_module = "{}:{}".format(widget.__module__, frame.tb_lineno) packages = ", ".join(sorted(get_installed_distributions())) machine_id = QSettings().value("error-reporting/machine-id", "", type=str) # If this exact error was already reported in this session, # just warn about it if (err_module, widget_module) in cls._cache: QMessageBox( QMessageBox.Warning, "Error Encountered", "Error encountered{}:<br><br><tt>{}</tt>".format( " in widget <b>{}</b>".format(widget.name) if widget else "", stacktrace.replace("\n", "<br>").replace(" ", " "), ), QMessageBox.Ignore, ).exec() return F = cls.DataField data = OrderedDict() data[F.EXCEPTION] = exception data[F.MODULE] = err_module if widget: data[F.WIDGET_NAME] = widget.name data[F.WIDGET_MODULE] = widget_module if canvas: fd, filename = mkstemp(prefix="ows-", suffix=".ows.xml") os.close(fd) # Prevent excepthook printing the same exception when # canvas tries to instantiate the broken widget again with patch("sys.excepthook", lambda *_: None), open(filename, "wb") as f: scheme = canvas.current_document().scheme() try: scheme.save_to(f, pretty=True, pickle_fallback=True) except Exception: pass data[F.WIDGET_SCHEME] = filename with open(filename, encoding="utf-8") as f: data["_" + F.WIDGET_SCHEME] = f.read() data[F.VERSION] = VERSION_STR data[F.ENVIRONMENT] = "Python {} on {} {} {} {}".format( platform.python_version(), platform.system(), platform.release(), platform.version(), platform.machine(), ) data[F.INSTALLED_PACKAGES] = packages data[F.MACHINE_ID] = machine_id or str(uuid.getnode()) data[F.STACK_TRACE] = stacktrace if err_locals: data[F.LOCALS] = err_locals cls(data=data).exec()
def report_abstract_model(model, view=None): columns = [i for i in range(model.columnCount()) if not view or not view.isColumnHidden(i)] rows = [i for i in range(model.rowCount()) if not view or not view.isRowHidden(i)] has_horizontal_header = (try_(lambda: not view.horizontalHeader().isHidden()) or try_(lambda: not view.header().isHidden())) has_vertical_header = try_(lambda: not view.verticalHeader().isHidden()) def item_html(row, col): def data(role=Qt.DisplayRole, orientation=Qt.Horizontal if row is None else Qt.Vertical): if row is None or col is None: return model.headerData(col if row is None else row, orientation, role) return model.data(model.index(row, col), role) selected = (view.selectionModel().isSelected(model.index(row, col)) if view and row is not None and col is not None else False) fgcolor = data(Qt.ForegroundRole) fgcolor = (QBrush(fgcolor).color().name() if isinstance(fgcolor, (QBrush, QColor)) else 'black') bgcolor = data(Qt.BackgroundRole) bgcolor = (QBrush(bgcolor).color().name() if isinstance(bgcolor, (QBrush, QColor)) else 'transparent') if bgcolor.lower() == '#ffffff': bgcolor = 'transparent' font = data(Qt.FontRole) weight = 'font-weight: bold;' if font and font.bold() else '' alignment = data(Qt.TextAlignmentRole) or Qt.AlignLeft halign = ('left' if alignment & Qt.AlignLeft else 'right' if alignment & Qt.AlignRight else 'center') valign = ('top' if alignment & Qt.AlignTop else 'bottom' if alignment & Qt.AlignBottom else 'middle') return ('<{tag} style="' 'color:{fgcolor};' 'border:{border};' 'background:{bgcolor};' '{weight}' 'text-align:{halign};' 'vertical-align:{valign};">{text}</{tag}>'.format( tag='th' if row is None or col is None else 'td', border='1px solid black' if selected else '0', text=data() or '', weight=weight, fgcolor=fgcolor, bgcolor=bgcolor, halign=halign, valign=valign)) stream = [] if has_horizontal_header: stream.append('<tr>') if has_vertical_header: stream.append('<th></th>') stream.extend(item_html(None, col) for col in columns) stream.append('</tr>') for row in rows[:row_limit]: stream.append('<tr>') if has_vertical_header: stream.append(item_html(row, None)) stream.extend(item_html(row, col) for col in columns) stream.append('</tr>') return ''.join(stream)
def handle_exception(cls, exc): etype, evalue, tb = exc exception = traceback.format_exception_only(etype, evalue)[-1].strip() stacktrace = ''.join(traceback.format_exception(etype, evalue, tb)) def _find_last_frame(tb): if not tb: return None while tb.tb_next: tb = tb.tb_next return tb err_locals, err_module, frame = None, None, _find_last_frame(tb) if frame: err_module = '{}:{}'.format( frame.tb_frame.f_globals.get( '__name__', frame.tb_frame.f_code.co_filename), frame.tb_lineno) err_locals = OrderedDict(sorted(frame.tb_frame.f_locals.items())) err_locals = try_(lambda: pformat(err_locals), try_(lambda: str(err_locals))) def _find_widget_frame(tb): while tb: if isinstance(tb.tb_frame.f_locals.get('self'), OWWidget): return tb tb = tb.tb_next widget_module = widget_class = widget = workflow = None frame = _find_widget_frame(tb) if frame is not None: widget = frame.tb_frame.f_locals['self'] # type: OWWidget widget_class = widget.__class__ widget_module = '{}:{}'.format(widget_class.__module__, frame.tb_lineno) if widget is not None: try: workflow = widget.signalManager.parent() if not isinstance(workflow, Scheme): raise TypeError except Exception: workflow = None packages = ', '.join(sorted(get_installed_distributions())) machine_id = QSettings().value('error-reporting/machine-id', '', type=str) # If this exact error was already reported in this session, # just warn about it if (err_module, widget_module) in cls._cache: QMessageBox( QMessageBox.Warning, 'Error Encountered', 'Error encountered{}:<br><br><tt>{}</tt>'.format( (' in widget <b>{}</b>'.format(widget_class.name) if widget_class else ''), stacktrace.replace('\n', '<br>').replace(' ', ' ')), QMessageBox.Ignore).exec() return F = cls.DataField data = OrderedDict() data[F.EXCEPTION] = exception data[F.MODULE] = err_module if widget_class is not None: data[F.WIDGET_NAME] = widget_class.name data[F.WIDGET_MODULE] = widget_module if workflow is not None: fd, filename = mkstemp(prefix='ows-', suffix='.ows.xml') os.close(fd) with open(filename, "wb") as f: try: workflow.save_to(f, pretty=True, pickle_fallback=True) except Exception: pass data[F.WIDGET_SCHEME] = filename with open(filename, encoding='utf-8') as f: data['_' + F.WIDGET_SCHEME] = f.read() data[F.VERSION] = VERSION_STR data[F.ENVIRONMENT] = 'Python {} on {} {} {} {}'.format( platform.python_version(), platform.system(), platform.release(), platform.version(), platform.machine()) data[F.INSTALLED_PACKAGES] = packages data[F.MACHINE_ID] = machine_id or str(uuid.getnode()) data[F.STACK_TRACE] = stacktrace if err_locals: data[F.LOCALS] = err_locals cls(data=data).exec()
def test_try_(self): self.assertTrue(try_(lambda: np.ones(3).any())) self.assertFalse(try_(lambda: np.whatever())) self.assertEqual(try_(len, default=SOMETHING), SOMETHING)
def handle_exception(cls, exc): (etype, evalue, tb), canvas = exc exception = traceback.format_exception_only(etype, evalue)[-1].strip() stacktrace = ''.join(traceback.format_exception(etype, evalue, tb)) def _find_last_frame(tb): if not tb: return None while tb.tb_next: tb = tb.tb_next return tb err_locals, err_module, frame = None, None, _find_last_frame(tb) if frame: err_module = '{}:{}'.format( frame.tb_frame.f_globals.get('__name__', frame.tb_frame.f_code.co_filename), frame.tb_lineno) err_locals = OrderedDict(sorted(frame.tb_frame.f_locals.items())) err_locals = try_(lambda: pformat(err_locals), try_(lambda: str(err_locals))) def _find_widget_frame(tb): while tb: if isinstance(tb.tb_frame.f_locals.get('self'), OWWidget): return tb tb = tb.tb_next widget_module, widget, frame = None, None, _find_widget_frame(tb) if frame: widget = frame.tb_frame.f_locals['self'].__class__ widget_module = '{}:{}'.format(widget.__module__, frame.tb_lineno) packages = ', '.join(sorted(get_installed_distributions())) machine_id = QSettings().value('error-reporting/machine-id', '', type=str) # If this exact error was already reported in this session, # just warn about it if (err_module, widget_module) in cls._cache: QMessageBox(QMessageBox.Warning, 'Error Encountered', 'Error encountered{}:<br><br><tt>{}</tt>'.format( ' in widget <b>{}</b>'.format(widget.name) if widget else '', stacktrace.replace('\n', '<br>').replace(' ', ' ')), QMessageBox.Ignore).exec() return F = cls.DataField data = OrderedDict() data[F.EXCEPTION] = exception data[F.MODULE] = err_module if widget: data[F.WIDGET_NAME] = widget.name data[F.WIDGET_MODULE] = widget_module if canvas: fd, filename = mkstemp(prefix='ows-', suffix='.ows.xml') os.close(fd) # Prevent excepthook printing the same exception when # canvas tries to instantiate the broken widget again with patch('sys.excepthook', lambda *_: None), \ open(filename, "wb") as f: scheme = canvas.current_document().scheme() try: scheme.save_to(f, pretty=True, pickle_fallback=True) except Exception: pass data[F.WIDGET_SCHEME] = filename with open(filename, encoding='utf-8') as f: data['_' + F.WIDGET_SCHEME] = f.read() data[F.VERSION] = VERSION_STR data[F.ENVIRONMENT] = 'Python {} on {} {} {} {}'.format( platform.python_version(), platform.system(), platform.release(), platform.version(), platform.machine()) data[F.INSTALLED_PACKAGES] = packages data[F.MACHINE_ID] = machine_id or str(uuid.getnode()) data[F.STACK_TRACE] = stacktrace if err_locals: data[F.LOCALS] = err_locals cls(data=data).exec()