From b5c3c43522e0abf55fbef31d5ab4d130d71a7fbb Mon Sep 17 00:00:00 2001 From: Matthew Pitkin Date: Mon, 27 Nov 2017 13:59:49 +0000 Subject: [PATCH] Fix use of ads module to only get references for returned pulsars rather than trying to get them all --- docs/source/conf.py | 3 +++ docs/source/query.rst | 1 - psrqpy/__init__.py | 2 +- psrqpy/search.py | 23 ++++++++++++++++++++--- psrqpy/utils.py | 18 +++++++++++++----- 5 files changed, 37 insertions(+), 10 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index eeb47f3d..3e28e045 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -41,6 +41,9 @@ 'sphinx.ext.napoleon' ] +autodoc_default_flags = ['members', 'show-inheritance'] +autodoc_member_order = 'bysource' + # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] diff --git a/docs/source/query.rst b/docs/source/query.rst index 01163bbc..28eb8bb7 100644 --- a/docs/source/query.rst +++ b/docs/source/query.rst @@ -4,4 +4,3 @@ Querying .. automodule:: psrqpy.search :members: - diff --git a/psrqpy/__init__.py b/psrqpy/__init__.py index 56c88646..92b81572 100644 --- a/psrqpy/__init__.py +++ b/psrqpy/__init__.py @@ -2,7 +2,7 @@ """ A Python tool for interacting with the ATNF pulsar catalogue """ -__version__ = "0.3.5" +__version__ = "0.3.6" from .search import QueryATNF from .pulsar import Pulsar, Pulsars diff --git a/psrqpy/search.py b/psrqpy/search.py index fbd1bda1..a4393f73 100644 --- a/psrqpy/search.py +++ b/psrqpy/search.py @@ -132,7 +132,7 @@ def __init__(self, params=None, condition=None, psrtype=None, assoc=None, bincom # get references is required if self._include_refs: - self._refs = get_references(useads=self._adsref) + self._refs = get_references() # perform query self._query_content = self.generate_query() @@ -390,8 +390,25 @@ def parse_query(self, requestcontent=''): refstring2 = re.sub(r'\s+', ' ', refstring.format(**thisref)) # remove any superfluous whitespace self._query_output[p+'_REF'][idx] = ','.join([a for a in refstring2.split(',') if a.strip()]) # remove any superfluous empty ',' seperated values - if self._adsref and 'ADS URL' in thisref: - self._query_output[p+'_REFURL'][idx] = thisref['ADS URL'] # remove any superfluous whitespace + if self._adsref: + if 'ADS URL' not in thisref: # get ADS reference + try: + import ads + except ImportError: + warnings.warn('Could not import ADS module, so no ADS information will be included', UserWarning) + article = [] + + try: + article = ads.SearchQuery(year=thisref['year'], first_author=thisref['authors'][0], title=thisref['title']) + except IOError: + warnings.warn('Could not get reference information, so no ADS information will be included', UserWarning) + article = [] + + article = list(article) + if len(article) > 0: + self._refs[reftag]['ADS URL'] = ADS_URL.format(list(article)[0].bibcode) + + self._query_output[p+'_REFURL'][idx] = thisref['ADS URL'] else: warnings.warn('Reference tag "{}" not found so omitting reference'.format(reftag), UserWarning) vidx += 1 diff --git a/psrqpy/utils.py b/psrqpy/utils.py index b949eca7..158358d0 100644 --- a/psrqpy/utils.py +++ b/psrqpy/utils.py @@ -61,7 +61,7 @@ def get_references(useads=False): queryrefs = requests.get(ATNF_BASE_URL + 'psrcat_ref.html') if queryrefs.status_code != 200: - warnings.warn("Could query the ATNF references. No references returned".format(ATNF_VERSION), UserWarning) + warnings.warn("Could query the ATNF references. No references returned", UserWarning) else: try: refsoup = BeautifulSoup(queryrefs.content, 'html.parser') @@ -231,12 +231,20 @@ def get_references(useads=False): warnings.warn('Could not import ADS module, so no ADS information will be included', UserWarning) continue + refs[reftag]['ADS'] = None + refs[reftag]['ADS URL'] = '' + try: - article = list(ads.SearchQuery(year=refs[reftag]['year'], first_author=refs[reftag]['authors'][0], title=refs[reftag]['title']))[0] - refs[reftag]['ADS'] = article - refs[reftag]['ADS URL'] = ADS_URL.format(article.bibcode) + article = ads.SearchQuery(year=refs[reftag]['year'], first_author=refs[reftag]['authors'][0], title=refs[reftag]['title']) except IOError: - warnings.warn('Could not import ADS module, so no ADS information will be included', UserWarning) + warnings.warn('Could not get reference information, so no ADS information will be included', UserWarning) + continue + + article = list(article) + + if len(article) > 0: + refs[reftag]['ADS'] = list(article)[0] + refs[reftag]['ADS URL'] = ADS_URL.format(list(article)[0].bibcode) return refs