1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
From: Christian Kastner <ckk@debian.org>
Date: Fri, 24 Jan 2020 17:58:41 +0100
Subject: Python 3 syntax fixes
setup.py uses 2to3=True to automatically convert syntax from Python 2 to 3
during build, but sphinx trips over the Python 2 syntax in the original source.
---
deap/algorithms.py | 14 +++++++-------
deap/base.py | 6 +++---
deap/benchmarks/movingpeaks.py | 4 ++--
deap/gp.py | 16 ++++++++--------
deap/tests/test_logbook.py | 2 +-
deap/tools/support.py | 6 +++---
doc/code/tutorials/part_3/3_next_step.py | 14 +++++++-------
doc/code/tutorials/part_4/4_4_Using_Cpp_NSGA.py | 20 ++++++++++----------
doc/code/tutorials/part_4/4_5_home_made_eval_func.py | 14 +++++++-------
doc/tutorials/advanced/numpy.rst | 4 ++--
10 files changed, 50 insertions(+), 50 deletions(-)
diff --git a/deap/algorithms.py b/deap/algorithms.py
index 4105fef..7e13333 100644
--- a/deap/algorithms.py
+++ b/deap/algorithms.py
@@ -157,7 +157,7 @@ def eaSimple(population, toolbox, cxpb, mutpb, ngen, stats=None,
record = stats.compile(population) if stats else {}
logbook.record(gen=0, nevals=len(invalid_ind), **record)
if verbose:
- print logbook.stream
+ print(logbook.stream)
# Begin the generational process
for gen in range(1, ngen + 1):
@@ -184,7 +184,7 @@ def eaSimple(population, toolbox, cxpb, mutpb, ngen, stats=None,
record = stats.compile(population) if stats else {}
logbook.record(gen=gen, nevals=len(invalid_ind), **record)
if verbose:
- print logbook.stream
+ print(logbook.stream)
return population, logbook
@@ -308,7 +308,7 @@ def eaMuPlusLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen,
record = stats.compile(population) if stats is not None else {}
logbook.record(gen=0, nevals=len(invalid_ind), **record)
if verbose:
- print logbook.stream
+ print(logbook.stream)
# Begin the generational process
for gen in range(1, ngen + 1):
@@ -332,7 +332,7 @@ def eaMuPlusLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen,
record = stats.compile(population) if stats is not None else {}
logbook.record(gen=gen, nevals=len(invalid_ind), **record)
if verbose:
- print logbook.stream
+ print(logbook.stream)
return population, logbook
@@ -409,7 +409,7 @@ def eaMuCommaLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen,
record = stats.compile(population) if stats is not None else {}
logbook.record(gen=0, nevals=len(invalid_ind), **record)
if verbose:
- print logbook.stream
+ print(logbook.stream)
# Begin the generational process
for gen in range(1, ngen + 1):
@@ -433,7 +433,7 @@ def eaMuCommaLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen,
record = stats.compile(population) if stats is not None else {}
logbook.record(gen=gen, nevals=len(invalid_ind), **record)
if verbose:
- print logbook.stream
+ print(logbook.stream)
return population, logbook
@@ -494,6 +494,6 @@ def eaGenerateUpdate(toolbox, ngen, halloffame=None, stats=None,
record = stats.compile(population) if stats is not None else {}
logbook.record(gen=gen, nevals=len(population), **record)
if verbose:
- print logbook.stream
+ print(logbook.stream)
return population, logbook
diff --git a/deap/base.py b/deap/base.py
index b57b966..af5cd9d 100644
--- a/deap/base.py
+++ b/deap/base.py
@@ -66,7 +66,7 @@ class Toolbox(object):
The following code block is an example of how the toolbox is used. ::
>>> def func(a, b, c=3):
- ... print a, b, c
+ ... print(a, b, c)
...
>>> tools = Toolbox()
>>> tools.register("myFunc", func, 2, c=4)
@@ -189,12 +189,12 @@ class Fitness(object):
self.wvalues = tuple(map(mul, values, self.weights))
except TypeError:
_, _, traceback = sys.exc_info()
- raise TypeError, ("Both weights and assigned values must be a "
+ raise TypeError(("Both weights and assigned values must be a "
"sequence of numbers when assigning to values of "
"%r. Currently assigning value(s) %r of %r to a "
"fitness with weights %s."
% (self.__class__, values, type(values),
- self.weights)), traceback
+ self.weights))).with_traceback(traceback)
def delValues(self):
self.wvalues = ()
diff --git a/deap/benchmarks/movingpeaks.py b/deap/benchmarks/movingpeaks.py
index f0cfe2e..cbedce6 100644
--- a/deap/benchmarks/movingpeaks.py
+++ b/deap/benchmarks/movingpeaks.py
@@ -393,6 +393,6 @@ def diversity(population):
if __name__ == "__main__":
mpb = MovingPeaks(dim=2, npeaks=[1,1,10], number_severity=0.1)
- print mpb.maximums()
+ print(mpb.maximums())
mpb.changePeaks()
- print mpb.maximums()
+ print(mpb.maximums())
diff --git a/deap/gp.py b/deap/gp.py
index 451a2da..26ca4e8 100644
--- a/deap/gp.py
+++ b/deap/gp.py
@@ -478,11 +478,11 @@ def compile(expr, pset):
return eval(code, pset.context, {})
except MemoryError:
_, _, traceback = sys.exc_info()
- raise MemoryError, ("DEAP : Error in tree evaluation :"
+ raise MemoryError("DEAP : Error in tree evaluation :"
" Python cannot evaluate a tree higher than 90. "
"To avoid this problem, you should use bloat control on your "
"operators. See the DEAP documentation for more information. "
- "DEAP will now abort."), traceback
+ "DEAP will now abort.").with_traceback(traceback)
def compileADF(expr, psets):
@@ -613,9 +613,9 @@ def generate(pset, min_, max_, condition, type_=None):
term = random.choice(pset.terminals[type_])
except IndexError:
_, _, traceback = sys.exc_info()
- raise IndexError, "The gp.generate function tried to add " \
+ raise IndexError("The gp.generate function tried to add " \
"a terminal of type '%s', but there is " \
- "none available." % (type_,), traceback
+ "none available." % (type_,)).with_traceback(traceback)
if isclass(term):
term = term()
expr.append(term)
@@ -624,9 +624,9 @@ def generate(pset, min_, max_, condition, type_=None):
prim = random.choice(pset.primitives[type_])
except IndexError:
_, _, traceback = sys.exc_info()
- raise IndexError, "The gp.generate function tried to add " \
+ raise IndexError("The gp.generate function tried to add " \
"a primitive of type '%s', but there is " \
- "none available." % (type_,), traceback
+ "none available." % (type_,)).with_traceback(traceback)
expr.append(prim)
for arg in reversed(prim.args):
stack.append((depth + 1, arg))
@@ -1058,7 +1058,7 @@ def harm(population, toolbox, cxpb, mutpb, ngen,
record = stats.compile(population) if stats else {}
logbook.record(gen=0, nevals=len(invalid_ind), **record)
if verbose:
- print logbook.stream
+ print(logbook.stream)
# Begin the generational process
for gen in range(1, ngen + 1):
@@ -1125,7 +1125,7 @@ def harm(population, toolbox, cxpb, mutpb, ngen,
record = stats.compile(population) if stats else {}
logbook.record(gen=gen, nevals=len(invalid_ind), **record)
if verbose:
- print logbook.stream
+ print(logbook.stream)
return population, logbook
diff --git a/deap/tests/test_logbook.py b/deap/tests/test_logbook.py
index 7a7639e..3194ade 100644
--- a/deap/tests/test_logbook.py
+++ b/deap/tests/test_logbook.py
@@ -7,7 +7,7 @@ class LogbookTest(unittest.TestCase):
def setUp(self):
self.logbook = tools.Logbook()
- print
+ print()
def test_multi_chapters(self):
self.logbook.record(gen=0, evals=100, fitness={'obj 1' : {'avg' : 1.0, 'max' : 10},
diff --git a/deap/tools/support.py b/deap/tools/support.py
index 3f4ac7e..7decd65 100644
--- a/deap/tools/support.py
+++ b/deap/tools/support.py
@@ -302,7 +302,7 @@ class Logbook(list):
>>> # [...]
>>> record = mstats.compile(population)
>>> logbook.record(**record)
- >>> print logbook
+ >>> print(logbook)
fitness length
------------ ------------
max mean max mean
@@ -388,11 +388,11 @@ class Logbook(list):
>>> log = Logbook()
>>> log.append({'gen' : 0})
- >>> print log.stream # doctest: +NORMALIZE_WHITESPACE
+ >>> print(log.stream) # doctest: +NORMALIZE_WHITESPACE
gen
0
>>> log.append({'gen' : 1})
- >>> print log.stream # doctest: +NORMALIZE_WHITESPACE
+ >>> print(log.stream) # doctest: +NORMALIZE_WHITESPACE
1
"""
startindex, self.buffindex = self.buffindex, len(self)
diff --git a/doc/code/tutorials/part_3/3_next_step.py b/doc/code/tutorials/part_3/3_next_step.py
index ff59e08..356e96a 100644
--- a/doc/code/tutorials/part_3/3_next_step.py
+++ b/doc/code/tutorials/part_3/3_next_step.py
@@ -17,8 +17,8 @@ toolbox.register("individual", tools.initRepeat, creator.Individual,
ind1 = toolbox.individual()
-print ind1 # [0.86..., 0.27..., 0.70..., 0.03..., 0.87...]
-print ind1.fitness.valid # False
+print(ind1) # [0.86..., 0.27..., 0.70..., 0.03..., 0.87...]
+print(ind1.fitness.valid) # False
## 3.2 Evaluation
def evaluate(individual):
@@ -28,16 +28,16 @@ def evaluate(individual):
return a, 1. / b
ind1.fitness.values = evaluate(ind1)
-print ind1.fitness.valid # True
-print ind1.fitness # (2.73, 0.2)
+print(ind1.fitness.valid) # True
+print(ind1.fitness) # (2.73, 0.2)
## 3.3 Mutation
mutant = toolbox.clone(ind1)
ind2, = tools.mutGaussian(mutant, mu=0.0, sigma=0.2, indpb=0.2)
del mutant.fitness.values
-print ind2 is mutant # True
-print mutant is ind1 # False
+print(ind2 is mutant) # True
+print(mutant is ind1) # False
## 3.4 Crossover
child1, child2 = [toolbox.clone(ind) for ind in (ind1, ind2)]
@@ -47,7 +47,7 @@ del child2.fitness.values
## 3.5 Selection
selected = tools.selBest([child1, child2], 2)
-print child1 in selected # True
+print(child1 in selected) # True
## 3.5 Note
LAMBDA = 10
diff --git a/doc/code/tutorials/part_4/4_4_Using_Cpp_NSGA.py b/doc/code/tutorials/part_4/4_4_Using_Cpp_NSGA.py
index 44e387f..dfa8b49 100644
--- a/doc/code/tutorials/part_4/4_4_Using_Cpp_NSGA.py
+++ b/doc/code/tutorials/part_4/4_4_Using_Cpp_NSGA.py
@@ -91,8 +91,8 @@ def main():
stats.update(population)
# Begin the evolution
- for g in xrange(NGEN):
- print "-- Generation %i --" % g
+ for g in range(NGEN):
+ print("-- Generation %i --" % g)
offspring = [toolbox.clone(ind) for ind in population]
# Apply crossover and mutation
@@ -121,21 +121,21 @@ def main():
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
- print " Evaluated %i individuals" % len(invalid_ind)
+ print(" Evaluated %i individuals" % len(invalid_ind))
population = toolbox.select(population+offspring, len(offspring))
hof.update(population)
stats.update(population)
- print " Min %s" % stats.Min[0][-1][0]
- print " Max %s" % stats.Max[0][-1][0]
- print " Avg %s" % stats.Avg[0][-1][0]
- print " Std %s" % stats.Std[0][-1][0]
+ print(" Min %s" % stats.Min[0][-1][0])
+ print(" Max %s" % stats.Max[0][-1][0])
+ print(" Avg %s" % stats.Avg[0][-1][0])
+ print(" Std %s" % stats.Std[0][-1][0])
best_network = sn.SortingNetwork(INPUTS, hof[0])
- print best_network
- print best_network.draw()
- print "%i errors, length %i, depth %i" % hof[0].fitness.values
+ print(best_network)
+ print(best_network.draw())
+ print("%i errors, length %i, depth %i" % hof[0].fitness.values)
return population, stats, hof
diff --git a/doc/code/tutorials/part_4/4_5_home_made_eval_func.py b/doc/code/tutorials/part_4/4_5_home_made_eval_func.py
index 1646fff..af5145e 100644
--- a/doc/code/tutorials/part_4/4_5_home_made_eval_func.py
+++ b/doc/code/tutorials/part_4/4_5_home_made_eval_func.py
@@ -97,9 +97,9 @@ def main():
stats.update(population)
# Begin the evolution
- for g in xrange(NGEN):
+ for g in range(NGEN):
t1 = time.time()
- print "-- Generation %i --" % g
+ print("-- Generation %i --" % g)
offspring = [toolbox.clone(ind) for ind in population]
t2 = time.time()
# Apply crossover and mutation
@@ -128,14 +128,14 @@ def main():
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
- print " Evaluated %i individuals" % len(invalid_ind)
+ print(" Evaluated %i individuals" % len(invalid_ind))
t5 = time.time()
population = toolbox.select(population+offspring, len(offspring))
t6 = time.time()
#hof.update(population)
stats.update(population)
t7 = time.time()
- print stats
+ print(stats)
print("Times :")
print("Clone : " + str(t2-t1) + " (" + str((t2-t1)/(t7-t1)) +"%)")
@@ -148,9 +148,9 @@ def main():
best_network = sn.SortingNetwork(INPUTS, hof[0])
- print best_network
- print best_network.draw()
- print "%i errors, length %i, depth %i" % hof[0].fitness.values
+ print(best_network)
+ print(best_network.draw())
+ print("%i errors, length %i, depth %i" % hof[0].fitness.values)
return population, stats, hof
diff --git a/doc/tutorials/advanced/numpy.rst b/doc/tutorials/advanced/numpy.rst
index d2c45f2..2c2a901 100644
--- a/doc/tutorials/advanced/numpy.rst
+++ b/doc/tutorials/advanced/numpy.rst
@@ -86,7 +86,7 @@ This cannot be used as a condition
::
>>> if operator.eq(a, b):
- ... print "Gosh!"
+ ... print("Gosh!")
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
@@ -102,7 +102,7 @@ Now the condition can be computed and the hall-of-fame will be happy.
::
>>> if numpy.array_equal(a, b):
- ... print "Yeah!"
+ ... print("Yeah!")
"Yeah!"
|