Skip to content

Commit 373081b

Browse files
committed
implementing review suggestions
1 parent ad6b910 commit 373081b

File tree

4 files changed

+66
-62
lines changed

4 files changed

+66
-62
lines changed

astroquery/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ def _get_bibtex():
4545
class Cache_Conf(_config.ConfigNamespace):
4646

4747
cache_timeout = _config.ConfigItem(
48-
604800, # 1 week
49-
'Astroquery-wide cache timeout (seconds).',
48+
604800,
49+
'Astroquery-wide cache timeout (seconds). Default is 1 week (604800).',
5050
cfgtype='integer'
5151
)
5252

astroquery/query.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,14 @@ def from_cache(self, cache_location, cache_timeout):
119119
else:
120120
current_time = datetime.utcnow()
121121
cache_time = datetime.utcfromtimestamp(request_file.stat().st_mtime)
122-
expired = ((current_time-cache_time) > timedelta(seconds=cache_timeout))
122+
expired = current_time-cache_time > timedelta(seconds=cache_timeout)
123123
if not expired:
124124
with open(request_file, "rb") as f:
125125
response = pickle.load(f)
126126
if not isinstance(response, requests.Response):
127127
response = None
128128
else:
129-
log.debug("Cache expired for {0}...".format(request_file))
129+
log.debug(f"Cache expired for {request_file}...")
130130
response = None
131131
except FileNotFoundError:
132132
response = None
@@ -251,7 +251,7 @@ def clear_cache(self):
251251

252252
def _request(self, method, url,
253253
params=None, data=None, headers=None,
254-
files=None, save=False, savedir='', timeout=None, cache=True,
254+
files=None, save=False, savedir='', timeout=None, cache=None,
255255
stream=False, auth=None, continuation=True, verify=True,
256256
allow_redirects=True,
257257
json=None, return_response_on_save=False):
@@ -311,7 +311,8 @@ def _request(self, method, url,
311311
is True.
312312
"""
313313

314-
cache &= cache_conf.cache_active
314+
if cache is None: # Global caching not overridden
315+
cache = cache_conf.cache_active
315316

316317
if save:
317318
local_filename = url.split('/')[-1]
@@ -334,7 +335,7 @@ def _request(self, method, url,
334335
else:
335336
query = AstroQuery(method, url, params=params, data=data, headers=headers,
336337
files=files, timeout=timeout, json=json)
337-
if (self.cache_location is None) or (not cache):
338+
if not cache:
338339
with cache_conf.set_temp("cache_active", False):
339340
response = query.request(self._session, stream=stream,
340341
auth=auth, verify=verify,
@@ -488,10 +489,10 @@ def __init__(self, obj=None):
488489
self.original_cache_setting = cache_conf.cache_active
489490

490491
def __enter__(self):
491-
conf.cache_active = False
492+
cache_conf.cache_active = False
492493

493494
def __exit__(self, exc_type, exc_value, traceback):
494-
conf.cache_active = self.original_cache_setting
495+
cache_conf.cache_active = self.original_cache_setting
495496
return False
496497

497498

@@ -547,7 +548,7 @@ def _login(self, *args, **kwargs):
547548
pass
548549

549550
def login(self, *args, **kwargs):
550-
with conf.set_temp("cache_active", False):
551+
with cache_conf.set_temp("cache_active", False):
551552
self._authenticated = self._login(*args, **kwargs)
552553
return self._authenticated
553554

astroquery/tests/test_cache.py

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from astropy.config import paths
99

1010
from astroquery.query import QueryWithLogin
11-
from astroquery import conf
11+
from astroquery import cache_conf
1212

1313
URL1 = "http://fakeurl.edu"
1414
URL2 = "http://fakeurl.ac.uk"
@@ -33,7 +33,7 @@ def get_mockreturn(url, *args, **kwargs):
3333
requests.Session.request = get_mockreturn
3434

3535

36-
class TestClass(QueryWithLogin):
36+
class CacheTestClass(QueryWithLogin):
3737
"""Bare bones class for testing caching"""
3838

3939
def test_func(self, requrl):
@@ -42,45 +42,40 @@ def test_func(self, requrl):
4242

4343
def _login(self, username):
4444

45-
resp = self._request(method="GET", url=username)
46-
47-
if resp.content == "Penguin":
48-
return True
49-
else:
50-
return False
45+
return self._request(method="GET", url=username).content == "Penguin"
5146

5247

5348
def test_conf():
54-
conf.reset()
49+
cache_conf.reset()
5550

56-
default_timeout = conf.cache_timeout
57-
default_active = conf.cache_active
51+
default_timeout = cache_conf.cache_timeout
52+
default_active = cache_conf.cache_active
5853

5954
assert default_timeout == 604800
6055
assert default_active is True
6156

62-
with conf.set_temp("cache_timeout", 5):
63-
assert conf.cache_timeout == 5
57+
with cache_conf.set_temp("cache_timeout", 5):
58+
assert cache_conf.cache_timeout == 5
6459

65-
with conf.set_temp("cache_active", False):
66-
assert conf.cache_active is False
60+
with cache_conf.set_temp("cache_active", False):
61+
assert cache_conf.cache_active is False
6762

68-
assert conf.cache_timeout == default_timeout
69-
assert conf.cache_active == default_active
63+
assert cache_conf.cache_timeout == default_timeout
64+
assert cache_conf.cache_active == default_active
7065

71-
conf.cache_timeout = 5
72-
conf.cache_active = False
73-
conf.reset()
66+
cache_conf.cache_timeout = 5
67+
cache_conf.cache_active = False
68+
cache_conf.reset()
7469

75-
assert conf.cache_timeout == default_timeout
76-
assert conf.cache_active == default_active
70+
assert cache_conf.cache_timeout == default_timeout
71+
assert cache_conf.cache_active == default_active
7772

7873

7974
def test_basic_caching():
80-
conf.reset()
75+
cache_conf.reset()
8176

82-
mytest = TestClass()
83-
assert conf.cache_active
77+
mytest = CacheTestClass()
78+
assert cache_conf.cache_active
8479

8580
mytest.clear_cache()
8681
assert len(os.listdir(mytest.cache_location)) == 0
@@ -108,35 +103,35 @@ def test_basic_caching():
108103
assert resp.content == TEXT2 # Now get new response
109104

110105

111-
def test_change_location(tmpdir):
112-
conf.reset()
106+
def test_change_location(tmp_path):
107+
cache_conf.reset()
113108

114-
mytest = TestClass()
109+
mytest = CacheTestClass()
115110
default_cache_location = mytest.cache_location
116111

117112
assert paths.get_cache_dir() in str(default_cache_location)
118113
assert "astroquery" in mytest.cache_location.parts
119114
assert mytest.name in mytest.cache_location.parts
120115

121-
new_loc = "new_dir"
116+
new_loc = tmp_path.joinpath("new_dir")
122117
mytest.cache_location = new_loc
123-
assert str(mytest.cache_location) == new_loc
118+
assert mytest.cache_location == new_loc
124119

125120
mytest.reset_cache_location()
126121
assert mytest.cache_location == default_cache_location
127122

128-
Path(new_loc).mkdir(parents=True, exist_ok=True)
123+
new_loc.mkdir(parents=True, exist_ok=True)
129124
with paths.set_temp_cache(new_loc):
130-
assert new_loc in mytest.cache_location.parts
125+
assert str(new_loc) in str(mytest.cache_location)
131126
assert "astroquery" in mytest.cache_location.parts
132127
assert mytest.name in mytest.cache_location.parts
133128

134129

135130
def test_login():
136-
conf.reset()
131+
cache_conf.reset()
137132

138-
mytest = TestClass()
139-
assert conf.cache_active
133+
mytest = CacheTestClass()
134+
assert cache_conf.cache_active
140135

141136
mytest.clear_cache()
142137
assert len(os.listdir(mytest.cache_location)) == 0
@@ -154,15 +149,15 @@ def test_login():
154149

155150

156151
def test_timeout():
157-
conf.reset()
152+
cache_conf.reset()
158153

159-
mytest = TestClass()
160-
assert conf.cache_active
154+
mytest = CacheTestClass()
155+
assert cache_conf.cache_active
161156

162157
mytest.clear_cache()
163158
assert len(os.listdir(mytest.cache_location)) == 0
164159

165-
conf.cache_timeout = 2 # Set to 2 sec so we can reach timeout easily
160+
cache_conf.cache_timeout = 2 # Set to 2 sec so we can reach timeout easily
166161

167162
set_response(TEXT1) # setting the response
168163

@@ -180,10 +175,10 @@ def test_timeout():
180175

181176

182177
def test_deactivate():
183-
conf.reset()
178+
cache_conf.reset()
184179

185-
mytest = TestClass()
186-
conf.cache_active = False
180+
mytest = CacheTestClass()
181+
cache_conf.cache_active = False
187182

188183
mytest.clear_cache()
189184
assert len(os.listdir(mytest.cache_location)) == 0
@@ -200,10 +195,10 @@ def test_deactivate():
200195
assert resp.content == TEXT2
201196
assert len(os.listdir(mytest.cache_location)) == 0
202197

203-
conf.reset()
204-
assert conf.cache_active is True
198+
cache_conf.reset()
199+
assert cache_conf.cache_active is True
205200

206-
with conf.set_temp('cache_active', False):
201+
with cache_conf.set_temp('cache_active', False):
207202
mytest.clear_cache()
208203
assert len(os.listdir(mytest.cache_location)) == 0
209204

@@ -219,4 +214,4 @@ def test_deactivate():
219214
assert resp.content == TEXT2
220215
assert len(os.listdir(mytest.cache_location)) == 0
221216

222-
assert conf.cache_active is True
217+
assert cache_conf.cache_active is True

docs/index.rst

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,39 +176,47 @@ uncomment the relevant configuration item(s), and insert your desired value(s).
176176
Caching
177177
-------
178178

179-
By default Astroquery employs query caching with a timeout of 1 week, although individual services
180-
may have different settings. The user can clear their cache at any time, as well as suspend cache usage,
179+
By default Astroquery employs query caching with a timeout of 1 week.
180+
The user can clear their cache at any time, as well as suspend cache usage,
181181
and change the cache location. Caching persists between Astroquery sessions.
182182
If you know the service you are using has released new data recently, or if you believe you are
183183
not recieving the newest data, try clearing the cache.
184184

185185

186186
The Astroquery cache location is divided by service, so each service's cache should be managed invidually,
187187
however whether the cache is active and the expiration time are controlled centrally through the
188-
astroquery ``conf`` module. Astroquery uses the Astropy configuration infrastructure, information about
188+
astroquery ``cache_conf`` module. Astroquery uses the Astropy configuration infrastructure, information about
189189
temporarily or permanently changing configuration values can be found
190190
`here <https://docs.astropy.org/en/latest/config/index.html>`_.
191191

192192
Shown here are the cache properties, using Simbad as an example:
193193

194194
.. code-block:: python
195195
196-
>>> from astroquery import conf
196+
>>> from astroquery import cache_conf
197197
>>> from astroquery.simbad import Simbad
198198
199199
>>> # Is the cache active?
200-
>>> print(conf.cache_active)
200+
>>> print(cache_conf.cache_active)
201201
True
202202
203203
>>> # Cache timout in seconds
204-
>>> print(conf.cache_timeout)
204+
>>> print(cache_conf.cache_timeout)
205205
604800
206206
207207
>>> # Cache location
208208
>>> print(Simbad.cache_location)
209209
/Users/username/.astropy/cache/astroquery/Simbad
210210
211211
212+
To clear the cache:
213+
214+
.. code-block:: python
215+
216+
>>> Simbad.clear_cache()
217+
218+
219+
212220
213221
Available Services
214222
==================

0 commit comments

Comments
 (0)