Skip to content

Commit

Permalink
Change recommended config item access in template
Browse files Browse the repository at this point in the history
Previously the template core module read the values of the configuration
items at class creation and stored them in class attributes. This meant
that changing the configuration items at runtime had no effect.

Now the template implements private properties that return the current
configuration item value, or the class (or instance) attribute value if
the user has changed it. This enables the users to use the full
functionality of the `astropy` configuration system, but users not
familiar with it can still override it entirely through the attributes.
  • Loading branch information
eerovaher committed Mar 28, 2022
1 parent 621a13f commit cbd4900
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions astroquery/template_module/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,20 @@ class TemplateClass(BaseQuery):
cases, new methods may be added if necessary, follow the guidelines at
<http://astroquery.readthedocs.io/en/latest/api.html>
"""
# use the Configuration Items imported from __init__.py to set the URL,
# TIMEOUT, etc.
URL = conf.server
TIMEOUT = conf.timeout

# The private properties defined here allow the users to change the
# configuration values at runtime, or to completely override them with
# instance attributes.
URL = '' # A falsy default that cannot be mistaken for a valid value.
TIMEOUT = None # Use `None` if the falsy value could be valid.

@property
def _url(self):
return self.URL or conf.server

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

# all query methods are implemented with an "async" method that handles
# making the actual HTTP request and returns the raw HTTP response, which
Expand Down Expand Up @@ -124,8 +134,8 @@ def query_object_async(self, object_name, get_query_payload=False,
return request_payload
# BaseQuery classes come with a _request method that includes a
# built-in caching system
response = self._request('GET', self.URL, params=request_payload,
timeout=self.TIMEOUT, cache=cache)
response = self._request('GET', self._url, params=request_payload,
timeout=self._timeout, cache=cache)
return response

# For services that can query coordinates, use the query_region method.
Expand Down Expand Up @@ -169,8 +179,8 @@ def query_region_async(self, coordinates, radius, height, width,
width)
if get_query_payload:
return request_payload
response = self._request('GET', self.URL, params=request_payload,
timeout=self.TIMEOUT, cache=cache)
response = self._request('GET', self._url, params=request_payload,
timeout=self._timeout, cache=cache)
return response

# as we mentioned earlier use various python regular expressions, etc
Expand Down Expand Up @@ -294,9 +304,9 @@ def get_image_list(self, coordinates, radius, get_query_payload=False,
request_payload = self._args_to_payload(coordinates, radius)
if get_query_payload:
return request_payload
response = self._request(method="GET", url=self.URL,
response = self._request(method="GET", url=self._url,
data=request_payload,
timeout=self.TIMEOUT, cache=cache)
timeout=self._timeout, cache=cache)

return self.extract_image_urls(response.text)

Expand Down

0 comments on commit cbd4900

Please sign in to comment.