Skip to content

Commit 647ff25

Browse files
authored
Merge pull request #789 from keflavich/hash_files
xmatch: bug with caching
2 parents 9c98e64 + a93e0fa commit 647ff25

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

astroquery/query.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,15 @@ def hash(self):
6464
request_key = (self.method, self.url)
6565
for k in (self.params, self.data, self.headers, self.files):
6666
if isinstance(k, dict):
67-
request_key += (tuple(sorted(k.items(),
68-
key=_replace_none_iterable)),)
67+
entry = (tuple(sorted(k.items(),
68+
key=_replace_none_iterable)))
69+
entry = tuple((k_,v_.read()) if hasattr(v_,'read')
70+
else (k_,v_) for k_,v_ in entry)
71+
for k_,v_ in entry:
72+
if hasattr(v_,'read') and hasattr(v_,'seek'):
73+
v_.seek(0)
74+
75+
request_key += entry
6976
elif isinstance(k, tuple) or isinstance(k, list):
7077
request_key += (tuple(sorted(k,
7178
key=_replace_none_iterable)),)

astroquery/xmatch/core.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class XMatchClass(BaseQuery):
1818
TIMEOUT = conf.timeout
1919

2020
def query(self, cat1, cat2, max_distance, colRA1=None, colDec1=None,
21-
colRA2=None, colDec2=None, cache=True):
21+
colRA2=None, colDec2=None, cache=True, get_query_payload=False):
2222
"""
2323
Query the `CDS cross-match service
2424
<http://cdsxmatch.u-strasbg.fr/xmatch>`_ by finding matches between
@@ -58,12 +58,16 @@ def query(self, cat1, cat2, max_distance, colRA1=None, colDec1=None,
5858
Query results table
5959
"""
6060
response = self.query_async(cat1, cat2, max_distance, colRA1, colDec1,
61-
colRA2, colDec2, cache=cache)
61+
colRA2, colDec2, cache=cache,
62+
get_query_payload=get_query_payload)
63+
if get_query_payload:
64+
return response
6265
return ascii.read(response.text, format='csv')
6366

6467
@prepend_docstr_noreturns("\n" + query.__doc__)
6568
def query_async(self, cat1, cat2, max_distance, colRA1=None, colDec1=None,
66-
colRA2=None, colDec2=None, cache=True):
69+
colRA2=None, colDec2=None, cache=True,
70+
get_query_payload=False):
6771
"""
6872
Returns
6973
-------
@@ -83,8 +87,13 @@ def query_async(self, cat1, cat2, max_distance, colRA1=None, colDec1=None,
8387
self._prepare_sending_table(1, payload, kwargs, cat1, colRA1, colDec1)
8488
self._prepare_sending_table(2, payload, kwargs, cat2, colRA2, colDec2)
8589

90+
if get_query_payload:
91+
return payload, kwargs
92+
8693
response = self._request(method='POST', url=self.URL, data=payload,
8794
timeout=self.TIMEOUT, cache=cache, **kwargs)
95+
response.raise_for_status()
96+
8897
return response
8998

9099
def _prepare_sending_table(self, i, payload, kwargs, cat, colRA, colDec):
@@ -101,10 +110,10 @@ def _prepare_sending_table(self, i, payload, kwargs, cat, colRA, colDec):
101110
fp = six.StringIO()
102111
cat.write(fp, format='ascii.csv')
103112
fp.seek(0)
104-
kwargs['files'] = {catstr: fp}
113+
kwargs['files'] = {catstr: ('cat1.csv', fp.read())}
105114
else:
106115
# assume it's a file-like object, support duck-typing
107-
kwargs['files'] = {catstr: cat}
116+
kwargs['files'] = {catstr: ('cat1.csv', cat.read())}
108117
if not self.is_table_available(cat):
109118
if ((colRA is None) or (colDec is None)):
110119
raise ValueError('Specify the name of the RA/Dec columns in' +

astroquery/xmatch/tests/test_xmatch_remote.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22
import os.path
3+
import os
34

45
from astropy.tests.helper import pytest, remote_data
56
from astropy.table import Table
67
from astropy.units import arcsec
8+
from astropy.io import ascii
79

810
from ...xmatch import XMatch
911

@@ -38,7 +40,7 @@ def test_xmatch_is_avail_table(xmatch):
3840

3941
@remote_data
4042
def test_xmatch_query(xmatch):
41-
with open(os.path.join(DATA_DIR, 'posList.csv')) as pos_list:
43+
with open(os.path.join(DATA_DIR, 'posList.csv'), 'r') as pos_list:
4244
table = xmatch.query(
4345
cat1=pos_list, cat2='vizier:II/246/out', max_distance=5 * arcsec,
4446
colRA1='ra', colDec1='dec')
@@ -49,6 +51,8 @@ def test_xmatch_query(xmatch):
4951
'e_Jmag', 'e_Hmag', 'e_Kmag', 'Qfl', 'Rfl', 'X', 'MeasureJD']
5052
assert len(table) == 11
5153

54+
http_test_table = http_test()
55+
assert all(table == http_test_table)
5256

5357
@remote_data
5458
def test_xmatch_query_astropy_table(xmatch):
@@ -63,3 +67,16 @@ def test_xmatch_query_astropy_table(xmatch):
6367
'errHalfMaj', 'errHalfMin', 'errPosAng', 'Jmag', 'Hmag', 'Kmag',
6468
'e_Jmag', 'e_Hmag', 'e_Kmag', 'Qfl', 'Rfl', 'X', 'MeasureJD']
6569
assert len(table) == 11
70+
71+
http_test_table = http_test()
72+
assert all(table == http_test_table)
73+
74+
@remote_data
75+
def http_test():
76+
# this can be used to check that the API is still functional & doing as expected
77+
infile = os.path.join(DATA_DIR, 'posList.csv')
78+
outfile = os.path.join(DATA_DIR, 'http_result.csv')
79+
os.system('curl -X POST -F request=xmatch -F distMaxArcsec=5 -F RESPONSEFORMAT=csv -F cat1=@{1} -F colRA1=ra -F colDec1=dec -F cat2=vizier:II/246/out http://cdsxmatch.u-strasbg.fr/xmatch/api/v1/sync > {0}'.format(outfile,
80+
infile))
81+
table = ascii.read(outfile, format='csv')
82+
return table

0 commit comments

Comments
 (0)