Skip to content

Commit

Permalink
Add change log entry for astropy#2292
Browse files Browse the repository at this point in the history
  • Loading branch information
eerovaher committed Jul 27, 2022
1 parent b1fcfff commit 035188a
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 44 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ oac
- Fix bug in parsing events that contain html tags (e.g. in their alias
field). [#2423]

simbad
^^^^^^

- It is now possible to apply changes through the configuration system at
runtime. However, if the corresponding class or instance variables have been
specified then they still take precedence. [#2292]

svo_fps
^^^^^^^

Expand Down
50 changes: 31 additions & 19 deletions astroquery/simbad/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ class SimbadClass(SimbadBaseQuery):
(http://simbad.u-strasbg.fr/simbad/sim-help?Page=sim-url)
"""
SIMBAD_URL = 'http://' + conf.server + '/simbad/sim-script'
TIMEOUT = conf.timeout
SIMBAD_URL = ''
TIMEOUT = None
WILDCARDS = {
'*': 'Any string of characters (including an empty one)',
'?': 'Any character (exactly one character)',
Expand All @@ -293,7 +293,7 @@ class SimbadClass(SimbadBaseQuery):
'query_bibobj_async': 'query bibobj'
}

ROW_LIMIT = conf.row_limit
ROW_LIMIT = None

# also find a way to fetch the votable fields table from
# <http://simbad.u-strasbg.fr/simbad/sim-help?Page=sim-fscript#VotableFields>
Expand All @@ -305,6 +305,18 @@ def __init__(self):
super().__init__()
self._VOTABLE_FIELDS = copy.copy(self._VOTABLE_FIELDS)

@property
def _row_limit(self):
return conf.row_limit if self.ROW_LIMIT is None else self.ROW_LIMIT

@property
def _simbad_url(self):
return self.SIMBAD_URL or f'https://{conf.server}/simbad/sim-script'

@property
def _timeout(self):
return conf.timeout if self.TIMEOUT is None else self.TIMEOUT

def list_wildcards(self):
"""
Displays the available wildcards that may be used in Simbad queries and
Expand Down Expand Up @@ -513,8 +525,8 @@ def query_criteria_async(self, *args, **kwargs):

request_payload = self._args_to_payload(caller='query_criteria_async',
*args, **kwargs)
response = self._request("POST", self.SIMBAD_URL, data=request_payload,
timeout=self.TIMEOUT, cache=cache)
response = self._request("POST", self._simbad_url, data=request_payload,
timeout=self._timeout, cache=cache)
return response

def query_object(self, object_name, wildcard=False, verbose=False,
Expand Down Expand Up @@ -577,8 +589,8 @@ def query_object_async(self, object_name, wildcard=False, cache=True,
if get_query_payload:
return request_payload

response = self._request("POST", self.SIMBAD_URL, data=request_payload,
timeout=self.TIMEOUT, cache=cache)
response = self._request("POST", self._simbad_url, data=request_payload,
timeout=self._timeout, cache=cache)
return response

def query_objects(self, object_names, wildcard=False, verbose=False,
Expand Down Expand Up @@ -715,8 +727,8 @@ def query_region_async(self, coordinates, radius=2*u.arcmin,
if get_query_payload:
return request_payload

response = self._request("POST", self.SIMBAD_URL, data=request_payload,
timeout=self.TIMEOUT, cache=cache)
response = self._request("POST", self._simbad_url, data=request_payload,
timeout=self._timeout, cache=cache)
return response

def query_catalog(self, catalog, verbose=False, cache=True,
Expand Down Expand Up @@ -772,8 +784,8 @@ def query_catalog_async(self, catalog, cache=True, get_query_payload=False):
if get_query_payload:
return request_payload

response = self._request("POST", self.SIMBAD_URL, data=request_payload,
timeout=self.TIMEOUT, cache=cache)
response = self._request("POST", self._simbad_url, data=request_payload,
timeout=self._timeout, cache=cache)
return response

def query_bibobj(self, bibcode, verbose=False, get_query_payload=False):
Expand Down Expand Up @@ -826,8 +838,8 @@ def query_bibobj_async(self, bibcode, cache=True, get_query_payload=False):
if get_query_payload:
return request_payload

response = self._request("POST", self.SIMBAD_URL, data=request_payload,
timeout=self.TIMEOUT, cache=cache)
response = self._request("POST", self._simbad_url, data=request_payload,
timeout=self._timeout, cache=cache)
return response

def query_bibcode(self, bibcode, wildcard=False, verbose=False,
Expand Down Expand Up @@ -894,8 +906,8 @@ def query_bibcode_async(self, bibcode, wildcard=False, cache=True,
if get_query_payload:
return request_payload

response = self._request("POST", self.SIMBAD_URL, cache=cache,
data=request_payload, timeout=self.TIMEOUT)
response = self._request("POST", self._simbad_url, data=request_payload,
timeout=self._timeout, cache=cache)

return response

Expand Down Expand Up @@ -950,8 +962,8 @@ def query_objectids_async(self, object_name, cache=True,
if get_query_payload:
return request_payload

response = self._request("POST", self.SIMBAD_URL, data=request_payload,
timeout=self.TIMEOUT, cache=cache)
response = self._request("POST", self._simbad_url, data=request_payload,
timeout=self._timeout, cache=cache)

return response

Expand Down Expand Up @@ -988,8 +1000,8 @@ def _args_to_payload(self, *args, **kwargs):
votable_header = self._get_query_header(get_raw)
votable_footer = self._get_query_footer(get_raw)

if self.ROW_LIMIT > 0:
script = "set limit " + str(self.ROW_LIMIT)
if self._row_limit > 0:
script = "set limit " + str(self._row_limit)
script = "\n".join([script, votable_header, command])
using_wildcard = False
if kwargs.get('wildcard'):
Expand Down
22 changes: 22 additions & 0 deletions astroquery/simbad/tests/test_simbad.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,28 @@ def test_query_criteria2(patch_post):
assert 'otype=SNR' in S._last_query.data['script']


def test_config_runtime_change():
sb = simbad.Simbad()

assert sb._row_limit == 0
with simbad.conf.set_temp('row_limit', 7):
assert sb._row_limit == 7
sb.ROW_LIMIT = 3
assert sb._row_limit == 3

assert sb._simbad_url == 'https://simbad.u-strasbg.fr/simbad/sim-script'
with simbad.conf.set_temp('server', 'simbad.harvard.edu'):
assert sb._simbad_url == 'https://simbad.harvard.edu/simbad/sim-script'
sb.SIMBAD_URL = 'asdfg'
assert sb._simbad_url == 'asdfg'

assert sb._timeout == 60
with simbad.conf.set_temp('timeout', 5):
assert sb._timeout == 5
sb.TIMEOUT = 3
assert sb._timeout == 3


def test_simbad_settings1():
sb = simbad.core.Simbad()
assert sb.get_votable_fields() == ['main_id', 'coordinates']
Expand Down
19 changes: 18 additions & 1 deletion astroquery/simbad/tests/test_simbad_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import astropy.units as u
from astropy.table import Table
from astroquery.utils.mocks import MockResponse
from astroquery.simbad import Simbad
from astroquery.simbad import conf, Simbad
# Maybe we need to expose SimbadVOTableResult to be in the public API?
from astroquery.simbad.core import SimbadVOTableResult

Expand All @@ -30,6 +30,23 @@ def fin():
request.addfinalizer(fin)
return my_temp_dir

def test_config_runtime_change(self, temp_dir):
simbad = Simbad()
simbad.cache_location = temp_dir
with conf.set_temp('server', 'simbad.harvard.edu'):
response = simbad.query_bibobj_async('2006AJ....131.1163S')
assert response.url == 'https://simbad.harvard.edu/simbad/sim-script'
response = simbad.query_bibobj_async('2006AJ....131.1163S')
assert response.url == 'https://simbad.u-strasbg.fr/simbad/sim-script'

simbad = Simbad()
with conf.set_temp('row_limit', 4):
result = simbad.query_bibobj('2005A&A.430.165F')
assert len(result) == 4
simbad.ROW_LIMIT = 5
result = simbad.query_bibobj('2005A&A.430.165F')
assert len(result) == 5

def test_query_criteria1(self, temp_dir):
simbad = Simbad()
simbad.cache_location = temp_dir
Expand Down
65 changes: 41 additions & 24 deletions docs/simbad/simbad.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,9 @@ instance to query the ESO catalog:

.. code-block:: python
>>> from astroquery.simbad import Simbad
>>> limitedSimbad = Simbad()
>>> limitedSimbad.ROW_LIMIT = 6
>>> result_table = limitedSimbad.query_catalog('eso')
>>> from astroquery.simbad import conf, Simbad
>>> with conf.set_temp("row_limit", 6):
>>> result_table = Simbad.query_catalog("eso")
>>> print(result_table)
MAIN_ID RA ... COO_WAVELENGTH COO_BIBCODE
Expand Down Expand Up @@ -532,39 +531,57 @@ Customizing the default settings
================================

There may be times when you wish to change the defaults that have been set for
the Simbad queries.
the Simbad queries. Items defined through the `astropy configuration system
<https://docs.astropy.org/en/stable/config/index.html>`_ can be modified
persistently by editing the ``astroquery`` configuration file, or for the
duration of the active Python session at runtime. For every such configuration
item there also exists a corresponding class attribute, which provides an
alternative way of changing the defaults, but if one of those attributes is
modified then the corresponding configuration item becomes inactive.

Changing the row limit
----------------------


To fetch all the rows in the result, the row limit must be set to 0. However for some
queries, results are likely to be very large, in such cases it may be best to
limit the rows to a smaller number. If you want to do this only for the current
python session then:

.. code-block:: python
By default the number of rows in the query results is not limited (done by
specifying the limit to be 0). However, for some queries the results can be
very large. In such cases it may be useful to set a limit to the number of
rows. This can be done through the :attr:`~astroquery.simbad.Conf.row_limit`
configuration item, or the corresponding
:attr:`~astroquery.simbad.SimbadClass.ROW_LIMIT` class attribute::

>>> from astroquery.simbad import conf
>>> with conf.set_temp("row_limit", 10):
... # Row limit is set to 10 within this code block.
... pass
>>> from astroquery.simbad import Simbad
>>> Simbad.ROW_LIMIT = 15 # now any query fetches at most 15 rows
If you would like to make your choice persistent, then you can do this by
modifying the setting in the Astroquery configuration file.
>>> # Setting the row limit to 15 for the instance.
>>> Simbad.ROW_LIMIT = 15

Changing the timeout
--------------------

The timeout is the time limit in seconds for establishing the connection with
the Simbad server and by default it is set to 60 seconds. It can be modified
through the :attr:`~astroquery.simbad.Conf.timeout` configuration item, or the
corresponding :attr:`~astroquery.simbad.SimbadClass.TIMEOUT` class attribute.

The timeout is the time limit in seconds for establishing connection with the
Simbad server and by default it is set to 100 seconds. You may want to modify
this - again you can do this at run-time if you want to adjust it only for the
current session. To make it persistent, you must modify the setting in the
Astroquery configuration file.
Changing the server
-------------------

.. code-block:: python
By default all queries are sent to ``simbad.u-strasbg.fr``, but it is also
possible to connect to the mirror at ``simbad.harvard.edu`` instead. This can
be specified through the :attr:`~astroquery.simbad.Conf.server` configuration
item::

>>> from astroquery.simbad import conf
>>> conf.server = "simbad.u-strasbg.fr"

The corresponding class attribute is
:attr:`~astroquery.simbad.SimbadClass.SIMBAD_URL`, and differently from the
configuration item it requires the entire URL to be specified::

>>> from astroquery.simbad import Simbad
>>> Simbad.TIMEOUT = 60 # sets the timeout to 60s
>>> Simbad.SIMBAD_URL = "http://simbad.u-strasbg.fr/simbad/sim-script"

Specifying which VOTable fields to include in the result
--------------------------------------------------------
Expand Down

0 comments on commit 035188a

Please sign in to comment.