Skip to content

Commit

Permalink
Edit Domain: add custom format
Browse files Browse the repository at this point in the history
  • Loading branch information
ajdapretnar committed Jul 5, 2024
1 parent 910d625 commit ffb1981
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions Orange/widgets/data/oweditdomain.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"""
from __future__ import annotations

import re
import warnings
from xml.sax.saxutils import escape
from itertools import zip_longest, repeat, chain, groupby
Expand Down Expand Up @@ -1526,14 +1528,22 @@ class TimeVariableEditor(VariableEditor):
def __init__(self, parent=None, **kwargs):
super().__init__(parent, **kwargs)
form = self.layout().itemAt(0)
self.custom_text = ""
self.have_date = 0
self.have_time = 0

self.format_cb = QComboBox()
for item, data in [("Detect automatically", (None, 1, 1))] + list(
Orange.data.TimeVariable.ADDITIONAL_FORMATS.items()
):
) + [("Custom format", (self.custom_text, self.have_time,
self.have_date))]:
self.format_cb.addItem(item, StrpTime(item, *data))
self.format_cb.currentIndexChanged.connect(self.variable_changed)
self.custom_edit = QLineEdit()
self.custom_edit.setPlaceholderText("%Y-%m-%d %H:%M:%S")
self.custom_edit.textChanged.connect(self._on_custom_change)
form.insertRow(2, "Format:", self.format_cb)
form.insertRow(3, "Custom format:", self.custom_edit)

def set_data(self, var, transform=()):
super().set_data(var, transform)
Expand All @@ -1555,6 +1565,18 @@ def get_data(self):
tr.insert(0, self.format_cb.currentData())
return var, tr

def _on_custom_change(self):
self.custom_text = self.custom_edit.text()
date_pat = r"%(-?)d|%(b|B)|%(-?)m|%(y|Y)|%(-?)j|%(-?)U|%(-?)W|%(a|A)|%w"
time_pat = r"%(-?)H|%(-?)I|%p|%(-?)M|%(-?)S|%f"
pattern1 = re.compile(date_pat)
pattern2 = re.compile(time_pat)
self.have_date = int(bool(pattern1.search(self.custom_text)))
self.have_time = int(bool(pattern2.search(self.custom_text)))
if self.format_cb.currentText() != "Custom format":
self.format_cb.setCurrentIndex(-1)
self.format_cb.emit()

Check warning on line 1578 in Orange/widgets/data/oweditdomain.py

View check run for this annotation

Codecov / codecov/patch

Orange/widgets/data/oweditdomain.py#L1569-L1578

Added lines #L1569 - L1578 were not covered by tests


def variable_icon(var):
# type: (Union[Variable, Type[Variable], ReinterpretTransform]) -> QIcon
Expand Down Expand Up @@ -3080,4 +3102,5 @@ def column_str_repr_string(


if __name__ == "__main__": # pragma: no cover
WidgetPreview(OWEditDomain).run(Orange.data.Table("iris"))
WidgetPreview(OWEditDomain).run(Orange.data.Table(
"/Users/ajda/Desktop/test.csv"))

0 comments on commit ffb1981

Please sign in to comment.