Skip to content

Commit 29c5bca

Browse files
authored
Merge pull request #4539 from ales-erjavec/fixes/timevariable-parse
[FIX] TimeVariable.parse: Do not modify _ISO_FORMATS
2 parents cd7a21f + 0f62649 commit 29c5bca

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

Orange/data/variable.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ class TimeVariable(ContinuousVariable):
882882
_all_vars = {}
883883
TYPE_HEADERS = ('time', 't')
884884
UNIX_EPOCH = datetime(1970, 1, 1)
885-
_ISO_FORMATS = [
885+
_ISO_FORMATS = (
886886
# have_date, have_time, format_str
887887
# in order of decreased probability
888888
(1, 1, '%Y-%m-%d %H:%M:%S%z'),
@@ -917,7 +917,10 @@ class TimeVariable(ContinuousVariable):
917917
# so these two lines must be in this order
918918
(1, 0, '%Y-%m'),
919919
(1, 0, '%Y-%j'),
920-
]
920+
)
921+
# Order in which `_ISO_FORMATS` are tried. Must never change order of
922+
# last 2 items. Only modified via assignment in `parse`.
923+
__ISO_FORMATS_PROBE_SEQ = list(range(len(_ISO_FORMATS)))
921924
# The regex that matches all above formats
922925
REGEX = (r'^('
923926
r'\d{1,4}-\d{2}-\d{2}([ T]\d{2}:\d{2}(:\d{2}(\.\d+)?([+-]\d{4})?)?)?|'
@@ -1007,17 +1010,20 @@ def parse(self, datestr):
10071010
except ValueError:
10081011
raise self.InvalidDateTimeFormatError(datestr)
10091012

1010-
for i, (have_date, have_time, fmt) in enumerate(self._ISO_FORMATS):
1013+
try_order = self.__ISO_FORMATS_PROBE_SEQ
1014+
for i, (have_date, have_time, fmt) in enumerate(
1015+
map(self._ISO_FORMATS.__getitem__, try_order)):
10111016
try:
10121017
dt = datetime.strptime(datestr, fmt)
10131018
except ValueError:
10141019
continue
10151020
else:
1016-
# Pop this most-recently-used format to front
1017-
if 0 < i < len(self._ISO_FORMATS) - 2:
1018-
self._ISO_FORMATS[i], self._ISO_FORMATS[0] = \
1019-
self._ISO_FORMATS[0], self._ISO_FORMATS[i]
1020-
1021+
# Pop this most-recently-used format index to front,
1022+
# excluding last 2
1023+
if 0 < i < len(try_order) - 2:
1024+
try_order = try_order.copy()
1025+
try_order[i], try_order[0] = try_order[0], try_order[i]
1026+
TimeVariable.__ISO_FORMATS_PROBE_SEQ = try_order
10211027
self.have_date |= have_date
10221028
self.have_time |= have_time
10231029
if not have_date:

0 commit comments

Comments
 (0)