Skip to content

Commit 7b63ceb

Browse files
committed
Reorder columns before returning results
1 parent 9fe4d55 commit 7b63ceb

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

astroquery/eso/core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from ..query import QueryWithLogin
3939
from ..utils import schema
4040
from .utils import py2adql, _split_str_as_list_of_str, \
41-
adql_sanitize_val, are_coords_valid
41+
adql_sanitize_val, are_coords_valid, reorder_columns
4242

4343
__doctest_skip__ = ['EsoClass.*']
4444

@@ -301,6 +301,7 @@ def query_tap_service(self,
301301
table_to_return = None
302302
tap_service = self._tap_service(authenticated)
303303
table_to_return = self._try_download_pyvo_table(query_str, tap_service)
304+
table_to_return = reorder_columns(table_to_return)
304305
return table_to_return
305306

306307
@unlimited_max_rec

astroquery/eso/tests/test_eso.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
from astroquery.utils.mocks import MockResponse
2020
from ...eso import Eso
21-
from ...eso.utils import py2adql, adql_sanitize_val
21+
from ...eso.utils import py2adql, adql_sanitize_val, reorder_columns, \
22+
DEFAULT_LEAD_COLS_RAW
2223
from ...exceptions import NoResultsWarning, MaxResultsWarning
2324

2425
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
@@ -333,6 +334,24 @@ def test_issue_table_length_warnings():
333334
eso_instance._maybe_warn_about_table_length(t)
334335

335336

337+
def test_reorder_columns(monkeypatch):
338+
eso = Eso()
339+
monkeypatch.setattr(eso, 'query_tap_service', monkey_tap)
340+
table = eso.query_main(target='SGR A', object='SGR A')
341+
names_before = table.colnames[:]
342+
table2 = reorder_columns(table)
343+
names_after = table2.colnames[:]
344+
345+
assert set(DEFAULT_LEAD_COLS_RAW).issubset(names_before)
346+
assert set(DEFAULT_LEAD_COLS_RAW).issubset(names_after)
347+
assert set(names_before) == set(names_after)
348+
assert table != table2
349+
assert names_before[:5] != names_after[:5]
350+
351+
for n in table.colnames:
352+
assert table[[n]].values_equal(table2[[n]]), n
353+
354+
336355
def test_py2adql():
337356
"""
338357
# Example query:

astroquery/eso/utils.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"""
44

55
from typing import Union, List, Optional
6+
from astropy.table import Table
7+
8+
DEFAULT_LEAD_COLS_RAW = ['object', 'ra', 'dec', 'dp_id', 'date_obs', 'prog_id']
9+
DEFAULT_LEAD_COLS_PHASE3 = ['target_name', 's_ra', 's_dec', 'dp_id', 'date_obs', 'proposal_id']
610

711

812
def _split_str_as_list_of_str(column_str: str):
@@ -13,6 +17,27 @@ def _split_str_as_list_of_str(column_str: str):
1317
return column_list
1418

1519

20+
def reorder_columns(table: Table,
21+
leading_cols: Optional[List[str]] = None):
22+
"""
23+
Reorders the columns of the pased table so that the
24+
colums given by the list leading_cols are first.
25+
If no leading cols are passed, it defaults to
26+
['object', 'ra', 'dec', 'dp_id', 'date_obs']
27+
Returns a table with the columns reordered.
28+
"""
29+
leading_cols = leading_cols or DEFAULT_LEAD_COLS_RAW
30+
first_cols = []
31+
last_cols = table.colnames[:]
32+
for x in leading_cols:
33+
if x in last_cols:
34+
last_cols.remove(x)
35+
first_cols.append(x)
36+
last_cols = first_cols + last_cols
37+
table = table[last_cols]
38+
return table
39+
40+
1641
def adql_sanitize_val(x):
1742
"""
1843
If the value is a string, put it into single quotes

0 commit comments

Comments
 (0)