def cats_to_json(): tina = Cat(name="Tina", age=5) klakier = Cat(name="Klakier", age=2) rudy = Cat(name="Rudy", age=1) cat_list = [] cat_list.append(tina.__dict__) cat_list.append(klakier.__dict__) cat_list.append(rudy.__dict__) return jsonify(cat_list)
def add_Cat(self): newcat = Cat() newcat.put() self.Mypets.append(newcat)
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()
from animals import Animal, Person, Cat if __name__ == '__main__': animal1 = Animal(10) animal1.set_name('Spot') # animal1.speak() # cat1 = Cat(5) # print(cat1) # cat1.speak() # person1 = Person("Kim", 25) # person1.add_friend('Jane') # person1.add_friend('Raj') # print(f'{str(person1)} has {len(person1.friends)} friends') cat1 = Cat(5) cat2 = Cat(5) cat3 = Cat(6) print(cat1 == cat2) print(cat2 == cat3) person1 = Person("Kim", 25) person2 = Person("Jane", 22) person1.age_diff(person2)
""" 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)
#!/usr/bin/python # module_attr.py from animals import Cat class Being: pass b = Being() print(b.__module__) c = Cat() print(c.__module__)
from animals import Cat garfield = Cat('Garfield', '1/1/1978', 'Orange', 'Tabby') garfield.printit() print(garfield)
def test_greet2(self): cat = Cat(name="Panienka", age=2.5) self.assertEqual(cat.greet(), "Hello, my name is Panienka. I am 2.5 years old.")
def test_greet1(self): cat = Cat(name="Connie", age=4) self.assertEqual(cat.greet(), "Hello, my name is Connie. I am 4 years old.")
def test_check_old(self): cat = Cat (name="Choco", age=4) self.assertEqual(cat.get_oldness(), Oldness.OLD)
def test_check_young(self): cat = Cat (name="Rudy", age=1) self.assertEqual(cat.get_oldness(), Oldness.YOUNG)
def test_check_little(self): cat = Cat(name="Bulcyngier", age=0.5) self.assertEqual(cat.get_oldness(), Oldness.LITTLE)