Skip to content

Commit

Permalink
🐛 Don't cache dict in bibdatabase (#348)
Browse files Browse the repository at this point in the history
Attention: Multiple calls to get_entries_dict() now lead to multiple creations of the dict. For huge databases, one may thus want to avoid doing that.
  • Loading branch information
MiWeiss committed Jan 3, 2023
1 parent 2d6c9ce commit ce64adf
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions bibtexparser/bibdatabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,15 @@ def entry_sort_key(entry, fields):
result.append(str(entry.get(field, '')).lower()) # Sorting always as string
return tuple(result)

def _make_entries_dict(self):
for entry in self.entries:
self._entries_dict[entry['ID']] = entry

def get_entry_dict(self):
"""Return a dictionary of BibTeX entries.
The dict key is the BibTeX entry key
"""Return a dictionary of BibTeX entries, where dict key is the BibTeX entry key.
This method re-creates the dict every time it is called,
hence subsequent calls should be avoided with large databases.
"""
# If the hash has never been made, make it
if not self._entries_dict:
self._make_entries_dict()
self._entries_dict = dict()
for entry in self.entries:
self._entries_dict[entry['ID']] = entry
return self._entries_dict

entries_dict = property(get_entry_dict)
Expand Down

0 comments on commit ce64adf

Please sign in to comment.