I've spent an hour or two collectively now trying to figure this problem out but I'm not getting any results. In my program when a defending animal is "killed" it should be deleted from it's registry, but no matter what I do I can't seem to get that to happen. In this instance if I were to initiate a hunter and a buffalo then attack() the buffalo with the hunter the buffalo should be killed and removed from it's classes registry but I can't get python to select that specific buffalo from the registry.
Any help is appreciated.
class IterRegistry(type):
def __iter__(cls):
return iter(cls._registry)
class Buffalo(object):
__metaclass__ = IterRegistry
_registry = []
hp = 1
price = 150
attacks = 0
regeneration = 2
def __init__(self, name):
self._registry.append(self)
self.name = name
def createBuffalo():
for i in range(len(Buffalo._registry),len(Buffalo._registry)+1):
varname = ("b" + str(i))
globals()[varname] = Buffalo("b" + str(i))
class Wolf:
__metaclass__ = IterRegistry
_registry = []
hp = 1
price = 0
attacks = 2
regeneration = 1.5
def __init__(self, name):
self._registry.append(self)
self.name = name
def createWolf():
for i in range(len(Wolf._registry),len(Wolf._registry)+1):
varname = ("w" + str(i))
globals()[varname] = Wolf("w" + str(i))
class Hunter:
__metaclass__ = IterRegistry
_registry = []
hp = 2
price = 0
attacks = 1
regeneration = 0
balance = 0
def __init__(self, name):
self._registry.append(self)
self.name = name
def createHunter():
for i in range(len(Hunter._registry),len(Hunter._registry)+1):
varname = ("h" + str(i))
globals()[varname] = Hunter("h" + str(i))
def attack(attacker, target):
if attacker.attacks >= 1:
target.hp -= 1
if target.hp == 0:
if type(attacker) == Hunter:
attacker.balance += 150
print(target)
if type(target) == Wolf:
del Wolf._registry[[n for n in Wolf._registry if n == target]]
if type(target) == Hunter:
del Hunter._registry[[n for n in Hunter._registry if n == target]]
if type(target) == Buffalo:
del Buffalo._registry[[n for n in Hunter._registry if n == target]]
One more note, the reason I have double brackets is because it was incorrect syntax with single brackets but the syntax was fine with doubles.
If you need any more code, ask in a comment below (I have not included all of it but it should be enough).
The issue you're trying to solve is caused by trying to use a list as an index to be removed from your list
[n for n in Wolf._registry if n == target]
All you're trying to get out of this list comprehension is the target, so just use that.
Wolf._registry.remove(target)
Related
I am currently creating my genetic algorithm and want to print the number of generations at the very end of the program when it finishes. However I am unsure how to access the counter variable that is the number of generations when it is outside of the class and method. So for example, at the end it would be like
Generation 100, average fit 18966, best fit 18947
Your best chromosone at generation 100
'\x06pzÂ\x8cYÆr¯n0q\x07l¿M8\x93Þ\x19\x87"\x01\x85\x1er\x89[F_VyER\x9b\x0bm=)\x9a\x9a¿¥\x10F\x12A\x84\x0fZ^\x14\x99\x8a4®\x9f¿*\\\xa0yi\x19E\x8aÇ+6(_<¾£cO~\x9c\x99\x932\x06\x0f\x82\x7f¤\x808xǸñA\x13\x0e<%\x06ÿ#í\x91Pô\x98 ®\r\x1b}\x89y¦\x0cqAK\tp\x95\x99ÔNj=Wn\x16\x94\x0cu!¯ñ\x13Qü[e8_ÂóU\x10\x1av_+%Q_¡ù\x87=\x08~ciÎ_Ï[\x8f#AëT\x14©qG\x89#Z«L\x9b¢\x94WL\x1dV¶R03\x84æ^ßr\x1fÃÈ\x1d\x8e Læª&®x\x94?TAÒD\x14£i\x82J\x15=w~\x03\x0c\xa0¾5\x02f5T\x91ol¢bIÞfk¬¡27W16(}6\x92\x87\n®xm0\x1a\n<8(à}ñ\x88̾\x17g\x9bj6\x8fI&\x12\x9aÂ\x9a_F\x1a\r[\x1dK\x15<.±DjcIy`98d>\x197Z\x91£%tIJ\x820\x93|\x07\x8dnÚ QÂ!Pf\x1d\nåòf\x91\x1d#S¾|\x9ff[d>O=T$ݶI\x9e»QÛÂ\x1d"¿U=û´F÷\x83C}wA\xa0É\x8aD\x93x»\x85\x7f\x14^\x0eL'
done:
100 generations
How do I exactly access the 100 from the method in the class?
import random
class GeneticAlgorithm(object):
def __init__(self, genetics):
self.genetics = genetics
pass
def run(self):
population = self.genetics.initial()
while True:
fits_pops = [(self.genetics.fitness(ch), ch) for ch in population]
if self.genetics.check_stop(fits_pops): break
population = self.next(fits_pops)
pass
return population
def next(self, fits):
parents_generator = self.genetics.parents(fits)
size = len(fits)
nexts = []
while len(nexts) < size:
parents = next(parents_generator)
cross = random.random() < self.genetics.probability_crossover()
children = self.genetics.crossover(parents) if cross else parents
for ch in children:
mutate = random.random() < self.genetics.probability_mutation()
nexts.append(self.genetics.mutation(ch) if mutate else ch)
pass
pass
return nexts[0:size]
pass
class GeneticFunctions(object):
def probability_crossover(self):
r"""returns rate of occur crossover(0.0-1.0)"""
return 1.0
def probability_mutation(self):
r"""returns rate of occur mutation(0.0-1.0)"""
return 0.0
def initial(self):
r"""returns list of initial population
"""
return []
def fitness(self, chromosome):
r"""returns domain fitness value of chromosome
"""
return len(chromosome)
def check_stop(self, fits_populations):
r"""stop run if returns True
- fits_populations: list of (fitness_value, chromosome)
"""
return False
def parents(self, fits_populations):
r"""generator of selected parents
"""
gen = iter(sorted(fits_populations))
while True:
f1, ch1 = next(gen)
f2, ch2 = next(gen)
yield (ch1, ch2)
pass
return
def crossover(self, parents):
r"""breed children
"""
return parents
def mutation(self, chromosome):
r"""mutate chromosome
"""
return chromosome
pass
if __name__ == "__main__":
"""
example: Mapped guess prepared Text
"""
class GuessText(GeneticFunctions):
def __init__(self, target_text,
limit=100, size=100,
prob_crossover=0.9, prob_mutation=0.2):
self.target = self.text2chromo(target_text)
self.counter = 0
self.limit = limit
self.size = size
self.prob_crossover = prob_crossover
self.prob_mutation = prob_mutation
pass
# GeneticFunctions interface impls
def probability_crossover(self):
return self.prob_crossover
def probability_mutation(self):
return self.prob_mutation
def initial(self):
return [self.random_chromo() for j in range(self.size)]
def fitness(self, chromo):
# larger is better, matched == 0
return -sum(abs(c - t) for c, t in zip(chromo, self.target))
def check_stop(self, fits_populations):
self.counter += 1
if self.counter % 100 == 0:
best_match = list(sorted(fits_populations))[-1][1]
fits = [f for f, ch in fits_populations]
best = -(max(fits))
ave = -(sum(fits) / len(fits))
print(
"Generation %3d, average fit %4d, best fit %4d" %
(self.counter, ave, best,
))
print("Your best chromosone at generation %3d" % self.counter)
print("%r" % self.chromo2text(best_match))
pass
return self.counter >= self.limit
def parents(self, fits_populations):
while True:
father = self.tournament(fits_populations)
mother = self.tournament(fits_populations)
yield (father, mother)
pass
pass
def crossover(self, parents):
father, mother = parents
index1 = random.randint(1, len(self.target) - 2)
index2 = random.randint(1, len(self.target) - 2)
if index1 > index2: index1, index2 = index2, index1
child1 = father[:index1] + mother[index1:index2] + father[index2:]
child2 = mother[:index1] + father[index1:index2] + mother[index2:]
return (child1, child2)
def mutation(self, chromosome):
index = random.randint(0, len(self.target) - 1)
vary = random.randint(-5, 5)
mutated = list(chromosome)
mutated[index] += vary
return mutated
# internals
def tournament(self, fits_populations):
alicef, alice = self.select_random(fits_populations)
bobf, bob = self.select_random(fits_populations)
return alice if alicef > bobf else bob
def select_random(self, fits_populations):
return fits_populations[random.randint(0, len(fits_populations)-1)]
def text2chromo(self, text):
return [ord(ch) for ch in text]
def chromo2text(self, chromo):
return "".join(chr(max(1, min(ch, 255))) for ch in chromo)
def random_chromo(self):
return [random.randint(1, 255) for i in range(len(self.target))]
pass
GeneticAlgorithm(GuessText("""The smartest and fastest Pixel yet.
Google Tensor: Our first custom-built processor.
The first processor designed by Google and made for Pixel, Tensor makes the new Pixel phones our most powerful yet.
The most advanced Pixel Camera ever.
Capture brilliant color and vivid detail with Pixels best-in-class computational photography and new pro-level lenses.""")).run()
print('done:')
print("%3d " 'generations' % counter)
pass
Define the GuessText first. Then access the counter.
gt = GuessText("""The smartest and fastest Pixel yet.
Google Tensor: Our first custom-built processor.
The first processor designed by Google and made for Pixel, Tensor makes the new Pixel phones our most powerful yet.
The most advanced Pixel Camera ever.
Capture brilliant color and vivid detail with Pixels best-in-class computational photography and new pro-level lenses.""")
GeneticAlgorithm(gt).run()
print('done:')
print("%3d " 'generations' % gt.counter)
I am using eval to run a generated string to append the newly created EggOrder instance to the list of the correct instance of the DailyOrders class. The day provided by EggOrder is used to used to append to the correct instance. This relies on eval and the variable name of the DailyOrders instance and so it would be great to get this removed. I know there must be a better way.
class DailyOrders:
PRICE_PER_DOZEN = 6.5
def __init__(self, day):
self.orders = []
self.day = day
def total_eggs(self):
total_eggs = 0
for order in self.orders:
total_eggs += order.eggs
return total_eggs
def show_report(self):
if self.total_eggs() < 0:
print("No Orders")
else:
print(f"Summary:\nTotal Eggs Ordered: {self.total_eggs()}")
print(f"Average Eggs Per Customer: {self.total_eggs() / len(self.orders):.0f}\n*********")
class EggOrder():
def __init__(self, eggs=0, name="", day=""):
if not name:
self.new_order()
else:
self.name = name
self.eggs = eggs
self.day = day
eval(f"{self.day.lower()}.orders.append(self)")
def new_order(self):
self.name = string_checker("Name: ")
self.eggs = num_checker("Number of Eggs: ")
self.day = string_checker("Date: ")
def get_dozens(self):
if self.eggs % 12 != 0:
dozens = int(math.ceil(self.eggs / 12))
else:
dozens = self.eggs / 12
return dozens
def show_order(self):
print(f"{self.name} ordered {self.eggs} eggs. The price is ${self.get_dozens() * DailyOrders.PRICE_PER_DOZEN}.")
if __name__ == "__main__":
friday = DailyOrders("Friday")
friday_order = EggOrder(12, "Someone", "Friday")
friday_order.show_order()
friday.show_report()
saturday = DailyOrders("Saturday")
saturday_order = EggOrder(19, "Something", "Saturday")
saturday_order = EggOrder(27, "Alex Stiles", "Saturday")
saturday.show_report()
DailyOrders isn't actually a superclass (it was in a earlier version), it acts like one and I suspect the answer might have some inheritance.
I am trying to call an object attribute with a variable based on user input. This is for a text game. The idea is the player types in 'hat', for example, and this function checks the player object's self.inventory = [] for Item objects named 'hat', and if such an object exists it will append the Item object to the player object's corresponding attribute, in this case self.helm = [].
To do this I am trying to have equipItem be a variable equal to the equipping item's class type which should be the same as one of the player objects' equipment attributes.
move = input("To equip, unequip, or an use item, type [item name]. To trade
in item for XP, type 't [item name]'. Type 'x' to exit inventory:
").lower().split()
print(move[0])
for i in currentPlayer.inventory:
print(i.name)
if i.name == move[0]:
print(i)
equipItem = i.itemClass
currentPlayer.equipItem.append(i)
currentPlayer.inventory.remove(i)
print(currentPlayer.equipItem)
But of course this throws an error because 'equipItem' in not a player attribute. So my question is how might I achieve the result I am after? Can I use a variable to call object attributes?
I understand this is all probably quite janky and I am not married to this approach by any means, so any suggestions other approaches are welcome as well!
Player class and Item class below:
class Player:
def __init__(self, name):
self.name = name
self.health = 10
self.maxHealth = 10
self.inventory = [Item(1, 1), Item(2, 2)]
self.weapon = [Item(0, 1)]
self.helm = [Item(1, 0)]
self.shield = [Item(5, 0)]
self.boots = [Item(3, 0)]
self.greaves = [Item(4, 0)]
self.gloves = [Item(6, 0)]
self.cuirass = [Item(2, 0)]
self.defaultPotion = []
self.attack = 5 + self.weapon[0].attack
armorClass = self.helm[0].defense + self.shield[0].defense +
self.boots[0].defense + self.greaves[0].defense +
self.gloves[0].defense
+ self.cuirass[0].defense
self.defense = 1 + armorClass
self.level = 1
self.experience = 0
self.offensiveMagic = 0
self.defenseiveMagic = 0
self.offensiveCombat = 0
self.weightClass = 10
def __str__(self):
return self.name
class Item:
def __init__(self, itemType, subType):
#denotes type of item (wepaon, gloves, etc)
self.itemType = allItems[itemType]
#denotes item name and stats (sword, attack value, etc)
self.subType = self.itemType[subType]
#denotes how item is used (euip, consume, etc)self.subType = 0:
if subType == 0:
self.itemClass = 'weapon'
elif subType == 1:
self.itemClass = 'helm'
elif subType == 2:
self.itemClass = 'cuirass'
elif subType == 3:
self.itemClass = 'boots'
elif subType == 4:
self.itemClass = 'greaves'
elif subType == 5:
self.itemClass = 'shield'
elif subType == 6:
self.itemClass = 'gloves'
self.name = self.subType['name']
self.attack = self.subType['attack']
self.defense = self.subType['defense']
# self.equip = 'no'
def __str__(self):
return self.itemType
return self.name
return self.attack
return self.defense
To get an object's attribute by name, use getattr builtin function. For example: getattr(currentPlayer, equipItem).append(i). To assign to an attribute, use setattr.
Okay, I'm trying to migrate some similar classes underneath a parent to make modifying all the classes at once easier. This is my code:
class CreditCard():
def __init__(self, name, short, tag, length):
self.name = name
self.short = short
self.tag = tag
self.length = length
self.CCnumber = tag
while (len(self.CCnumber) < self.length - 1):
rand_int = random.randrange(10)
rand_str = str(rand_int)
self.CCnumber = self.CCnumber + rand_str
if (length == 15):
d = CheckSumDigit(self.CCnumber)
self.CCnumber = self.CCnumber + d
if (not RigorousVerifyLuhn(self.CCnumber)):
ln = self.length - 1
clip = self.CCnumber[0:ln]
fulfilled = False
dig = 0
while (dig <= 9 and fulfilled == False):
cand = clip + str(dig)
if (RigorousVerifyLuhn(cand)):
fulfilled = True
self.CCnumber = cand
dig = dig + 1
if (fulfilled == False):
if (len(self.CCnumber) != self.length):
print("Invalid " + self.name + " number, LENGTH " + len(self.CCnumber) + " (" + self.CCnumber + ")")
else:
print("Invalid " + self.name + " number, LUHN " + "(" + self.CCnumber + ")")
class AmexCreditCard(CreditCard):
def __init__(self):
self.NAME = 'American Express'
self.SHORT = 'AMEX'
self.TAG = '3'
self.LENGTH = 15
CreditCard.__init__(self, 'American Express', 'AMEX', '3', 15)
class VisaCreditCard():
def __init__(self):
self.NAME = 'Visa'
self.SHORT = 'VISA'
self.TAG = '4'
self.LENGTH = 16
CreditCard.__init__(self, self.NAME, self.SHORT, self.TAG, self.LENGTH)
class MasterCardCreditCard():
def __init__(self):
CreditCard.__init__(self, 'MasterCard', 'MC', '5', 16)
class DiscoverCreditCard():
def __init__(self):
CreditCard.__init__(self, 'Discover', 'DISC', '6011', 16)
I have several different styles of syntax on the child credit cards because I'm not sure of the proper syntax for it. When I run this, I get this error:
TypeError: unbound method __init__() must be called with CreditCard instance as first argument (got VisaCreditCard instance instead)
Can anyone help me fix this error? Thank you!
You forgot to inherit from CreditCard:
class VisaCreditCard(CreditCard):
The same applies to your MasterCardCreditCard and DiscoverCreditCard classes.
I'm working on getting better with OOP in Python and I've run into some real hackishness in one program I'm writing. It works, but it's a mess.
Below is a short test example to illustrate. It creates cars of either 0, 2, or 4 windows into a list, and then compares the first element with the rest of the list.
The 3rd method of the first class shows what I'm worried about. I just want to be able to refer to whatever container that particular object is in without having to call it from the parameters each time. It isn't even that bad in this example, but what I'm working on has it in so many places that it's starting to get confusing.
import random
class Car:
def __init__ (self, company, doors, id):
self.company = company
self.doors = doors
self.id = id
def printDoors(self, id):
print 'Car ' + `self.id` + ' has ' + `self.doors` + ' doors.'
def findSameDoors(self, id):
# these next lines are the ones that really bother me
companyAbstract = self.company + 's'
for i in eval(companyAbstract):
if self.id != i.id and self.doors == i.doors:
print 'Car ' + `i.id` + ' does too!'
class Company:
def __init__ (self, types):
self.types = types
def typesToNum(self):
result = []
for i in self.types:
if i == 'sedan':
result.append(4)
elif i == 'convertible':
result.append(2)
else:
result.append(0)
return result
porsche = Company(['sedan', 'convertible'])
honda = Company(['sedan', 'convertible', 'motorcycle'])
porsches = []
for i in range(10):
porsches.append(Car('porsche', random.choice(porsche.typesToNum()), i))
hondas = []
for i in range(10):
hondas.append(Car('honda', random.choice(honda.typesToNum()), i))
porsches[0].printDoors(0)
porsches[0].findSameDoors(0)
Just in case it matters, Python 2.4.3 on RHEL. Thanks!
If I'm understanding your question right, you want to attach the list of cars to the company object:
import random
class Car:
def __init__ (self, company, doors, id):
self.company = company
self.doors = doors
self.id = id
def printDoors(self, id):
print 'Car ' + `self.id` + ' has ' + `self.doors` + ' doors.'
def findSameDoors(self, id):
for i in self.company.cars:
if self.id != i.id and self.doors == i.doors:
print 'Car ' + `i.id` + ' does too!'
class Company:
def __init__ (self, types):
self.types = types
self.cars = []
def typesToNum(self):
result = []
for i in self.types:
if i == 'sedan':
result.append(4)
elif i == 'convertible':
result.append(2)
else:
result.append(0)
return result
porsche = Company(['sedan', 'convertible'])
honda = Company(['sedan', 'convertible', 'motorcycle'])
for i in range(10):
porsche.cars.append(Car(porsche, random.choice(porsche.typesToNum()), i))
for i in range(10):
honda.cars.append(Car(honda, random.choice(honda.typesToNum()), i))
porsche.cars[0].printDoors(0)
porsche.cars[0].findSameDoors(0)
There's more cleanup that could be done to it, but I think that should solve your immediate concern.