Skip to content

Commit 84d79aa

Browse files
committed
fix python2 to python3 to make pre-commit black hook work
1 parent 75e5111 commit 84d79aa

16 files changed

+978
-701
lines changed

Diff for: devutils/makesdist

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ sys.path.insert(0, BASEDIR)
1717
from setup import versiondata
1818
timestamp = versiondata.getint('DEFAULT', 'timestamp')
1919

20-
print 'Run "setup.py sdist --formats=tar"',
20+
print('Run "setup.py sdist --formats=tar"',)
2121
cmd_sdist = [sys.executable] + 'setup.py sdist --formats=tar'.split()
2222
ec = subprocess.call(cmd_sdist, cwd=BASEDIR, stdout=open(os.devnull, 'w'))
2323
if ec: sys.exit(ec)
24-
print "[done]"
24+
print("[done]")
2525

2626
tarname = max(glob.glob(BASEDIR + '/dist/*.tar'), key=os.path.getmtime)
2727

@@ -36,8 +36,8 @@ def fixtarinfo(tinfo):
3636
return tinfo
3737

3838

39-
print 'Filter %s --> %s.gz' % (2 * (os.path.basename(tarname),)),
39+
print('Filter %s --> %s.gz' % (2 * (os.path.basename(tarname),)),)
4040
for ti in tfin:
4141
tfout.addfile(fixtarinfo(ti), tfin.extractfile(ti))
4242
os.remove(tarname)
43-
print "[done]"
43+
print("[done]")

Diff for: devutils/prep.py

+25-21
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
__basedir__ = os.getcwdu()
1010

11+
from numpy.compat import unicode
12+
1113
# Example imports
1214

1315

@@ -18,20 +20,21 @@ def __init__(self):
1820

1921
def test(self, call, *args, **kwds):
2022
m = sys.modules[call.__module__]
21-
testname = m.__name__+'.'+call.__name__
23+
testname = m.__name__ + "." + call.__name__
2224
path = os.path.dirname(m.__file__)
2325
os.chdir(path)
2426
try:
2527
call(*args, **kwds)
26-
self.messages.append("%s: success" %testname)
27-
except Exception, e:
28-
self.messages.append("%s: error, details below.\n%s" %(testname, e))
28+
self.messages.append("%s: success" % testname)
29+
except Exception as e:
30+
self.messages.append("%s: error, details below.\n%s" % (testname, e))
2931
finally:
3032
os.chdir(__basedir__)
3133

3234
def report(self):
33-
print '==== Results of Tests ===='
34-
print '\n'.join(self.messages)
35+
print("==== Results of Tests ====")
36+
print("\n".join(self.messages))
37+
3538

3639
def scrubeol(directory, filerestr):
3740
"""Use unix-style endlines for files in directory matched by regex string.
@@ -50,11 +53,11 @@ def scrubeol(directory, filerestr):
5053
text = unicode(original.read())
5154
original.close()
5255

53-
updated = io.open(f, 'w', newline='\n')
56+
updated = io.open(f, "w", newline="\n")
5457
updated.write(text)
5558
updated.close()
5659

57-
print "Updated %s to unix-style endlines." %f
60+
print("Updated %s to unix-style endlines." % f)
5861

5962

6063
def rm(directory, filerestr):
@@ -72,14 +75,13 @@ def rm(directory, filerestr):
7275
for f in files:
7376
os.remove(f)
7477

75-
print "Deleted %s." %f
76-
78+
print("Deleted %s." % f)
7779

7880

7981
if __name__ == "__main__":
8082

8183
# Temporarily add examples to path
82-
lib_path = os.path.abspath(os.path.join('..','doc','examples'))
84+
lib_path = os.path.abspath(os.path.join("..", "doc", "examples"))
8385
sys.path.append(lib_path)
8486

8587
# Delete existing files that don't necessarily have a fixed name.
@@ -88,14 +90,16 @@ def rm(directory, filerestr):
8890

8991
### Testing examples
9092
examples = Test()
91-
test_names = ["extract_single_peak",
92-
"parameter_summary",
93-
"fit_initial",
94-
"query_results",
95-
"multimodel_known_dG1",
96-
"multimodel_known_dG2",
97-
"multimodel_unknown_dG1",
98-
"multimodel_unknown_dG2"]
93+
test_names = [
94+
"extract_single_peak",
95+
"parameter_summary",
96+
"fit_initial",
97+
"query_results",
98+
"multimodel_known_dG1",
99+
"multimodel_known_dG2",
100+
"multimodel_unknown_dG1",
101+
"multimodel_unknown_dG2",
102+
]
99103

100104
test_modules = []
101105
for test in test_names:
@@ -107,7 +111,7 @@ def rm(directory, filerestr):
107111
examples.report()
108112

109113
### Convert output of example files to Unix-style endlines for sdist.
110-
if os.linesep != '\n':
111-
print "==== Scrubbing Endlines ===="
114+
if os.linesep != "\n":
115+
print("==== Scrubbing Endlines ====")
112116
# All *.srmise and *.pwa files in examples directory.
113117
scrubeol("../doc/examples/output", r".*(\.srmise|\.pwa)")

Diff for: diffpy/srmise/baselines/arbitrary.py

+31-25
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
import numpy as np
1818

1919
import diffpy.srmise.srmiselog
20+
from diffpy.srmise.baselines import Polynomial
2021
from diffpy.srmise.baselines.base import BaselineFunction
2122
from diffpy.srmise.srmiseerrors import SrMiseEstimationError
2223

2324
logger = logging.getLogger("diffpy.srmise")
2425

25-
class Arbitrary (BaselineFunction):
26+
27+
class Arbitrary(BaselineFunction):
2628
"""Methods for evaluating a baseline from an arbitrary function.
2729
2830
Supports baseline calculations with arbitrary functions. These functions,
@@ -64,10 +66,10 @@ def __init__(self, npars, valuef, jacobianf=None, estimatef=None, Cache=None):
6466
# Define parameterdict
6567
# e.g. {"a_0":0, "a_1":1, "a_2":2, "a_3":3} if npars is 4.
6668
parameterdict = {}
67-
for d in range(self.testnpars+1):
68-
parameterdict["a_"+str(d)] = d
69-
formats = ['internal']
70-
default_formats = {'default_input':'internal', 'default_output':'internal'}
69+
for d in range(self.testnpars + 1):
70+
parameterdict["a_" + str(d)] = d
71+
formats = ["internal"]
72+
default_formats = {"default_input": "internal", "default_output": "internal"}
7173

7274
# Check that the provided functions are at least callable
7375
if valuef is None or callable(valuef):
@@ -93,7 +95,9 @@ def __init__(self, npars, valuef, jacobianf=None, estimatef=None, Cache=None):
9395
metadict["valuef"] = (valuef, repr)
9496
metadict["jacobianf"] = (jacobianf, repr)
9597
metadict["estimatef"] = (estimatef, repr)
96-
BaselineFunction.__init__(self, parameterdict, formats, default_formats, metadict, None, Cache)
98+
BaselineFunction.__init__(
99+
self, parameterdict, formats, default_formats, metadict, None, Cache
100+
)
97101

98102
#### Methods required by BaselineFunction ####
99103

@@ -114,9 +118,8 @@ def estimate_parameters(self, r, y):
114118
# TODO: check that estimatef returns something proper?
115119
try:
116120
return self.estimatef(r, y)
117-
except Exception, e:
118-
emsg = "Error within estimation routine provided to Arbitrary:\n"+\
119-
str(e)
121+
except Exception as e:
122+
emsg = "Error within estimation routine provided to Arbitrary:\n" + str(e)
120123
raise SrMiseEstimationError(emsg)
121124

122125
def _jacobianraw(self, pars, r, free):
@@ -137,10 +140,10 @@ def _jacobianraw(self, pars, r, free):
137140
emsg = "No jacobian routine provided to Arbitrary."
138141
raise NotImplementedError(emsg)
139142
if len(pars) != self.npars:
140-
emsg = "Argument pars must have "+str(self.npars)+" elements."
143+
emsg = "Argument pars must have " + str(self.npars) + " elements."
141144
raise ValueError(emsg)
142145
if len(free) != self.npars:
143-
emsg = "Argument free must have "+str(self.npars)+" elements."
146+
emsg = "Argument free must have " + str(self.npars) + " elements."
144147
raise ValueError(emsg)
145148

146149
# Allow an arbitrary function without a Jacobian provided act as
@@ -149,7 +152,7 @@ def _jacobianraw(self, pars, r, free):
149152
# large performance implications if all other functions used while
150153
# fitting a function define a Jacobian.
151154
if nfree == 0:
152-
return [None for p in range(len(par))]
155+
return [None for p in range(len(pars))]
153156

154157
# TODO: check that jacobianf returns something proper?
155158
return self.jacobianf(pars, r, free)
@@ -170,15 +173,17 @@ def _transform_parametersraw(self, pars, in_format, out_format):
170173
if in_format == "internal":
171174
pass
172175
else:
173-
raise ValueError("Argument 'in_format' must be one of %s." \
174-
% self.parformats)
176+
raise ValueError(
177+
"Argument 'in_format' must be one of %s." % self.parformats
178+
)
175179

176180
# Convert to specified output format from "internal" format.
177181
if out_format == "internal":
178182
pass
179183
else:
180-
raise ValueError("Argument 'out_format' must be one of %s." \
181-
% self.parformats)
184+
raise ValueError(
185+
"Argument 'out_format' must be one of %s." % self.parformats
186+
)
182187
return temp
183188

184189
def _valueraw(self, pars, r):
@@ -192,7 +197,7 @@ def _valueraw(self, pars, r):
192197
...
193198
r: sequence or scalar over which pars is evaluated"""
194199
if len(pars) != self.npars:
195-
emsg = "Argument pars must have "+str(self.npars)+" elements."
200+
emsg = "Argument pars must have " + str(self.npars) + " elements."
196201
raise ValueError(emsg)
197202

198203
# TODO: check that valuef returns something proper?
@@ -201,21 +206,22 @@ def _valueraw(self, pars, r):
201206
def getmodule(self):
202207
return __name__
203208

204-
#end of class Polynomial
209+
210+
# end of class Polynomial
205211

206212
# simple test code
207-
if __name__ == '__main__':
213+
if __name__ == "__main__":
208214

209-
f = Polynomial(degree = 3)
215+
f = Polynomial(degree=3)
210216
r = np.arange(5)
211217
pars = np.array([3, 0, 1, 2])
212218
free = np.array([True, False, True, True])
213-
print f._valueraw(pars, r)
214-
print f._jacobianraw(pars, r, free)
219+
print(f._valueraw(pars, r))
220+
print(f._jacobianraw(pars, r, free))
215221

216-
f = Polynomial(degree = -1)
222+
f = Polynomial(degree=-1)
217223
r = np.arange(5)
218224
pars = np.array([])
219225
free = np.array([])
220-
print f._valueraw(pars, r)
221-
print f._jacobianraw(pars, r, free)
226+
print(f._valueraw(pars, r))
227+
print(f._jacobianraw(pars, r, free))

Diff for: diffpy/srmise/baselines/base.py

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import diffpy.srmise.srmiselog
1919
from diffpy.srmise.basefunction import BaseFunction
2020
from diffpy.srmise.modelparts import ModelPart
21+
from diffpy.srmise.peaks import Peaks
2122
from diffpy.srmise.srmiseerrors import *
2223

2324
logger = logging.getLogger("diffpy.srmise")

0 commit comments

Comments
 (0)