def _validate(self): try: Duration(self.duration.text()) except ParameterValueFormatError: self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False) return self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(True)
def get_spec(self): start_datetime = DateTime(self.datetime.dateTime().toString( Qt.ISODate)) duration = Duration(self.duration.text()) start_int = self.start_integer.value() return IntegerSequenceDateTimeConvertSpec(start_datetime, start_int, duration)
def __init__(self, start_datetime, start_int, duration): if not isinstance(start_datetime, DateTime): start_datetime = DateTime(start_datetime) if not isinstance(duration, Duration): duration = Duration(duration) self.start_datetime = start_datetime self.start_int = start_int self.duration = duration
def __init__(self, parent=None): from ..ui.duration_editor import Ui_DurationEditor # pylint: disable=import-outside-toplevel super().__init__(parent) self._value = Duration(duration_to_relativedelta("1 hour")) self._ui = Ui_DurationEditor() self._ui.setupUi(self) self._ui.duration_edit.editingFinished.connect(self._change_duration) self._ui.duration_edit.setText(str(self._value))
class DurationEditor(QWidget): """ An editor widget for Duration type parameter values. Attributes: parent (QWidget): a parent widget """ def __init__(self, parent=None): from ..ui.duration_editor import Ui_DurationEditor super().__init__(parent) self._value = Duration(duration_to_relativedelta("1 hour")) self._ui = Ui_DurationEditor() self._ui.setupUi(self) self._ui.duration_edit.editingFinished.connect(self._change_duration) self._ui.duration_edit.setText(self._value.to_text()) @Slot(name="_change_duration") def _change_duration(self): """Updates the value being edited.""" text = self._ui.duration_edit.text() tokens = text.split(',') try: durations = [ duration_to_relativedelta(token.strip()) for token in tokens ] except ParameterValueFormatError: self._ui.duration_edit.setText(self._value.to_text()) return self._value = Duration(durations) def set_value(self, value): """Sets the value for editing.""" self._value = value self._ui.duration_edit.setText(self._value.to_text()) def value(self): """Returns the current Duration.""" return self._value
def test_setData_expands_empty_table(self): model = MapModel(Map([], [], Duration)) self.assertEqual(model.rowCount(), 1) self.assertEqual(model.columnCount(), 1) self.assertEqual(model.index(0, 0).data(), "") self.assertTrue(model.setData(model.index(0, 0), Duration("1 month"))) self.assertEqual(model.rowCount(), 2) self.assertEqual(model.columnCount(), 3) self.assertEqual(model.index(0, 0).data(), "1M") self.assertEqual(model.index(0, 1).data(), None) self.assertEqual(model.index(0, 2).data(), "") self.assertEqual(model.index(1, 0).data(), "") self.assertEqual(model.index(1, 1).data(), "") self.assertEqual(model.index(1, 2).data(), "")
def test_setData_expands_rows(self): model = MapModel(Map([Duration("1 month")], [1.1])) self.assertEqual(model.rowCount(), 2) self.assertEqual(model.columnCount(), 3) self.assertTrue(model.setData(model.index(1, 1), 2.2)) self.assertEqual(model.rowCount(), 3) self.assertEqual(model.columnCount(), 3) self.assertEqual(model.index(0, 0).data(), "1M") self.assertEqual(model.index(0, 1).data(), 1.1) self.assertEqual(model.index(0, 2).data(), "") self.assertEqual(model.index(1, 0).data(), "1M") self.assertEqual(model.index(1, 1).data(), 2.2) self.assertEqual(model.index(1, 2).data(), "") self.assertEqual(model.index(2, 0).data(), "") self.assertEqual(model.index(2, 1).data(), "") self.assertEqual(model.index(2, 2).data(), "")
def value_to_convert_spec(value): if isinstance(value, ConvertSpec): return value if isinstance(value, str): spec = { "datetime": DateTimeConvertSpec, "duration": DurationConvertSpec, "float": FloatConvertSpec, "string": StringConvertSpec, }.get(value) return spec() if isinstance(value, dict): start_datetime = DateTime(value.get("start_datetime")) duration = Duration(value.get("duration")) start_int = value.get("start_int") return IntegerSequenceDateTimeConvertSpec(start_datetime, start_int, duration) raise TypeError( f"value must be str or dict instead got {type(value).__name__}")
def _read_pasted_text(text): """ Parses a given CSV table Args: text (str): a CSV table Returns: list of list: a list of table rows """ data = list() with io.StringIO(text) as input_stream: reader = csv.reader(input_stream, delimiter='\t') for row in reader: data_row = list() for cell in row: try: number = locale.atof(cell) data_row.append(number) continue except ValueError: pass try: # Try parsing Duration before DateTime because DateTime will happily accept strings like '1h' value = Duration(cell) data_row.append(value) continue except SpineDBAPIError: pass try: value = DateTime(cell) data_row.append(value) continue except SpineDBAPIError: pass try: value = from_database(cell) data_row.append(value) continue except ParameterValueFormatError: pass data_row.append(cell) data.append(data_row) return data
def test_editor_sets_duration_in_parent_model(self): duration = Duration(duration_to_relativedelta('3 months')) self._check_parent_model_updated_when_closed(duration)
def test_initial_value(self): editor = DurationEditor() value = editor.value() self.assertEqual(value, Duration("1h"))
def test_Duration(self): self.assertEqual(DurationConvertSpec().convert_function()("1h"), Duration("1h"))
def test_duration_in_display_role(self): value = Duration("3Y") self.db_mngr.get_item.return_value = {"value": to_database(value)} formatted = self.get_value(Qt.DisplayRole) self.assertEqual(formatted, "3Y")
def test_duration_in_edit_role(self): value = Duration("2M") self.db_mngr.get_item.return_value = {"value": to_database(value)} formatted = self.get_value(Qt.EditRole) self.assertEqual(formatted, to_database(value))
def test_duration_in_tool_tip_role(self): value = Duration("13D") self.db_mngr.get_item.return_value = {"value": to_database(value)} self.assertIsNone(self.get_value(Qt.ToolTipRole))
def test_setData(self): map_value = Map(["a"], [1.1]) model = MapModel(map_value) self.assertTrue(model.setData(model.index(0, 0), Duration("1 month"))) self.assertEqual(model.index(0, 0).data(), "1M")
def test_value_access_single_duration(self): editor = DurationEditor() editor.set_value(Duration("3 months")) self.assertEqual(editor.value(), Duration("3M"))
def test_value_access_variable_duration(self): editor = DurationEditor() editor.set_value(Duration(["3 months", "6 months"])) self.assertEqual(editor.value(), Duration(["3M", "6M"]))