def setUp(self):

        # Redirect standard out to captured_output.
        # Since methods sleep(), bark(), and meow() print output, rather than
        # merely returning a string, it is necessary to capture standard out.
        self.captured_output = io.StringIO()
        sys.stdout = self.captured_output

        # Setup Dogs
        self.fido = Dog('Fido', 'Golden Retriever')
        self.fido.add_offspring(Dog('Spot', 'Goldendoodle'))
        self.fido.add_offspring(Dog('Rover', 'Goldendoodle'))

        # Setup Cats
        self.morris = Cat('Morris', 'orange')
        self.morris.add_offspring(Cat('Tiger', 'gray'))
        self.felix = Cat('Felix', 'black')
        self.morris = Cat('Morris', 'orange')
        self.tiger = Cat('Tiger', 'gray')
        self.mongo = Cat('Mongo', 'white')
        self.felix.add_offspring(self.morris)
        self.morris.add_offspring(self.tiger)
        self.tiger.add_offspring(self.mongo)
import yaml
from Animal import Cat, Dog, main_cat, main_dog

if __name__ == '__main__':

    with open(r'./data_animal.yml') as f:
        data = yaml.safe_load(f)
        mimi = Cat(**data['cat'])
        main_cat(mimi)
        wangcai = Dog(**data['dog'])
        main_dog(wangcai)
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
from Animal import Animal, Dog, Cat

print('type(123) = ', type(123))
print('type("abc") = ', type("abc"))
print('type([1,2,3]) = ', type([1, 2, 3]))
print('type((1,2,3)) = ', type((1, 2, 3)))
print('type({1:1,2:2}) = ', type({1: 1, 2: 2}))
print('type({1,2,3}) = ', type({1, 2, 3}))
print('type(Animal()) = ', type(Animal()))
print('type(Dog()) = ', type(Dog()))

animal = Animal()
dog = Dog()
cat = Cat()
print('animal is Animal?', isinstance(animal, Animal))
print('dog is Animal?', isinstance(dog, Animal))
print('cat is Animal?', isinstance(cat, Animal))
print('cat is Cat?', isinstance(cat, Cat))
print('cat is Dog?', isinstance(cat, Dog))

print(dir(animal))

print(hasattr(animal, 'run'))
print(hasattr(animal, 'test'))
print(hasattr(animal, 'test'))
print(getattr(animal, 'run'))
print(getattr(animal, 'test', 'none'))
Exemple #4
0
from Solution import Solution2
from Animal import Dog, Cat


# if __name__ == '__main__':
#     solution = Solution2(1,2)
#     result = solution.test([1,3,5,7], 2)
#     print(result)

if __name__ == '__main__':
    A = Dog("小黄", 2)
    result = A.set()
    print(A)

class AnimalTestCase(unittest.TestCase):

    # Perform setup common to multiple tests - DRY
    def setUp(self):

        # Redirect standard out to captured_output.
        # Since methods sleep(), bark(), and meow() print output, rather than
        # merely returning a string, it is necessary to capture standard out.
        self.captured_output = io.StringIO()
        sys.stdout = self.captured_output

        # Setup Dogs
        self.fido = Dog('Fido', 'Golden Retriever')
        self.fido.add_offspring(Dog('Spot', 'Goldendoodle'))
        self.fido.add_offspring(Dog('Rover', 'Goldendoodle'))

        # Setup Cats
        self.morris = Cat('Morris', 'orange')
        self.morris.add_offspring(Cat('Tiger', 'gray'))
        self.felix = Cat('Felix', 'black')
        self.morris = Cat('Morris', 'orange')
        self.tiger = Cat('Tiger', 'gray')
        self.mongo = Cat('Mongo', 'white')
        self.felix.add_offspring(self.morris)
        self.morris.add_offspring(self.tiger)
        self.tiger.add_offspring(self.mongo)

    # Cleanup
    def tearDown(self):
        self.fido = None
        self.morris = None
        self.felix = None
        self.tiger = None
        self.mongo = None

    def test_dog_sleep(self):
        self.fido.sleep()
        # Append \n because print() writes \n to standard out.
        self.assertEqual(self.captured_output.getvalue(), 'Fido sleeps\n')

    def test_dog_bark(self):
        self.fido.bark()
        self.assertEqual(self.captured_output.getvalue(), 'Fido barks\n')

    def test_dog_breed(self):
        self.assertEqual(self.fido.breed, 'Golden Retriever')

    def test_cat_sleep(self):
        self.morris.sleep()
        # Append \n because print() writes \n to standard out.
        self.assertEqual(self.captured_output.getvalue(), 'Morris sleeps\n')

    def test_cat_meow(self):
        self.morris.meow()
        self.assertEqual(self.captured_output.getvalue(), 'Morris meows\n')

    def test_cat_hair_color(self):
        self.assertEqual(self.morris.hair_color, 'orange')

    def test_dog_offspring_sleep_0(self):
        self.fido.offspring[0].sleep()
        self.assertEqual(self.captured_output.getvalue(), 'Spot sleeps\n')

    def test_dog_offspring_sleep_1(self):
        self.fido.offspring[1].sleep()
        self.assertEqual(self.captured_output.getvalue(), 'Rover sleeps\n')

    def test_dog_offspring_bark_0(self):
        self.fido.offspring[0].bark()
        self.assertEqual(self.captured_output.getvalue(), 'Spot barks\n')

    def test_dog_offspring_bark_1(self):
        self.fido.offspring[1].bark()
        self.assertEqual(self.captured_output.getvalue(), 'Rover barks\n')

    def test_dog_offspring_breed_0(self):
        self.assertEqual(self.fido.offspring[0].breed, 'Goldendoodle')

    def test_dog_offspring_breed_1(self):
        self.assertEqual(self.fido.offspring[1].breed, 'Goldendoodle')

    def test_cat_offspring_sleep(self):
        self.morris.offspring[0].sleep()
        self.assertEqual(self.captured_output.getvalue(), 'Tiger sleeps\n')

    def test_cat_offspring_meow(self):
        self.morris.offspring[0].meow()
        self.assertEqual(self.captured_output.getvalue(), 'Tiger meows\n')

    def test_cat_offspring_hair_color(self):
        self.assertEqual(self.morris.offspring[0].hair_color, 'gray')

    def test_cat_pedigree_0(self):
        self.mongo.print_pedigree()
        self.assertEqual(self.captured_output.getvalue(),
                         'Felix, Morris, Tiger, Mongo\n')

    ##############################
    #  Exceptions and edge cases
    ##############################

    # Exception: Parent Dog given offspring Cat
    def test_animal_offspring_value_error_0(self):
        self.assertRaises(TypeError, self.fido.add_offspring,
                          Cat('Salem', 'black'))

    # Exception: Parent Cat given offspring Dog
    def test_animal_offspring_value_error_1(self):
        self.assertRaises(TypeError, self.morris.add_offspring,
                          Dog('Lassie', 'Collie'))

    # Exception: Attempt to set Dog.breed
    def test_dog_readonly_0(self):
        with self.assertRaises(AttributeError):
            self.fido.breed = 'Belgian Malinois'

    # Exception: Attempt to set Cat.hair_color
    def test_cat_readonly_0(self):
        with self.assertRaises(AttributeError):
            self.morris.hair_color = 'white'

    # Edge Case: Ensure there is not an error when the pedigree has only one member.
    def test_animal_pedigree_1(self):
        self.fido.print_pedigree()
        self.assertEqual(self.captured_output.getvalue(), 'Fido\n')
 def test_animal_offspring_value_error_1(self):
     self.assertRaises(TypeError, self.morris.add_offspring,
                       Dog('Lassie', 'Collie'))
Exemple #7
0
# -*- coding: utf-8 -*-
"""
Created on Mon May 22 18:05:58 2017

@author: Ahmed Usama Khalifa
"""
from Animal import Dog

Lasse = Dog(n=['Lasse'], c=['Brown'], Ec=['Red', 'Black'], N='Jp', H=80)
Marf = Dog(n=['Marf'],
           c=['Brown'],
           Ec=['Red', 'Black'],
           N='US',
           H=50,
           L=60,
           W=70)
Saso = Dog(n=['Saso'], c=['White'], Ec=['Orange', 'blue'], N='Egy', H=40, L=40)

print(Lasse.Hight)
Lasse.sit()
Lasse.come('NY HAW')
Marf.come('Come here')
Saso.come('Come here')
print(Dog.number_of_dogs)
Exemple #8
0
class Zoo(object):
    def __init__(self, name):
        self.name = name
        self.__animals = set()

    def add_animal(self, ani):
        if hasattr(self, ani.__class__.__name__):
            print(f"the zoo {self.name} has {ani.__class__.__name__} already.")
        else:
            setattr(self, ani.__class__.__name__, True)
            self.__animals.add(ani)


if __name__ == '__main__':
    z = Zoo('小嘿动物园')
    cat1 = Cat('大花猫 1', '食肉', '小', '温顺')
    z.add_animal(cat1)
    have_cat = hasattr(z, 'Cat')
    print(f"the zoo {z.name} has a Cat:{have_cat}")
    print(f"{cat1.name} is ferocious? {cat1.is_ferocious}")
    print(f"{cat1.name} is ok as a pet? {cat1.is_ok_pet}")

    dog1 = Dog('阿黄', '食肉', '中等', '温顺的')
    z.add_animal(dog1)
    print(hasattr(z, "Dog"))
    print(f"the zoo {z.name} has a Dog:{have_cat}")

    dog2 = Dog('大黄', '食肉', '中等', '凶猛')
    z.add_animal(dog2)