Skip to content

Commit 52c0e5c

Browse files
committed
Simplify simbad code
Various small simplifications throughout the module.
1 parent 878ba33 commit 52c0e5c

File tree

1 file changed

+18
-38
lines changed

1 file changed

+18
-38
lines changed

astroquery/simbad/core.py

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,8 @@ def list_votable_fields(self):
343343
fields_dict = json.load(f)
344344

345345
print("Available VOTABLE fields:\n")
346-
for field in list(sorted(fields_dict.keys())):
347-
print("{}".format(field))
346+
for field in sorted(fields_dict.keys()):
347+
print(str(field))
348348
print("For more information on a field:\n"
349349
"Simbad.get_field_description ('field_name') \n"
350350
"Currently active VOTABLE fields:\n {0}"
@@ -405,10 +405,7 @@ def add_votable_fields(self, *args):
405405
os.path.join('data', 'votable_fields_dict.json'))
406406

407407
with open(dict_file, "r") as f:
408-
fields_dict = json.load(f)
409-
fields_dict = dict(
410-
((strip_field(ff), fields_dict[ff])
411-
for ff in fields_dict))
408+
fields_dict = {strip_field(k): v for k, v in json.load(f).items()}
412409

413410
for field in args:
414411
sf = strip_field(field)
@@ -417,34 +414,30 @@ def add_votable_fields(self, *args):
417414
else:
418415
self._VOTABLE_FIELDS.append(field)
419416

420-
def remove_votable_fields(self, *args, **kwargs):
417+
def remove_votable_fields(self, *args, strip_params=False):
421418
"""
422419
Removes the specified field names from ``SimbadClass._VOTABLE_FIELDS``
423420
424421
Parameters
425422
----------
426423
list of field_names to be removed
427-
strip_params: bool
424+
strip_params: bool, optional
428425
If true, strip the specified keywords before removing them:
429426
e.g., ra(foo) would remove ra(bar) if this is True
430427
"""
431-
strip_params = kwargs.pop('strip_params', False)
432-
433428
if strip_params:
434-
sargs = [strip_field(a) for a in args]
429+
sargs = {strip_field(a) for a in args}
435430
sfields = [strip_field(a) for a in self._VOTABLE_FIELDS]
436431
else:
437-
sargs = args
432+
sargs = set(args)
438433
sfields = self._VOTABLE_FIELDS
439-
absent_fields = set(sargs) - set(sfields)
440434

441-
for b, f in list(zip(sfields, self._VOTABLE_FIELDS)):
442-
if b in sargs:
443-
self._VOTABLE_FIELDS.remove(f)
444-
445-
for field in absent_fields:
435+
for field in sargs.difference(sfields):
446436
warnings.warn("{field}: this field is not set".format(field=field))
447437

438+
zipped_fields = zip(sfields, self._VOTABLE_FIELDS)
439+
self._VOTABLE_FIELDS = [f for b, f in zipped_fields if b not in sargs]
440+
448441
# check if all fields are removed
449442
if not self._VOTABLE_FIELDS:
450443
warnings.warn("All fields have been removed. "
@@ -677,7 +670,7 @@ def query_region_async(self, coordinates, radius=2*u.arcmin,
677670
if len(set(frame)) > 1:
678671
raise ValueError("Coordinates have different frames")
679672
else:
680-
frame = set(frame).pop()
673+
frame = frame[0]
681674

682675
if isiterable(radius):
683676
if len(radius) != len(ra):
@@ -941,20 +934,13 @@ def query_objectids_async(self, object_name, cache=True,
941934
return response
942935

943936
def _get_query_header(self, get_raw=False):
944-
votable_fields = ','.join(self.get_votable_fields())
945937
# if get_raw is set then don't fetch as votable
946938
if get_raw:
947939
return ""
948-
votable_def = "votable {" + votable_fields + "}"
949-
votable_open = "votable open"
950-
return "\n".join([votable_def, votable_open])
940+
return "votable {" + ','.join(self.get_votable_fields()) + "}\nvotable open"
951941

952942
def _get_query_footer(self, get_raw=False):
953-
if get_raw:
954-
return ""
955-
votable_close = "votable close"
956-
957-
return votable_close
943+
return "" if get_raw else "votable close"
958944

959945
@validate_epoch_decorator
960946
@validate_equinox_decorator
@@ -989,25 +975,19 @@ def _args_to_payload(self, *args, **kwargs):
989975
kwargs['equi'] = kwargs['equinox']
990976
del kwargs['equinox']
991977
# remove default None from kwargs
992-
# be compatible with python3
993-
for key in list(kwargs):
994-
if not kwargs[key]:
995-
del kwargs[key]
978+
kwargs = {key: value for key, value in kwargs.items() if value is not None}
996979
# join in the order specified otherwise results in error
997980
all_keys = ['radius', 'frame', 'equi', 'epoch']
998981
present_keys = [key for key in all_keys if key in kwargs]
999982
if caller == 'query_criteria_async':
1000-
for k in kwargs:
1001-
present_keys.append(k)
983+
present_keys.extend(kwargs)
1002984
# need ampersands to join args
1003985
args_str = '&'.join([str(val) for val in args])
1004-
if len(args) > 0 and len(present_keys) > 0:
986+
if args and present_keys:
1005987
args_str += " & "
1006988
else:
1007989
args_str = ' '.join([str(val) for val in args])
1008-
kwargs_str = ' '.join("{key}={value}".format(key=key,
1009-
value=kwargs[key])
1010-
for key in present_keys)
990+
kwargs_str = ' '.join(f"{key}={kwargs[key]}" for key in present_keys)
1011991

1012992
# For the record, I feel dirty for writing this wildcard-case hack.
1013993
# This entire function should be refactored when someone has time.

0 commit comments

Comments
 (0)