Added automatic casting for easier usage
This commit is contained in:
@ -14,7 +14,7 @@ class Chromosome:
|
|||||||
"""Add a gene to the chromosome at the specified index, defaulted to end of the chromosome"""
|
"""Add a gene to the chromosome at the specified index, defaulted to end of the chromosome"""
|
||||||
if index is None:
|
if index is None:
|
||||||
index = len(self)
|
index = len(self)
|
||||||
self.gene_list.insert(index, gene)
|
self.gene_list.insert(index, make_gene(gene))
|
||||||
|
|
||||||
|
|
||||||
def remove_gene(self, index):
|
def remove_gene(self, index):
|
||||||
@ -67,7 +67,10 @@ class Chromosome:
|
|||||||
chromosome[index] = gene
|
chromosome[index] = gene
|
||||||
to set the indexed gene.
|
to set the indexed gene.
|
||||||
"""
|
"""
|
||||||
self.gene_list[index] = gene
|
if isinstance(index, int):
|
||||||
|
self.gene_list[index] = make_gene(gene)
|
||||||
|
else:
|
||||||
|
self.gene_list[index] = (make_gene(item) for item in gene)
|
||||||
|
|
||||||
|
|
||||||
def __delitem__(self, index):
|
def __delitem__(self, index):
|
||||||
@ -88,13 +91,13 @@ class Chromosome:
|
|||||||
return len(self.gene_list)
|
return len(self.gene_list)
|
||||||
|
|
||||||
|
|
||||||
def __contains__(self, searched_gene):
|
def __contains__(self, gene):
|
||||||
"""
|
"""
|
||||||
Allows the user to use
|
Allows the user to use
|
||||||
if gene in chromosome
|
if gene in chromosome
|
||||||
to check if a gene is in the chromosome.
|
to check if a gene is in the chromosome.
|
||||||
"""
|
"""
|
||||||
return (searched_gene in self.gene_list)
|
return (make_gene(gene) in self.gene_list)
|
||||||
|
|
||||||
|
|
||||||
def index_of(self, gene, guess = None):
|
def index_of(self, gene, guess = None):
|
||||||
@ -109,6 +112,9 @@ class Chromosome:
|
|||||||
the index quicker.
|
the index quicker.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# Cast to gene object
|
||||||
|
gene = make_gene(gene)
|
||||||
|
|
||||||
# Use built-in method
|
# Use built-in method
|
||||||
if guess is None:
|
if guess is None:
|
||||||
return self.gene_list.index(gene)
|
return self.gene_list.index(gene)
|
||||||
|
|||||||
@ -15,13 +15,14 @@ class Gene:
|
|||||||
|
|
||||||
|
|
||||||
def __eq__(self, other_gene):
|
def __eq__(self, other_gene):
|
||||||
"""Comparing two genes by their value.
|
"""Comparing two genes by their value."""
|
||||||
Returns False if either gene is None."""
|
|
||||||
|
|
||||||
if (self is None) or (other_gene is None):
|
try:
|
||||||
return False
|
other_value = other_gene.value
|
||||||
else:
|
except:
|
||||||
return self.value == other_gene.value
|
other_value = other_gene
|
||||||
|
|
||||||
|
return self.value == other_value
|
||||||
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user