Skip to content

Commit

Permalink
Various changes to make the code python 3.X compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpitkin committed Nov 28, 2017
1 parent b5c3c43 commit 3931b9e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion psrqpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

""" A Python tool for interacting with the ATNF pulsar catalogue """

__version__ = "0.3.6"
__version__ = "0.4.1"

from .search import QueryATNF
from .pulsar import Pulsar, Pulsars
Expand Down
8 changes: 4 additions & 4 deletions psrqpy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
}

# just return the parameter names
PSR_GENERAL_PARS = PSR_GENERAL.keys()
PSR_GENERAL_PARS = list(PSR_GENERAL.keys())

# timing solution and profile parameters
PSR_TIMING = {'P0': {'ref': True, 'err': True, 'units': 's', 'format': 'f8'}, # Barycentric period of the pulsar (s)
Expand All @@ -79,7 +79,7 @@
'S2000': {'ref': True, 'err': True, 'units': 'mJy', 'format': 'f8'} # Mean flux density at 2000 MHz (mJy)
}

PSR_TIMING_PARS = PSR_TIMING.keys()
PSR_TIMING_PARS = list(PSR_TIMING.keys())

# binary system parameters
PSR_BINARY = {'BINARY': {'ref': True, 'err': False, 'units': None, 'format': 'S5'}, # Binary model (usually one of several recognised by the pulsar timing programs TEMPO or TEMPO2). Modified versions of standard models are often used - refer to the source paper for details of the binary model used.
Expand All @@ -96,7 +96,7 @@
'BINCOMP': {'ref': True, 'err': False, 'units': None, 'format': 'S4'} # Companion type
}

PSR_BINARY_PARS = PSR_BINARY.keys()
PSR_BINARY_PARS = list(PSR_BINARY.keys())

# derived parameters
PSR_DERIVED = {'R_LUM': {'ref': False, 'err': False, 'units': 'mJy kpc^2', 'format': 'f8'}, # Radio luminosity at 400 MHz (mJy kpc2)
Expand All @@ -113,7 +113,7 @@
'B_LC': {'ref': False, 'err': False, 'units': 'G', 'format': 'f8'} # Magnetic field at light cylinder (gauss)
}

PSR_DERIVED_PARS = PSR_DERIVED.keys()
PSR_DERIVED_PARS = list(PSR_DERIVED.keys())

# a list of all allowed parameters for querying
PSR_ALL = dict(itertools.chain(PSR_GENERAL.items(), PSR_TIMING.items(), PSR_BINARY.items(), PSR_DERIVED.items()))
Expand Down
13 changes: 7 additions & 6 deletions psrqpy/pulsar.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from __future__ import print_function, division

import warnings
import six

from six import string_types, iteritems

from .config import PSR_ALL_PARS, PSR_ALL
from .utils import get_version
Expand All @@ -28,7 +29,7 @@ def __init__(self, psrname, version=None, **kwargs):
self._version = version if not version else get_version()
self._ephemeris = None

for key, value in six.iteritems(kwargs):
for key, value in iteritems(kwargs):
setattr(self, key, value)

def __repr__(self):
Expand All @@ -51,7 +52,7 @@ def items(self):
"""
Return a list of the class attribute values
"""
return [value for key, value in six.iteritems(self.__dict__) if key in PSR_ALL_PARS+[par+'_ERR' for par in PSR_ALL_PARS]]
return [value for key, value in iteritems(self.__dict__) if key in PSR_ALL_PARS+[par+'_ERR' for par in PSR_ALL_PARS]]

@property
def name(self):
Expand Down Expand Up @@ -185,7 +186,7 @@ def set_ephemeris(self, ephem=None):
if not self._ephemeris and ephem:
self._ephemeris = ephem # set ephemeris if it doesn't already exist

assert isinstance(self._ephemeris, basestring), 'Ephemeris must be a string'
assert isinstance(self._ephemeris, string_types), 'Ephemeris must be a string'

# get ephemeris values
ephemvals = [ev.split() for ev in ephem.split('\n') if len(ev.split()) > 1]
Expand Down Expand Up @@ -336,7 +337,7 @@ def remove_pulsar(self, psrname):
psrname (str): a string with the name of a pulsar
"""

assert isinstance(psrname, basestring), 'psrname is not a string'
assert isinstance(psrname, string_types), 'psrname is not a string'

if psrname in self._psrs:
del self._psrs[psrname]
Expand All @@ -348,7 +349,7 @@ def pop(self, psrname):
Args:
psrname (str): a string with the name of a pulsar
"""
assert isinstance(psrname, basestring), 'psrname is not a string'
assert isinstance(psrname, string_types), 'psrname is not a string'

if psrname in self._psrs:
return self._psrs.pop(psrname)
Expand Down
30 changes: 16 additions & 14 deletions psrqpy/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import warnings
from collections import OrderedDict
import re
import cPickle as pickle
import six

from six.moves import cPickle as pickle
from six import string_types

import numpy as np
import requests
from bs4 import BeautifulSoup
Expand Down Expand Up @@ -107,12 +109,12 @@ def __init__(self, params=None, condition=None, psrtype=None, assoc=None, bincom
raise Exception("No parameters in list")

for p in params:
if not isinstance(p, basestring):
if not isinstance(p, string_types):
raise Exception("Non-string value '{}' found in params list".format(p))

self._query_params = [p.upper() for p in params] # make sure parameter names are all upper case
else:
if isinstance(params, basestring):
if isinstance(params, string_types):
self._query_params = [params.upper()] # make sure parameter is all upper case
else:
if self._psrs and self._get_ephemeris: # if getting ephemerides then param can be None
Expand Down Expand Up @@ -211,7 +213,7 @@ def generate_query(self, version='', params=None, condition='', sortorder='asc',
query_dict['version'] = self._atnf_version

if params:
if isinstance(params, basestring):
if isinstance(params, string_types):
params = [params] # convert to list
else:
if not isinstance(params, list):
Expand All @@ -236,7 +238,7 @@ def generate_query(self, version='', params=None, condition='', sortorder='asc',
query_dict['sortattr'] = self._sort_attr

if psrnames:
if isinstance(psrnames, basestring):
if isinstance(psrnames, string_types):
self._psrs = [psrnames] # convert to list
else:
if not isinstance(psrnames, list):
Expand All @@ -245,7 +247,7 @@ def generate_query(self, version='', params=None, condition='', sortorder='asc',

qpulsars = '' # pulsar name query string
if self._psrs is not None:
if isinstance(self._psrs, basestring):
if isinstance(self._psrs, string_types):
self._psrs = [self._psrs] # if a string pulsar name then convert to list

for psr in self._psrs:
Expand Down Expand Up @@ -555,7 +557,7 @@ def parse_conditions(self, condition, psrtype=None, assoc=None, bincomp=None, ex
if not condition:
conditionparse = ''
else:
if not isinstance(condition, basestring):
if not isinstance(condition, string_types):
warnings.warn('Condition "{}" must be a string. No condition being set'.format(condition), UserWarning)
return ''

Expand Down Expand Up @@ -602,11 +604,11 @@ def parse_conditions(self, condition, psrtype=None, assoc=None, bincomp=None, ex
raise Exception("No pulsar types in list")

for p in psrtype:
if not isinstance(p, basestring):
if not isinstance(p, string_types):
raise Exception("Non-string value '{}' found in pulsar type list".format(p))
self._query_psr_types = psrtype
else:
if isinstance(psrtype, basestring):
if isinstance(psrtype, string_types):
self._query_psr_types = [psrtype]
else:
raise Exception("'psrtype' must be a list or string")
Expand All @@ -628,11 +630,11 @@ def parse_conditions(self, condition, psrtype=None, assoc=None, bincomp=None, ex
raise Exception("No pulsar types in list")

for p in assoc:
if not isinstance(p, basestring):
if not isinstance(p, string_types):
raise Exception("Non-string value '{}' found in associations list".format(p))
self._query_assocs = assoc
else:
if isinstance(assoc, basestring):
if isinstance(assoc, string_types):
self._query_assocs = [assoc]
else:
raise Exception("'assoc' must be a list or string")
Expand All @@ -654,11 +656,11 @@ def parse_conditions(self, condition, psrtype=None, assoc=None, bincomp=None, ex
raise Exception("No pulsar types in list")

for p in bincomp:
if not isinstance(p, basestring):
if not isinstance(p, string_types):
raise Exception("Non-string value '{}' found in binary companions list".format(p))
self._query_bincomps = bincomp
else:
if isinstance(bincomp, basestring):
if isinstance(bincomp, string_types):
self._query_bincomps = [bincomp]
else:
raise Exception("'bincomp' must be a list or string")
Expand Down Expand Up @@ -772,7 +774,7 @@ def ppdot(self, intrinsicpdot=False, excludeGCs=False, showtypes=[], showGCs=Fal
if intrinsicpdot and 'P1_I' not in self._query_params:
self._query_params.append('P1_I')

if isinstance(showtypes, basestring):
if isinstance(showtypes, string_types):
nshowtypes = [showtypes]
else:
nshowtypes = showtypes
Expand Down
7 changes: 4 additions & 3 deletions psrqpy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import datetime
import numpy as np
import requests

from bs4 import BeautifulSoup

from six import string_types

from .config import ATNF_BASE_URL, ATNF_VERSION, ADS_URL

# problematic references that are hard to parse
Expand Down Expand Up @@ -117,7 +118,7 @@ def get_references(useads=False):
'Sov. Astron. Lett.': 'SvAL',
'ATel.': 'ATel'}

if isinstance(rdf, basestring): # only run on string values
if isinstance(rdf, string_types): # only run on string values
rdfs = re.sub(r'\s+', ' ', rdf) # make sure only single spaces are present
for js in journalsubs:
if js in rdfs:
Expand Down Expand Up @@ -160,7 +161,7 @@ def get_references(useads=False):
if '&' in sepauthors[-1] or 'and' in sepauthors[-1]: # split any authors that are seperated by an ampersand
lastauthors = [a.strip() for a in re.split(r'& | and ', sepauthors.pop(-1))]
sepauthors = sepauthors + lastauthors
for i in xrange(len(sepauthors)-2):
for i in range(len(sepauthors)-2):
sepauthors[i] += '.' # re-add final full stops where needed
sepauthors[-1] += '.'
else:
Expand Down

0 comments on commit 3931b9e

Please sign in to comment.