-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
39 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,10 +72,15 @@ def match_whole_speaker(self, speaker_name, speaker_date): | |
m = re.search("^([^\(]*)(.*)", speaker_name) | ||
first_part = m.group(1).strip() | ||
bracketed_parts = m.group(2).strip() | ||
|
||
ids_from_first_part = memberList.match_string_somehow( | ||
first_part, speaker_date, party, False | ||
) | ||
|
||
if ids_from_first_part is None and bracketed_parts: | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ajparsons
Author
Contributor
|
||
ids_from_first_part = memberList.match_string_somehow( | ||
bracketed_parts, speaker_date, party, False | ||
) | ||
|
||
if ids_from_first_part is None: | ||
return None | ||
else: | ||
|
@@ -356,7 +361,32 @@ def log_speaker(speaker, date, message): | |
fp.write(str(date) + ": [" + message + "] " + speaker + "\n") | ||
|
||
|
||
def get_unique_person_id(tidied_speaker: str, on_date: str): | ||
class IDCache: | ||
def __init__(self): | ||
self.cache = {} | ||
|
||
def check(self, key: str | None) -> None | str: | ||
if key is not None: | ||
return self.cache.get(key, None) | ||
else: | ||
return None | ||
|
||
def set(self, key: str | None, value: str): | ||
if key: | ||
self.cache[key] = value | ||
return value | ||
|
||
|
||
id_cache = IDCache() | ||
|
||
|
||
def get_unique_person_id( | ||
tidied_speaker: str, on_date: str, lookup_key: str | None = None | ||
): | ||
# check we haven't cached this one first | ||
if v := id_cache.check(lookup_key): | ||
return v | ||
|
||
ids = memberList.match_whole_speaker(tidied_speaker, str(on_date)) | ||
if ids is None: | ||
# This special return value (None) indicates that the speaker | ||
|
@@ -367,7 +397,8 @@ def get_unique_person_id(tidied_speaker: str, on_date: str): | |
log_speaker(tidied_speaker, str(on_date), "missing") | ||
return None | ||
elif len(ids) == 1: | ||
return ids[0] | ||
# cache for future lookup | ||
return id_cache.set(lookup_key, ids[0]) | ||
else: | ||
raise Exception( | ||
f"The speaker '{tidied_speaker}' could not be resolved, found: {ids}" | ||
|
Does this then replace the bracketed_parts while loop further down? Would make this fn quite a bit smaller if it did, not sure what that loop is doing though.