Skip to content

Commit c3e91e8

Browse files
oscar-LTethanfurman
authored andcommitted
Improve time complexity by using filter()
1 parent 606e29a commit c3e91e8

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

Lib/enum.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,15 +1214,18 @@ def _generate_next_value_(name, start, count, last_values):
12141214
count: the number of existing members
12151215
last_values: the list of values assigned
12161216
"""
1217-
incrementable_last_values = []
1218-
for val in last_values:
1219-
try:
1220-
incrementable_last_values.append(val + 1)
1221-
except TypeError:
1222-
pass
12231217

1224-
if incrementable_last_values:
1225-
return sorted(incrementable_last_values)[-1]
1218+
# Filter funciton to deal with last_values lists of mixed types
1219+
def test_incrementable(n):
1220+
try:
1221+
n + 1
1222+
return True
1223+
except:
1224+
return False
1225+
1226+
checked_last_values = sorted(filter(test_incrementable, last_values))
1227+
if checked_last_values:
1228+
return checked_last_values[-1] + 1
12261229
else:
12271230
return start
12281231

0 commit comments

Comments
 (0)