From 2c46e32782fbc2c8cfd82e657d08649c1a36df72 Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Thu, 19 Nov 2020 20:18:11 -0500 Subject: [PATCH] Minor changes and fixes __iter__ has to return an iter --- src/structure/chromosome.py | 14 +++++++------- src/structure/population.py | 26 +++++++++++++------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/structure/chromosome.py b/src/structure/chromosome.py index d729e99..fecae9d 100644 --- a/src/structure/chromosome.py +++ b/src/structure/chromosome.py @@ -57,17 +57,17 @@ class Chromosome: def __iter__(self): """Returns an iterable of the gene list""" - return self.gene_list + return iter(self.gene_list) - def __getitem__(self, k): - """Returns the k-th gene""" - return self.get_gene(k) + def __getitem__(self, index): + """Returns the indexed gene""" + return self.gene_list[index] - def __setitem__(self, k, gene): - """Sets the k-th gene value""" - self.set_gene(gene, k) + def __setitem__(self, index, gene): + """Sets the indexed gene value""" + self.gene_list[index] = gene def __len__(self): diff --git a/src/structure/population.py b/src/structure/population.py index 92befa3..83bf086 100644 --- a/src/structure/population.py +++ b/src/structure/population.py @@ -21,18 +21,18 @@ class Population: def remove_chromosome(self, index): - """Removes a chromosome from the indicated index from the population""" - del self.chromosome_list[index] + """Removes and returns a chromosome from the indicated index from the population""" + return self.chromosome_list.pop(index) def remove_parent(self, index): - """Removes a parent from the indicated index from the mating pool""" - del self.mating_pool[index] + """Removes and returns a parent from the indicated index from the mating pool""" + return self.mating_pool.pop(index) def remove_child(self, index): - """Removes a child from the indicated index from the next population""" - del self.next_population[index] + """Removes and returns a child from the indicated index from the next population""" + return self.next_population.pop(index) def reset_mating_pool(self): @@ -153,17 +153,17 @@ class Population: def __iter__(self): """Returns an iterable of chromosomes""" - return self.chromosome_list + return iter(self.chromosome_list) - def __getitem__(self, k): - """Returns the k-th chromosome""" - return self.get_chromosome(k) + def __getitem__(self, index): + """Returns the indexed chromosome""" + return self.chromosome_list[index] - def __setitem__(self, k, chromosome): - """Sets the k-th chromosome""" - self.set_chromosome(chromosome, k) + def __setitem__(self, index, chromosome): + """Sets the indexed chromosome""" + self.chromosome_list[index] = chromosome def __len__(self):