Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First round of Pylint fixes. #717

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions deap/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ def varAnd(population, toolbox, cxpb, mutpb):
offspring[i])
del offspring[i - 1].fitness.values, offspring[i].fitness.values

for i in range(len(offspring)):
for i, off in enumerate(offspring):
if random.random() < mutpb:
offspring[i], = toolbox.mutate(offspring[i])
offspring[i], = toolbox.mutate(off)
del offspring[i].fitness.values

return offspring
Expand Down
16 changes: 8 additions & 8 deletions deap/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,9 @@ def __le__(self, other):

if self_violates_constraints and other_violates_constraints:
return True
elif self_violates_constraints:
if self_violates_constraints:
return True
elif other_violates_constraints:
if other_violates_constraints:
return False

return self.wvalues <= other.wvalues
Expand All @@ -311,9 +311,9 @@ def __lt__(self, other):

if self_violates_constraints and other_violates_constraints:
return False
elif self_violates_constraints:
if self_violates_constraints:
return True
elif other_violates_constraints:
if other_violates_constraints:
return False

return self.wvalues < other.wvalues
Expand All @@ -324,9 +324,9 @@ def __eq__(self, other):

if self_violates_constraints and other_violates_constraints:
return True
elif self_violates_constraints:
if self_violates_constraints:
return False
elif other_violates_constraints:
if other_violates_constraints:
return False

return self.wvalues == other.wvalues
Expand All @@ -340,9 +340,9 @@ def dominates(self, other):

if self_violates_constraints and other_violates_constraints:
return False
elif self_violates_constraints:
if self_violates_constraints:
return False
elif other_violates_constraints:
if other_violates_constraints:
return True

return super(ConstrainedFitness, self).dominates(other)
Expand Down
10 changes: 5 additions & 5 deletions deap/cma.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ def generate(self, ind_init):
of its parent.
"""
arz = numpy.random.randn(self.lambda_, self.dim)
individuals = list()
individuals = []

# Make sure every parent has a parent tag and index
for i, p in enumerate(self.parents):
Expand Down Expand Up @@ -436,9 +436,9 @@ def _select(self, candidates):

pareto_fronts = tools.sortLogNondominated(candidates, len(candidates))

chosen = list()
chosen = []
mid_front = None
not_chosen = list()
not_chosen = []

# Fill the next population (chosen) with the fronts until there is not enough space
# When an entire front does not fit in the space left we rely on the hypervolume
Expand Down Expand Up @@ -641,7 +641,7 @@ def __init__(self, parent, sigma, steps, **kargs):
< self.S_int)

self.constraint_vecs = None
self.ancestors_fitness = list()
self.ancestors_fitness = []

@property
def lambda_(self):
Expand Down Expand Up @@ -703,7 +703,7 @@ def _integer_mutation(self):
# the last best solution. We skip this last best solution part.
if n_I_R == 0:
return numpy.zeros((self.lambda_, self.dim))
elif n_I_R == self.dim:
if n_I_R == self.dim:
p = self.lambda_ / 2.0 / self.lambda_
# lambda_int = int(numpy.floor(self.lambda_ / 2))
else:
Expand Down
37 changes: 17 additions & 20 deletions deap/gp.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ def __eq__(self, other):
if type(self) is type(other):
return all(getattr(self, slot) == getattr(other, slot)
for slot in self.__slots__)
else:
return NotImplemented
return NotImplemented


class Terminal(object):
Expand All @@ -238,8 +237,7 @@ def __eq__(self, other):
if type(self) is type(other):
return all(getattr(self, slot) == getattr(other, slot)
for slot in self.__slots__)
else:
return NotImplemented
return NotImplemented


class MetaEphemeral(type):
Expand Down Expand Up @@ -296,7 +294,7 @@ def __init__(self, name, in_types, ret_type, prefix="ARG"):
# being polluted by builtins function when evaluating
# GP expression.
self.context = {"__builtins__": None}
self.mapping = dict()
self.mapping = {}
self.terms_count = 0
self.prims_count = 0

Expand Down Expand Up @@ -639,7 +637,7 @@ def generate(pset, min_, max_, condition, type_=None):
raise IndexError("The gp.generate function tried to add "
"a terminal of type '%s', but there is "
"none available." % (type_,)).with_traceback(traceback)
if type(term) is MetaEphemeral:
if isinstance(term, MetaEphemeral):
term = term()
expr.append(term)
else:
Expand Down Expand Up @@ -772,7 +770,7 @@ def mutUniform(individual, expr, pset):
slice_ = individual.searchSubtree(index)
type_ = individual[index].ret
individual[slice_] = expr(pset=pset, type_=type_)
return individual,
return (individual,)


def mutNodeReplacement(individual, pset):
Expand All @@ -784,21 +782,21 @@ def mutNodeReplacement(individual, pset):
:returns: A tuple of one tree.
"""
if len(individual) < 2:
return individual,
return (individual,)

index = random.randrange(1, len(individual))
node = individual[index]

if node.arity == 0: # Terminal
term = random.choice(pset.terminals[node.ret])
if type(term) is MetaEphemeral:
if isinstance(term, MetaEphemeral):
term = term()
individual[index] = term
else: # Primitive
prims = [p for p in pset.primitives[node.ret] if p.args == node.args]
individual[index] = random.choice(prims)

return individual,
return (individual,)


def mutEphemeral(individual, mode):
Expand Down Expand Up @@ -826,7 +824,7 @@ def mutEphemeral(individual, mode):
for i in ephemerals_idx:
individual[i] = type(individual[i])()

return individual,
return (individual,)


def mutInsert(individual, pset):
Expand All @@ -850,7 +848,7 @@ def mutInsert(individual, pset):
primitives = [p for p in pset.primitives[node.ret] if node.ret in p.args]

if len(primitives) == 0:
return individual,
return (individual,)

new_node = choice(primitives)
new_subtree = [None] * len(new_node.args)
Expand All @@ -866,7 +864,7 @@ def mutInsert(individual, pset):
new_subtree[position:position + 1] = individual[slice_]
new_subtree.insert(0, new_node)
individual[slice_] = new_subtree
return individual,
return (individual,)


def mutShrink(individual):
Expand All @@ -878,7 +876,7 @@ def mutShrink(individual):
"""
# We don't want to "shrink" the root
if len(individual) < 3 or individual.height <= 1:
return individual,
return (individual,)

iprims = []
for i, node in enumerate(individual[1:], 1):
Expand All @@ -897,7 +895,7 @@ def mutShrink(individual):
slice_ = individual.searchSubtree(index)
individual[slice_] = subtree

return individual,
return (individual,)


######################################
Expand Down Expand Up @@ -1057,8 +1055,7 @@ def _genpop(n, pickfrom=[], acceptfunc=lambda s: True, producesizes=False):

if producesizes:
return producedpop, producedpopsizes
else:
return producedpop
return producedpop

def halflifefunc(x):
return x * float(alpha) + beta
Expand Down Expand Up @@ -1210,8 +1207,8 @@ def graph(expr):
out of order when using `NetworX <http://networkx.github.com/>`_.
"""
nodes = list(range(len(expr)))
edges = list()
labels = dict()
edges = []
labels = {}

stack = []
for i, node in enumerate(expr):
Expand Down Expand Up @@ -1282,7 +1279,7 @@ def mutSemantic(individual, gen_func=genGrow, pset=None, ms=None, min=2, max=6):
new_ind.extend(tr1)
new_ind.extend(tr2)

return new_ind,
return (new_ind,)


def cxSemantic(ind1, ind2, gen_func=genGrow, pset=None, min=2, max=6):
Expand Down
4 changes: 2 additions & 2 deletions examples/bbob.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def tupleize(func):
when the evaluation function returns a single value.
"""
def wrapper(*args, **kargs):
return func(*args, **kargs),
return (func(*args, **kargs),)
return wrapper


Expand Down Expand Up @@ -71,7 +71,7 @@ def main(func, dim, maxfuncevals, ftarget=None):

# Evolve until ftarget is reached or the number of evaluation
# is exhausted (maxfuncevals)
for g in range(1, maxfuncevals):
for _ in range(1, maxfuncevals):
toolbox.update(worst, best, sigma)
worst.fitness.values = toolbox.evaluate(worst)
if best.fitness <= worst.fitness:
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import deap

warnings = list()
warnings = []

try:
from setuptools import setup, Extension, find_packages
Expand Down Expand Up @@ -59,7 +59,7 @@ def build_extension(self, ext):
def run_setup(build_ext):
extra_modules = None
if build_ext:
extra_modules = list()
extra_modules = []

hv_module = Extension("deap.tools._hypervolume.hv", sources=["deap/tools/_hypervolume/_hv.c", "deap/tools/_hypervolume/hv.cpp"])
extra_modules.append(hv_module)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_nsga2(setup_teardown_multi_obj):
ind.fitness.values = fit

pop = toolbox.select(pop, len(pop))
for gen in range(1, NGEN):
for _ in range(1, NGEN):
offspring = tools.selTournamentDCD(pop, len(pop))
offspring = [toolbox.clone(ind) for ind in offspring]

Expand Down Expand Up @@ -169,7 +169,7 @@ def valid(individual):
toolbox.register("generate", strategy.generate, creator.__dict__[INDCLSNAME])
toolbox.register("update", strategy.update)

for gen in range(NGEN):
for _ in range(NGEN):
# Generate a new population
population = toolbox.generate()

Expand Down Expand Up @@ -222,7 +222,7 @@ def test_nsga3(setup_teardown_multi_obj):

pop = toolbox.select(pop, len(pop))
# Begin the generational process
for gen in range(1, NGEN):
for _ in range(1, NGEN):
offspring = algorithms.varAnd(pop, toolbox, 1.0, 1.0)

# Evaluate the individuals with an invalid fitness
Expand Down
14 changes: 7 additions & 7 deletions tests/test_convergence.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_cma_mixed_integer_1_p_1_no_constraint(self):

best = None

for gen in range(NGEN):
for _ in range(NGEN):
# Generate a new population
population = toolbox.generate()

Expand Down Expand Up @@ -118,7 +118,7 @@ def test_cma_mixed_integer_1_p_20_no_constraint(self):

best = None

for gen in range(NGEN):
for _ in range(NGEN):
# Generate a new population
population = toolbox.generate()

Expand Down Expand Up @@ -174,7 +174,7 @@ def c2(individual):

best = None

for gen in range(NGEN):
for _ in range(NGEN):
# Generate a new population
population = toolbox.generate()

Expand Down Expand Up @@ -234,7 +234,7 @@ def c2(individual):

best = None

for gen in range(NGEN):
for _ in range(NGEN):
# Generate a new population
population = toolbox.generate()

Expand Down Expand Up @@ -291,7 +291,7 @@ def test_nsga2(self):
ind.fitness.values = fit

pop = toolbox.select(pop, len(pop))
for gen in range(1, NGEN):
for _ in range(1, NGEN):
offspring = tools.selTournamentDCD(pop, len(pop))
offspring = [toolbox.clone(ind) for ind in offspring]

Expand Down Expand Up @@ -344,7 +344,7 @@ def test_nsga3(self):

pop = toolbox.select(pop, len(pop))
# Begin the generational process
for gen in range(1, NGEN):
for _ in range(1, NGEN):
# Vary the individuals
offspring = list(islice(algorithms.varAnd(pop, toolbox, 1.0, 1.0), len(pop)))

Expand Down Expand Up @@ -414,7 +414,7 @@ def valid(individual):
toolbox.register("generate", strategy.generate, creator.__dict__[INDCLSNAME])
toolbox.register("update", strategy.update)

for gen in range(NGEN):
for _ in range(NGEN):
# Generate a new population
population = toolbox.generate()

Expand Down
2 changes: 1 addition & 1 deletion tests/test_multiproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def _evalOneMax(individual):
return sum(individual),
return (sum(individual),)


def test_multiproc():
Expand Down