def get_similar(self, candidates=None): if self.rank < 0: return self if candidates is None: candidates = [c for c in self.ranked if c.rank >= 0] candidates = sorted(candidates, key=lambda c: c.rank) index = candidates.index(self) index = mutate_normal(index, maximum=len(candidates)-1) return candidates[index]
def mutate(self): if not hasattr(self, "mutate_attributes"): return for attribute in sorted(self.mutate_attributes): if isinstance(self.mutate_attributes[attribute], type): tob = self.mutate_attributes[attribute] index = getattr(self, attribute) tob = tob.get(index) tob = tob.get_similar() setattr(self, attribute, tob.index) else: minmax = self.mutate_attributes[attribute] if type(minmax) is tuple: minimum, maximum = minmax else: minimum, maximum = 0, 0xff value = getattr(self, attribute) if value < minimum or value > maximum: continue value = mutate_normal(value, minimum=minimum, maximum=maximum) setattr(self, attribute, value)