From cbd4900c92bdf122c066a2349b78626eba0b283f Mon Sep 17 00:00:00 2001 From: Eero Vaher Date: Mon, 28 Mar 2022 19:15:11 +0200 Subject: [PATCH 1/3] Change recommended config item access in template 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. --- astroquery/template_module/core.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/astroquery/template_module/core.py b/astroquery/template_module/core.py index d7a7811cad..ac14263df7 100644 --- a/astroquery/template_module/core.py +++ b/astroquery/template_module/core.py @@ -41,10 +41,20 @@ class TemplateClass(BaseQuery): cases, new methods may be added if necessary, follow the guidelines at """ - # 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 @@ -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. @@ -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 @@ -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) From 5137807fee8967671294e607dfb7c2a4745fabf6 Mon Sep 17 00:00:00 2001 From: Eero Vaher Date: Mon, 28 Mar 2022 19:35:40 +0200 Subject: [PATCH 2/3] Update configuration item names in template The template core module allows users to set configuration items in two ways, either through the `astropy` configuration system or through class or instance attributes. The attribute names are no longer in capital letters because the convention is to reserve such names for constants, and the configuration item names now match the attribute names. --- astroquery/template_module/__init__.py | 2 +- astroquery/template_module/core.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/astroquery/template_module/__init__.py b/astroquery/template_module/__init__.py index ab6aa76061..d9eb2e702f 100644 --- a/astroquery/template_module/__init__.py +++ b/astroquery/template_module/__init__.py @@ -18,7 +18,7 @@ class Conf(_config.ConfigNamespace): """ Configuration parameters for `astroquery.template_module`. """ - server = _config.ConfigItem( + url = _config.ConfigItem( ['http://dummy_server_mirror_1', 'http://dummy_server_mirror_2', 'http://dummy_server_mirror_n'], diff --git a/astroquery/template_module/core.py b/astroquery/template_module/core.py index ac14263df7..907f34725c 100644 --- a/astroquery/template_module/core.py +++ b/astroquery/template_module/core.py @@ -45,16 +45,16 @@ class TemplateClass(BaseQuery): # 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. + 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 + return self.url or conf.url @property def _timeout(self): - return conf.timeout if self.TIMEOUT is None else self.TIMEOUT + 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 From 1ffb0d9c9eb5adfea9604a96f289bcd1480f434c Mon Sep 17 00:00:00 2001 From: Eero Vaher Date: Mon, 28 Mar 2022 21:26:26 +0200 Subject: [PATCH 3/3] Add `__init__()` to template core module The template now recommends implementing an `__init__()` function that allows the users to conveniently override the `astropy` configuration system. --- astroquery/template_module/core.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/astroquery/template_module/core.py b/astroquery/template_module/core.py index 907f34725c..36dd5be796 100644 --- a/astroquery/template_module/core.py +++ b/astroquery/template_module/core.py @@ -36,17 +36,16 @@ @async_to_sync class TemplateClass(BaseQuery): - """ - Not all the methods below are necessary but these cover most of the common - cases, new methods may be added if necessary, follow the guidelines at - - """ + # `__init__()` allows the user to conveniently set the instance attributes + # to override the configuration items. The default attribute values should + # be falsy (`x` is called falsy if `bool(x)` is `False`) or `None`. + def __init__(self, url='', timeout=None): + self.url = url # A falsy default that cannot be mistaken for a valid value. + self.timeout = timeout # Use `None` as default if the falsy value could be valid. # 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. + # configuration values at runtime if the corresponding instance attributes + # have default values. @property def _url(self): @@ -56,6 +55,12 @@ def _url(self): def _timeout(self): return conf.timeout if self.timeout is None else self.timeout + """ + Not all the methods below are necessary but these cover most of the common + cases, new methods may be added if necessary, follow the guidelines at + + """ + # all query methods are implemented with an "async" method that handles # making the actual HTTP request and returns the raw HTTP response, which # should be parsed by a separate _parse_result method. The query_object @@ -332,7 +337,7 @@ def extract_image_urls(self, html_str): pass -# the default tool for users to interact with is an instance of the Class +# the default tool for users to interact with is a default instance of the Class Template = TemplateClass() # once your class is done, tests should be written