def setData(self, index, value, role=Qt.EditRole): """Cell content change""" if not index.isValid() or self.readonly: return False i = index.row() j = index.column() value = from_qvariant(value, str) dtype = self._data.dtype.name if dtype == "bool": try: val = bool(float(value)) except ValueError: val = value.lower() == "true" elif dtype.startswith("string") or dtype.startswith("bytes"): val = to_binary_string(value, 'utf8') elif dtype.startswith("unicode") or dtype.startswith("str"): val = to_text_string(value) else: if value.lower().startswith('e') or value.lower().endswith('e'): return False try: val = complex(value) if not val.imag: val = val.real except ValueError as e: QMessageBox.critical(self.dialog, "Error", "Value error: %s" % str(e)) return False try: self.test_array[0] = val # will raise an Exception eventually except OverflowError as e: print("OverflowError: " + str(e)) # spyder: test-skip QMessageBox.critical(self.dialog, "Error", "Overflow error: %s" % str(e)) return False # Add change to self.changes self.changes[(i, j)] = val self.dataChanged.emit(index, index) if not is_string(val): val = self.color_func(val) if val > self.vmax: self.vmax = val if val < self.vmin: self.vmin = val return True
def to_unicode_from_fs(string): """ Return a unicode version of string decoded using the file system encoding. """ if not is_string(string): # string is a QString string = to_text_string(string.toUtf8(), 'utf-8') else: if is_binary_string(string): try: unic = string.decode(FS_ENCODING) except (UnicodeError, TypeError): pass else: return unic return string
def write(self, text, flush=False, error=False, prompt=False): """Simulate stdout and stderr""" if prompt: self.flush() if not is_string(text): # This test is useful to discriminate QStrings from decoded str text = to_text_string(text) self.__buffer.append(text) ts = time.time() if flush or prompt: self.flush(error=error, prompt=prompt) elif ts - self.__timestamp > 0.05: self.flush(error=error) self.__timestamp = ts # Timer to flush strings cached by last write() operation in series self.__flushtimer.start(50)
def setData(self, index, value, role=Qt.EditRole): """Cell content change""" if not index.isValid() or self.readonly: return False i = index.row() j = index.column() value = from_qvariant(value, str) dtype = self._data.dtype.name if dtype == "bool": try: val = bool(float(value)) except ValueError: val = value.lower() == "true" elif dtype.startswith("string") or dtype.startswith("bytes"): val = to_binary_string(value, 'utf8') elif dtype.startswith("unicode") or dtype.startswith("str"): val = to_text_string(value) else: if value.lower().startswith('e') or value.lower().endswith('e'): return False try: val = complex(value) if not val.imag: val = val.real except ValueError as e: QMessageBox.critical(self.dialog, "Error", "Value error: %s" % str(e)) return False try: self.test_array[0] = val # will raise an Exception eventually except OverflowError as e: print("OverflowError: " + str(e)) # spyder: test-skip QMessageBox.critical(self.dialog, "Error", "Overflow error: %s" % str(e)) return False # Add change to self.changes self.changes[(i, j)] = val self.dataChanged.emit(index, index) if not is_string(val): if val > self.vmax: self.vmax = val if val < self.vmin: self.vmin = val return True
def text_to_qcolor(text): """ Create a QColor from specified string Avoid warning from Qt when an invalid QColor is instantiated """ color = QColor() if not is_string(text): # testing for QString (PyQt API#1) text = str(text) if not is_text_string(text): return color if text.startswith('#') and len(text)==7: correct = '#0123456789abcdef' for char in text: if char.lower() not in correct: return color elif text not in list(QColor.colorNames()): return color color.setNamedColor(text) return color
def connect_client_to_kernel(self, client, is_cython=False, **kwargs): # kwargs for is_pylab, is_sympy """Connect a client to its kernel Copied and modified from spyder.plugins.ipythonconsole.IPythonConsole """ ipycon = self.main.ipyconsole connection_file = client.connection_file if ipycon.test_no_stderr: stderr_handle = None else: stderr_handle = client.stderr_handle km, kc = self.create_kernel_manager_and_kernel_client( connection_file, stderr_handle, is_cython=is_cython, **kwargs) # An error occurred if this is True if is_string(km) and kc is None: client.shellwidget.kernel_manager = None client.show_kernel_error(km) return # This avoids a recurrent, spurious NameError when running our # tests in our CIs if not ipycon.testing: kc.started_channels.connect( lambda c=client: self.process_started(c)) kc.stopped_channels.connect( lambda c=client: self.process_finished(c)) kc.start_channels(shell=True, iopub=True) shellwidget = client.shellwidget shellwidget.set_kernel_client_and_manager(kc, km) shellwidget.sig_exception_occurred.connect( self.main.console.handle_exception)
def connect_client_to_kernel(self, client, is_cython=False): """Connect a client to its kernel Copied and modified from spyder.plugins.ipythonconsole.IPythonConsole """ ipyconsole = self.main.ipyconsole connection_file = client.connection_file if ipyconsole.test_no_stderr: stderr_file_or_handle = None else: if spyder.version_info < (3, 3, 2): stderr_file_or_handle = client.stderr_file else: stderr_file_or_handle = client.stderr_handle km, kc = self.create_kernel_manager_and_kernel_client( connection_file, stderr_file_or_handle, is_cython=is_cython) # An error occurred if this is True if is_string(km) and kc is None: client.shellwidget.kernel_manager = None client.show_kernel_error(km) return kc.started_channels.connect(lambda c=client: self.process_started(c)) kc.stopped_channels.connect(lambda c=client: self.process_finished(c)) kc.start_channels(shell=True, iopub=True) shellwidget = client.shellwidget if spyder.version_info > (4, ): shellwidget.set_kernel_client_and_manager(kc, km) shellwidget.sig_exception_occurred.connect( self.main.console.exception_occurred) else: shellwidget.kernel_manager = km shellwidget.kernel_client = kc