From 154710b6401c5fc81c0941536ef588275e38b71d Mon Sep 17 00:00:00 2001 From: Ryan Lynch Date: Fri, 18 Jan 2019 16:47:50 -0500 Subject: [PATCH] python2 to 3 compatbility for integer division and testing for string types --- bin/pyplotres.py | 16 ++++++++++++++-- lib/python/residuals.py | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/bin/pyplotres.py b/bin/pyplotres.py index db4b1db2f..fb9429098 100755 --- a/bin/pyplotres.py +++ b/bin/pyplotres.py @@ -202,7 +202,13 @@ def get_xdata(self, key): """Return label describing xaxis and the corresponding data given keyword 'key'. """ - if not (isinstance(key, bytes) or isinstance(key, str)): + # Python2/3 compatible way of checking for string types + # Taken from https://stackoverflow.com/questions/11301138/how-to-check-if-variable-is-string-with-python-2-and-3-compatibility + try: + basestring + except NameError: + basestring = str + if not isinstance(key, basestring): raise ValueError("key must be of type string.") xopt = key.lower() if xopt == 'numtoa': @@ -228,7 +234,13 @@ def get_ydata(self, key, postfit=True): 'postfit' is a boolean argument that determines if postfit, or prefit data is to be returned. """ - if not (isinstance(key, bytes) or isinstance(key, str)): + # Python2/3 compatible way of checking for string types + # Taken from https://stackoverflow.com/questions/11301138/how-to-check-if-variable-is-string-with-python-2-and-3-compatibility + try: + basestring + except NameError: + basestring = str + if not isinstance(key, basestring): raise ValueError("key must be of type string.") yopt = key.lower() if postfit: diff --git a/lib/python/residuals.py b/lib/python/residuals.py index 01e9c4560..69ae4c720 100644 --- a/lib/python/residuals.py +++ b/lib/python/residuals.py @@ -61,7 +61,7 @@ def read_residuals(filename="resid2.tmp"): not (reclen==struct.calcsize(rectype))): print("Warning: possibly reading residuals incorrectly... don't understand record size") infile.seek(0, 0) # position at file start - r.numTOAs = int(filelen / reclen) + r.numTOAs = filelen // reclen r.bary_TOA = Num.zeros(r.numTOAs, 'd') r.postfit_phs = Num.zeros(r.numTOAs, 'd') r.postfit_sec = Num.zeros(r.numTOAs, 'd')