diff --git a/doc/news/describe-perfromance.rst b/doc/news/describe-perfromance.rst new file mode 100644 index 00000000..163ecddf --- /dev/null +++ b/doc/news/describe-perfromance.rst @@ -0,0 +1,3 @@ +**Performance:** + +* Improved speed to return the echemdb description by caching the bibliography data. diff --git a/unitpackage/database/echemdb.py b/unitpackage/database/echemdb.py index e5f2a21d..35b3dc79 100644 --- a/unitpackage/database/echemdb.py +++ b/unitpackage/database/echemdb.py @@ -149,24 +149,24 @@ def bibliography(self): '' """ - from pybtex.database import BibliographyData - - bib_data = BibliographyData( - { - entry.bibliography.key: entry.bibliography - for entry in self - if entry.bibliography - } - ) + from pybtex.database import BibliographyData, parse_string - if isinstance(bib_data, str): - return bib_data + # Parse each unique bibdata string only once; many entries share a publication. + seen_citations = {} + for entry in self: + try: + citation = entry.source.bibdata + except AttributeError: + citation = "" + if citation and citation not in seen_citations: + seen_citations[citation] = parse_string(citation, "bibtex") - # Remove duplicates from the bibliography - bib_data_ = BibliographyData() + if not seen_citations: + return "" - for key, entry in bib_data.entries.items(): - if key not in bib_data_.entries: - bib_data_.add_entry(key, entry) + bib_data_ = BibliographyData() + for parsed in seen_citations.values(): + for key, bib_entry in parsed.entries.items(): + bib_data_.add_entry(key, bib_entry) return bib_data_ diff --git a/unitpackage/database/echemdb_entry.py b/unitpackage/database/echemdb_entry.py index 425ccf44..e9513b34 100644 --- a/unitpackage/database/echemdb_entry.py +++ b/unitpackage/database/echemdb_entry.py @@ -65,6 +65,7 @@ # along with unitpackage. If not, see . # ******************************************************************** import logging +from functools import cached_property from unitpackage.entry import Entry @@ -174,7 +175,7 @@ def from_mpt(cls, csvname, encoding=None): return entry - @property + @cached_property def bibliography(self): r""" Return a pybtex bibliography object associated with this entry.