Skip to content

Commit cb99c1d

Browse files
derek73claude
andcommitted
Simplify join_on_conjunctions bookkeeping
Dedup the conjunction-index shift loop into a shift_conj_index helper, replace the pop()/try-except-IndexError boundary check with an equivalent slice delete, and replace slice-and-rebuild list reconstruction (pieces = pieces[:i] + [x] + pieces[j:]) with in-place slice assignment. No behavior change; full test suite still passes. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 7901046 commit cb99c1d

1 file changed

Lines changed: 14 additions & 19 deletions

File tree

nameparser/parser.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,13 @@ def register_joined_piece(new_piece: str, neighbor: str) -> None:
12801280
# chain onto a following prefix/lastname (see "von und zu")
12811281
self.C.prefixes.add(new_piece)
12821282

1283+
def shift_conj_index(past: int, by: int) -> None:
1284+
# after removing pieces at/after `past`, indices of the
1285+
# remaining conjunctions need to shift down by `by`
1286+
for j, val in enumerate(conj_index):
1287+
if val > past:
1288+
conj_index[j] = val - by
1289+
12831290
for i in conj_index:
12841291
if len(pieces[i]) == 1 and total_length < 4 and pieces[i].isalpha():
12851292
# if there are only 3 total parts (minus known titles, suffixes
@@ -1293,27 +1300,15 @@ def register_joined_piece(new_piece: str, neighbor: str) -> None:
12931300
register_joined_piece(new_piece, pieces[i+1])
12941301
pieces[i] = new_piece
12951302
pieces.pop(i+1)
1296-
# subtract 1 from the index of all the remaining conjunctions
1297-
for j, val in enumerate(conj_index):
1298-
if val > i:
1299-
conj_index[j] = val-1
1303+
shift_conj_index(past=i, by=1)
13001304

13011305
else:
13021306
new_piece = " ".join(pieces[i-1:i+2])
13031307
register_joined_piece(new_piece, pieces[i-1])
13041308
pieces[i-1] = new_piece
1305-
pieces.pop(i)
1306-
rm_count = 2
1307-
try:
1308-
pieces.pop(i)
1309-
except IndexError:
1310-
rm_count = 1
1311-
1312-
# subtract the number of removed pieces from the index
1313-
# of all the remaining conjunctions
1314-
for j, val in enumerate(conj_index):
1315-
if val > i:
1316-
conj_index[j] = val - rm_count
1309+
rm_count = min(2, len(pieces) - i)
1310+
del pieces[i:i+rm_count]
1311+
shift_conj_index(past=i, by=rm_count)
13171312

13181313
# join prefixes to following lastnames: ['de la Vega'], ['van Buren']
13191314
prefixes = list(filter(self.is_prefix, pieces))
@@ -1343,7 +1338,7 @@ def register_joined_piece(new_piece: str, neighbor: str) -> None:
13431338
# if there are two prefixes in sequence, join to the following piece
13441339
j += 1
13451340
new_piece = ' '.join(pieces[i:j])
1346-
pieces = pieces[:i] + [new_piece] + pieces[j:]
1341+
pieces[i:j] = [new_piece]
13471342
except StopIteration:
13481343
try:
13491344
# if there are no more prefixes, look for a suffix to stop at
@@ -1355,12 +1350,12 @@ def register_joined_piece(new_piece: str, neighbor: str) -> None:
13551350
# before the prefix) would be matched instead.
13561351
j = pieces.index(stop_at, i + 1)
13571352
new_piece = ' '.join(pieces[i:j])
1358-
pieces = pieces[:i] + [new_piece] + pieces[j:]
1353+
pieces[i:j] = [new_piece]
13591354
except StopIteration:
13601355
# if there were no suffixes, nothing to stop at so join all
13611356
# remaining pieces
13621357
new_piece = ' '.join(pieces[i:])
1363-
pieces = pieces[:i] + [new_piece]
1358+
pieces[i:] = [new_piece]
13641359

13651360
log.debug("pieces: %s", pieces)
13661361
return pieces

0 commit comments

Comments
 (0)