class DogTest(unittest.TestCase): def setUp(self): self.default_dog = Dog() self.custom_dog = Dog(name='Mickey', species='Lab') def test_defaults(self): self.assertEqual(self.default_dog.name, 'Spot') self.assertEqual(self.default_dog.species, 'Dalmation') self.assertEqual(self.custom_dog.name, 'Mickey') self.assertEqual(self.custom_dog.species, 'Lab') def test_bark(self): self.assertEqual(self.default_dog.bark(), 'Woof!') self.assertEqual(self.custom_dog.bark(), 'Woof!') def test_run(self): self.assertEqual(self.default_dog.run(), 'Vroom!') # Doesn't work! Printing is a side effect
#my_dogs.py from dog import Dog my_dog = Dog("Rex", "Superdog") my_other_dog = Dog("Annie", "Superdog") my_other_other_dog = Dog("Rufus", "German Shepherd") my_dog.bark() my_other_dog.sit() my_other_other_dog.rollOver()
#!/usr/bin/env python from cat import Cat from dog import Dog if __name__ == '__main__': dog = Dog('Bello') print(dog) # <Dog named 'Bello'> print(dog.name) # Bello dog.bark() # woof woof! cat = Cat() dog.chase(cat) # woof woof! # meow!
"""Import modules.""" from dog import Dog aardvark = Dog('Aardvark', 'Pit Bull') aardvark.bark() annie = Dog('Annie', 'Border Collie') annie.bark()
# how to import # This runs everything in dog.py from dog import Dog dog = Dog("DJ", "black and white") dog.bark(20)
# my_dogs.py from dog import Dog # we need to specify exactly what we want my_dog = Dog("Rex", "SuperDog") my_dog.bark() my_other_dog = Dog("Annie", "SuperDog") my_other_dog.bark() another_dog = Dog("Cane", "Shadow Hound") another_dog.sit() hound = Dog("Mordicai", "Mordicainian's Faithful Hound") hound.roll_over()
# ----------------- # my_dogs.py # ----------------- from dog import Dog # Instantiation of Dog my_dog = Dog("Rex", "SuperDog") my_dog.bark() my_other_dog = Dog("Annie", "SuperDog") print(my_other_dog.name) dog_2 = Dog("Matt", "SuperDog") dog_2.bark() dog_3 = Dog("Chris", "SuperDog") dog_3.sit() dog_4 = Dog("Alex", "SuperDog") dog_4.roll_over()
from dog import Dog my_dog = Dog("Rex", "SuperDog") my_dog.bark() my_other_dog = Dog("Annie", "SuperDog") print(my_other_dog.name) dog1 = Dog("Milo", "Rad Dog") dog2 = Dog("Little Brother", "DogDog") dog3 = Dog("Puppers", "Pup Dog") dog1.bark() dog2.sit() dog3.rollover() # Dog.greeting = "Woah"
#!/usr/bin/env python3 # -*- coding=utf-8 -*- from cat import Cat from dog import Dog cat = Cat('囧爷', 'brown', 2) dog1 = Dog.age('wang', 'brown', 2) dog = Dog('', 'yellow') print(cat) print(dog1) cat() Dog.bark('wang wang wang') cat.introduce() dog.introduce() cat.set_name('囧大爷') print(cat.get_name()) cat.set_color(None) print(not isinstance(cat.get_color(), str) or not cat.get_color()) cat.introduce() cat.run() dog.run() print(dir(cat)) print(hasattr(cat, '_Animal__name')) # true try: cat.test2 = 'test failed' except Exception as e: print('Exception: ', e)
# my_dogs.py from dog import Dog my_dog1 = Dog("Bubbles", "Maltipoo") print(f"My dog's name is {my_dog1.name}, she is a {my_dog1.breed}") my_dog1.bark() my_dog2 = Dog("Bopper", "Corgi") print(f"My dog's name is {my_dog2.name}, they are a {my_dog2.breed}") my_dog2.sit() my_dog3 = Dog("Buddy", "Jack Russell-Shih Tzu") print(f"My dog's name is {my_dog3.name}, he is a {my_dog3.breed}") my_dog3.roll_over()
class Gui(Frame): """class gui for creating UI frame""" __colors = ["white", "black", "red", "yellow", "green", "cyan", "blue", "magenta", "orange", "brown", "pink", "grey"] def __init__(self, parent): """initialize gui itself""" Frame.__init__(self, parent, background="white") self.parent = parent self.parent.title("Test Gui") self.pack(fill=BOTH, expand=1) self.__dog = Dog self.__name_entry = MaxLengthEntry self.__first_number_entry = IntegerEntry self.__second_number_entry = IntegerEntry self.__text_label = Label self.__x_label = Label self.__ok_calc_button = Button self.__go_racing_button = Button self.__give_food_button = Button self.__give_drink_button = Button self.__center_window() self.__init_ui() def __center_window(self): """define position and size of gui""" w = 500 h = 300 sw = self.parent.winfo_screenwidth() sh = self.parent.winfo_screenheight() x = (sw - w) / 2 y = (sh - h) / 2 self.parent.geometry('%dx%d+%d+%d' % (w, h, x, y)) def __init_ui(self): """initialize UI components""" # button "Wijzig achtergrondskleur" change_button = Button(self.parent, text="Wijzig achtergrondskleur", command=self.__change_button_on_click) change_button.place(x=50, y=50) # entry (textbox) for dog's name input self.__name_entry = MaxLengthEntry(self.parent, maxlength=30, width=30) self.__name_entry.place(x=50, y=104) # button "OK" for admitting dog's name input ok_name_button = Button(self.parent, text="OK", command=self.__ok_name_button_on_click, width=10) ok_name_button.place(x=300, y=100) # label for showing text to user self.__text_label = Label(self.parent, text="Hoe heet je hond?", background="white", wraplength=400) self.__text_label.place(x=50, y=204) # label "X" between entries for input numbers self.__x_label = Label(self.parent, text="X", background="white") self.__x_label.place(x=428, y=54) self.__x_label.lower() # entry for first number input self.__first_number_entry = IntegerEntry(self.parent, width=2) self.__first_number_entry.place(x=400, y=54) self.__first_number_entry.lower() # entry for second number input self.__second_number_entry = IntegerEntry(self.parent, width=2) self.__second_number_entry.place(x=450, y=54) self.__second_number_entry.lower() # button "OK, reken!" for making dog to do math self.__ok_calc_button = Button(self.parent, text="OK, reken!", command=self.__ok_math_button_on_click, width=10) self.__ok_calc_button.place(x=400, y=100) self.__ok_calc_button.lower() # button "Ga rennen!" for making dog to go racing self.__go_racing_button = Button(self.parent, text="Ga rennen!", command=self.__go_racing_button_on_click, width=10) self.__go_racing_button.place(x=200, y=150) self.__go_racing_button.lower() # button "Eten!" for giving food to dog self.__give_food_button = Button(self.parent, text="Eten!", command=self.__give_food_button_on_click, width=10) self.__give_food_button.place(x=300, y=150) self.__give_food_button.lower() # button "Drinken!" for giving drink to dog self.__give_drink_button = Button(self.parent, text="Drinken!", command=self.__give_drink_button_on_click, width=10) self.__give_drink_button.place(x=400, y=150) self.__give_drink_button.lower() def __change_button_on_click(self): """button click event of change_button""" color = self.__random_color(self["background"]) self.configure(background=color) self.__text_label.configure(background=color) self.__x_label.configure(background=color) if color == "black": self.__text_label.configure(foreground="white") self.__x_label.configure(foreground="white") else: self.__text_label.configure(foreground="black") self.__x_label.configure(foreground="black") print(color) def __ok_name_button_on_click(self): """button click event of ok_name_button""" s = self.__name_entry.get() if s != "": self.__dog = Dog(s) self.__text_label.configure(text=self.__dog.bark()) # show components: self.__x_label.lift() self.__first_number_entry.lift() self.__second_number_entry.lift() self.__ok_calc_button.lift() self.__go_racing_button.lift() self.__give_food_button.lift() self.__give_drink_button.lift() else: self.__text_label.configure(text="Graag een naam van je hond invoeren.") # hide components: self.__x_label.lower() self.__first_number_entry.lower() self.__second_number_entry.lower() self.__ok_calc_button.lower() self.__go_racing_button.lower() self.__give_food_button.lower() self.__give_drink_button.lower() def __ok_math_button_on_click(self): """button click event of ok_math_button""" n1 = int(self.__first_number_entry.get()) n2 = int(self.__second_number_entry.get()) self.__text_label.configure(text=self.__dog.do_math(n1, n2)) def __go_racing_button_on_click(self): """button click event of go_racing_button""" self.__text_label.configure(text=self.__dog.go_racing()) def __give_food_button_on_click(self): """button click event of give_food_button""" self.__text_label.configure(text=self.__dog.get_food()) def __give_drink_button_on_click(self): """button click event of give_drink_button""" self.__text_label.configure(text=self.__dog.get_drink()) def __random_color(self, old_color): """get different color""" new_color = old_color while new_color == old_color: new_color = choice(self.__colors) return new_color
# A class for instantiating Dog objects. from dog import Dog my_dog = Dog("Tommy", "WatchDog") my_dog.bark() my_other_dog = Dog("Annabelle", "CornDog") print(my_other_dog.name) # Stretch challenge dogs cartoon_dog = Dog("Scooby", "DetectiveDog") bull_dog = Dog("Thrasher", "K9") mythical_dog = Dog("Cerberus", "Harry Potter Dog") cartoon_dog.bark() bull_dog.sit() mythical_dog.roll_over()
from dog import Dog Rex = Dog("Golden", 8) # make a Dog instance, Rex Spot = Dog("Pit Bull", 12) # make another Dog instance, Spot Rex.addTrick("roll over") # add a trick to Rex Spot.addTrick("sit") # add a trick to Spot Spot.addTrick("play dead") # add another trick to Spot print(Rex.kind) # print kind, which is inherited by all Dogs print(Spot.kind) print(Rex.breed) # breed is specified at instantiation print(Rex.age) # age is specified at instantiation print(Spot.breed) print(Spot.age) print(Rex.tricks) # all dogs have a tricks array print(Spot.tricks) Spot.age += 1 # you can modify Dog properties Spot.weight = 70 # i just added a property to Spot from outside the class. this is bad form. print(Spot.age) # see the age has been modified print(Spot.weight) # you're allowed to do this, but ew. print(Spot.bark()) # the bark method returns "arf"
from dog import Dog from cat import Cat dog_karma = Dog("Karma", "Pitbull", "Blue and White") print(dog_karma.name, dog_karma.breed, dog_karma.color) dog_karma.bark() cat_cali = Cat("Cali", "stray", "Black and Orange") print(cat_cali.name, cat_cali.breed, cat_cali.color) cat_cali.meow()
from dog import Dog my_dog = Dog('Rex', 'SuperDog') my_dog.bark() my_other_dog = Dog('Annie', 'SuperDog') print(my_other_dog.name) amigo = Dog('Amigo', 'SuperSuperDog') solo = Dog('Solo', 'SuperDuperDog') mando = Dog('Mando', 'SuperSuperDuperDog') amigo.sit() solo.roll() mando.bark()
from dog import Dog my_dog = Dog("Rex", "Superdog") print(my_dog.name) my_dog.bark() my_other_dog = Dog("Annie", "SuperDog") print(my_other_dog.name) my_other_dog.rollover() my_moms_dog = Dog("Bodie", "Crocker Spaniel") print(my_moms_dog.name) my_moms_dog.sit() my_moms_dog.bark() Dog.greeting = ("woah")