One-to-many Object Relationships Lab (CodeGrade)
- Due No Due Date
- Points 1
- Submitting an external tool
Learning Goals
- Create a one-to-many relationship.
Key Vocab
- Class: a bundle of data and functionality. Can be copied and modified to accomplish a wide variety of programming tasks.
- Object: the more common name for an instance. The two can usually be used interchangeably.
- Object-Oriented Programming: programming that is oriented around data (made mobile and changeable in objects) rather than functionality. Python is an object-oriented programming language.
- Function: a series of steps that create, transform, and move data.
- Method: a function that is defined inside of a class.
Introduction
In this lab we will implement a one-to-many relationship between a Owner and Pet.
Instructions
This is a test-driven lab. Run pipenv install to create your virtual environment and pipenv shell to enter the virtual environment. Then run pytest -x to run your tests. Use these instructions and pytest 's error messages to complete your work in the lib/ folder.
Instructions:
- Define an
Ownerclass and pass into the constructor anameargument. - Define a
Petand pass into the constructor itsname,pet_typeand an optionalowner. - Define a class variable
PET_TYPES = ["dog", "cat", "rodent", "bird", "reptile", "exotic"]and validate that thepet_typeis one of those types in the__init__method.raise Exceptionif this check fails.
- Define a class variable
allthat stores all instances of thePetclass. - In the
Ownerclass write a method calledpets(self)that returns a full list of the owner's pets. - In the
Ownerclass write a method calledadd_pet(self, pet)that checks that the the pet is of typePetand adds the owner to the pet. - In the
Ownerclass write a method calledget_sorted_pets(self)returns a sorted list of pets by their names. OwnerandPetshould useisinstanceto check types whenever instances are passed into methods.raise Exceptionif these checks fail.