classification
Title: Lib/_strptime.py: utilize all()
Type: performance Stage: patch review
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: dilyan.palauzov, serhiy.storchaka, terry.reedy
Priority: normal Keywords:

Created on 2018-02-08 16:09 by dilyan.palauzov, last changed 2018-02-11 23:49 by serhiy.storchaka.

Messages (7)
msg311838 - (view) Author: Дилян Палаузов (dilyan.palauzov) Date: 2018-02-08 16:09
diff --git a/Lib/_strptime.py b/Lib/_strptime.py
--- a/Lib/_strptime.py
+++ b/Lib/_strptime.py
@@ -238,10 +238,7 @@ class TimeRE(dict):
 
         """
         to_convert = sorted(to_convert, key=len, reverse=True)
-        for value in to_convert:
-            if value != '':
-                break
-        else:
+        if all(value == '' for value in to_convert):
             return ''
         regex = '|'.join(re_escape(stuff) for stuff in to_convert)
         regex = '(?P<%s>%s' % (directive, regex)
msg311935 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-02-10 04:10
This looks straightforward.  I believe we normally do not backport this type of change.  The tests for _strptime.py are in test_strptime.py, not test__strptime.py.
msg311941 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-02-10 07:08
Just

    if not any(to_convert):
        return ''
msg311965 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-02-10 22:37
And the conditional return can be done before sorting.
msg312016 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-02-11 23:09
Serhiy, Is there any reason to not combine last two lines in one?

-        regex = '(?P<%s>%s' % (directive, regex)
-        return '%s)' % regex
+        return '(?P<%s>%s)' % (directive, regex)

or to use f-string to then get

+        return f'(?P<{directive}>{regex})'
msg312022 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-02-11 23:37
For me, on Windows, the tests for time, datetime, and _strptime have failures on the master branch, so I don't want to submit a PR just now.
msg312024 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-02-11 23:49
I don't know such reason.

I would not combine f-strings and regular expressions. Curly braces are syntactically meaningful in regular expressions, and regular expression which uses them for substituting look strange.
History
Date User Action Args
2018-02-11 23:49:45serhiy.storchakasetmessages: + msg312024
2018-02-11 23:37:02terry.reedysetmessages: + msg312022
2018-02-11 23:09:45terry.reedysetmessages: + msg312016
2018-02-10 22:37:50terry.reedysetmessages: + msg311965
2018-02-10 07:08:53serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg311941
2018-02-10 04:10:03terry.reedysetnosy: + terry.reedy
messages: + msg311935

type: performance
stage: patch review
2018-02-08 16:09:49dilyan.palauzovcreate