def test_dog_walk(self): self.doggo = Dog("Doggo") self.doggo.walk() self.assertEqual(0, self.bob.speed) self.doggo.set_legs(4) self.doggo.walk() self.assertEqual(self.doggo.speed, .8)
def test_dog_walks_at_speed(self): # creating a separate test class here because there is no "stop walking" method. self.bill = Dog('Bill') self.bill.set_legs(4) self.assertEqual(self.bill.speed, 0) self.bill.walk() self.assertEqual(self.bill.speed, (self.bill.speed + (0.2 * self.bill.legs)))
class TestAnimal(unittest.TestCase): @classmethod def setUpClass(self): self.bob = Dog("Bob") self.jacob = Animal("Jacob") def test_animal_creation(self): murph = Dog("Murph") self.assertIsInstance(murph, Dog) self.assertIsInstance(murph, Animal) self.assertIsInstance(self.jacob, Animal) def test_animal_can_set_legs(self): self.bob.set_legs(6) self.assertEqual(self.bob.legs, 6) def test_animal_can_get_species(self): self.assertIs(self.bob.get_species(), "Dog") def test_animal_can_set_species(self): self.bob.set_species("Poodle") self.assertIs(self.bob.species, "Poodle") def test_setting_speed(self): self.bob.set_legs(6) self.bob.walk() self.assertEqual(self.bob.speed, 1.20) def test_animal_get_name(self): self.assertEqual(self.bob.name, "Bob")
class TestAnimal(unittest.TestCase): @classmethod def setUpClass(self): self.bob = Dog("Bob") def test_animal_creation(self): bob = Dog("Bob") self.assertIsInstance(bob, Dog) self.assertIsInstance(bob, Animal) def test_animals_can_set_legs(self): self.bob.set_legs(6) self.assertEqual(self.bob.legs, 6)
def test_animal_creation(self): murph = Dog("Murph") self.assertIsInstance(murph, Dog) self.assertIsInstance(murph, Animal) self.assertIsInstance(self.jacob, Animal)
def setUpClass(self): self.bob = Dog("Bob") self.jacob = Animal("Jacob")
DrobnyMuzikSTaskou.inventory['taška'].append('celý lup') DrobnyMuzikSTaskou.disappear() Venku.spawn(DrobnyMuzikSTaskou) del (Zalozna.DrobnyMuzikSTaskou) time.sleep(0) Zalozna.spawn(Trachta(experience=float('infinity'))) repr(Trachta) Trachta.say( SlecnaTicha, 'Žádné strachy, slečno Tichá.'.join( ['', 'Však my si ho najdeme', 'My mu tipec zatneme.'])) SlecnaTicha.worry(False) for b in [0, random.choice((2, 3))]: Trachta.whistle.blow() Doubrava = Dog(sex='fenka', name='Doubrava') Zalozna.spawn(Doubrava) Doubrava.sniff().sniff() repr(Doubrava) for i in [Trachta, Doubrava, SlecnaTicha]: Venku.spawn(i) del (Zalozna.i) Venku.spawn([Blato(), Strom()]) Trachta.say(Doubrava, 'Čile za ním!') Doubrava.ignore().procrastinate() chlapci = [] for m in range(0, 6): chlapec = Man(name='Chlapec #%d' % m)
from animals import Dog from animals import Cat from animals import Bird dude = Dog('Dude', '1/1/2011', 'Brown', 'Jack Russell') oogs = Cat('Oogie', '1/1/2006', 'Grey', 'Fluffy') bbird = Bird('Big Bird', '10/11/1969', 'Yellow', 'Canary') dude.printit() oogs.printit() bbird.printit()
def test_animal_creation(self): bob = Dog("Bob") self.assertIsInstance(bob, Dog) self.assertIsInstance(bob, Animal)
from animals import Dog d3 = Dog("Old Yeller", 10) print d3.run()
def fn(): pass print(type(fn) == types.FunctionType) print(type(abs) == types.BuiltinFunctionType) print(type(lambda x: x) == types.LambdaType) print(type((x for x in range(10))) == types.GeneratorType) # 使用isinstance() class Husky(Dog): def run(): print("Husky is running...") d = Dog() h = Husky() print(isinstance(h, Husky)) print(isinstance(h, Dog)) print(isinstance(h, Animal)) print(isinstance('a', str)) print(isinstance(123, int)) print(isinstance([1, 2, 3], (list, tuple))) print(isinstance((1, 2, 3), (list, tuple))) print(dir('ABC')) print(len('ABC')) print('ABC'.__len__())
def setUpClass(self): self.bob = Dog('Bob')
class TestAnimal(unittest.TestCase): @classmethod def setUpClass(self): self.bob = Dog('Bob') def test_animal_exists(self): self.assertIsInstance(self.bob, Animal) self.assertIsInstance(self.bob, Dog) def test_animal_can_set_legs(self): self.bob.set_legs(6) self.assertEqual(self.bob.legs, 6) def test_animal_has_correct_name(self): self.bob.get_name() self.assertEqual(self.bob.name, 'Bob') def test_animal_has_correct_species(self): self.bob.set_species('C. lupus') self.assertEqual(self.bob.species.lower(), 'c. lupus') self.assertEqual(self.bob.get_species().lower(), 'c. lupus') def test_animal_returns_correct_str(self): self.assertEqual(self.bob.__str__().lower(), 'bob is a c. lupus') def test_dog_walks_at_speed(self): # creating a separate test class here because there is no "stop walking" method. self.bill = Dog('Bill') self.bill.set_legs(4) self.assertEqual(self.bill.speed, 0) self.bill.walk() self.assertEqual(self.bill.speed, (self.bill.speed + (0.2 * self.bill.legs)))
def setUpClass(): critter = Animal() scooby = Dog(Animal) print(scooby)
from mathdojo import MathDojo from hospital import Patient from hospital import Hospital from car import Car from callcenter import Call from callcenter import CallCenter from bike import Bike from animals import Animal from animals import Dog from animals import Dragon md = MathDojo(0) print md hosp = Hospital("Great Hospital", 6) print hosp patient = Patient(1, "Bob") print patient car = Car(5000, "75mph", "Full", "25mpg") print car call = Call(1, "Stephen", "770-789-7038", 8.30, "For Fun") print call call_center = CallCenter() print call_center bike = Bike(150, "15mph") print bike fox = Animal("Fox") print fox pitbull = Dog("Pitbull") print pitbull trogdor = Dragon("Trogdor the Burninator") print trogdor
from animals import Dog myDog = Dog("Pichula", "Orejero Aleman", 5, 30) myDog.bark() print(myDog.getName()) print(myDog.getRace()) print(myDog.getAge()) print(myDog.getWeigth()) myDog.eat() myDog.age() myDog.bark() print(myDog.getName()) print(myDog.getRace()) print(myDog.getAge()) print(myDog.getWeigth()) myDog.poop(2) print(myDog.getWeigth())
class TestAnimal(unittest.TestCase): @classmethod def setUpClass(self): self.bob = Dog("Bob") self.rob = Animal("Rob") def test_animal_creation(self): bob = Dog("Bob") self.assertIsInstance(bob, Animal) self.assertIsInstance(bob, Dog) def test_animal_can_set_legs(self): self.bob.set_legs(6) self.assertEqual(self.bob.legs, 6) def test_get_name(self): self.assertEqual(self.bob.get_name(), "Bob") def test_dog_walk(self): self.doggo = Dog("Doggo") self.doggo.walk() self.assertEqual(0, self.bob.speed) self.doggo.set_legs(4) self.doggo.walk() self.assertEqual(self.doggo.speed, .8) def test_animal_walk(self): self.animal = Animal() with self.assertRaises(ValueError): self.animal.walk() self.animal.set_legs(2) self.animal.walk() self.assertEqual(self.animal.speed, .2) def test_species(self): self.bob.set_species("dog") self.assertEqual(self.bob.get_species(), "dog")
def setUpClass(self): self.bob = Dog("Bob")
def setUpClass(self): self.bob = Dog("Bob") self.rob = Animal("Rob")
class TestAnimal(unittest.TestCase): @classmethod def setUpClass(self): self.bob = Dog("Bob") def test_animal_creation(self): bob = Dog("Bob") self.assertIsInstance(bob, Dog) self.assertIsInstance(bob, Animal) def test_animal_can_set_legs(self): self.bob.set_legs(6) self.assertEqual(self.bob.legs, 6) def test_animal_set_species(self): self.bob.set_species("Yorkie Poo") self.assertEqual(self.bob.species, "Yorkie Poo") def test_animal_get_species(self): self.bob.set_species("Yorkie Poo") self.bob.get_species() self.assertEqual(self.bob.species, "Yorkie Poo") def test_animal_speed(self): self.bob.set_legs(7) self.bob.walk() self.assertEqual(self.bob.speed, 1.4)
""" Method overloading. Create a base class named Animal with a method called talk and then create two subclasses: Dog and Cat, and make their own implementation of the method talk be different. For instance, Dog’s can be to print ‘woof woof’, while Cat’s can be to print ‘meow’. Also, create a simple generic function, which takes as input instance of a Cat or Dog classes and performs talk method on input parameter. """ from animals import Dog, Cat, animal_talk if __name__ == "__main__": dog = Dog() cat = Cat() animal_talk(dog) animal_talk(cat)
def add_Dog(self): newdog = Dog() newdog.put() self.Mypets.append(newdog)
animal_classes = [Parrot, Penguin, Dog, HouseCat, BobCat] colors = ["White", "Black", "Red", "Green", "Blue", "Striped"] habitats = ["Land", "Sea", "Air", "Tree", "Campus"] random_true_false = [True, False] zoo = [] zoo_size = 10 for _ in range(zoo_size): spec = animal_classes[random.randint(0, 4)] age = random.randint(1, 10) color = random.choice(colors) habitat = random.choice(habitats) if spec == Parrot: new_animal = Parrot(age, color, random.choice(random_true_false)) elif spec == Penguin: new_animal = Penguin(age, color) elif spec == Dog: new_animal = Dog(age, color) elif spec == HouseCat: new_animal = HouseCat(age, color) else: # BobCat try: new_animal = BobCat(age, color, habitat) except ValueError as value_error: print(habitat + " is " + str(value_error)) continue zoo.append(new_animal) print("There are {} animals in the zoo".format(len(zoo))) for animal in zoo: print('{} says {}'.format(str(animal), animal.sound()))