lab01 : Python Classes

num ready? description assigned due
lab01 true Python Classes Sun 10/11 11:59PM Sun 10/18 11:59PM

In this lab, you’ll have the opportunity to practice:

It may be a good idea to read up on some tools we’ll use in this lab before you get started, specifically Chapter 1.4.2.1 (String Formatting) and 1.4.6 - 1.4.6.1 (Object Oriented Programming).

The main idea for this lab is to write a program that will organize Animals into an Animal shelter. The program should have the ability to add / remove / search for animals.

In addition to defining classes for Animals and an AnimalShelter, you should test your code correctness by unit testing various scenarios using pytest for these classes.

Instructions

You will need to create three files:

There will be no starter code for this assignment, but rather the class descriptions and required methods are defined in the specification below.

It’s recommended that you organize your lab work in its own directory. This way all files for a lab are located in a single folder. Also, this will be easy to import various files into your code using the import / from technique shown in lecture.

Animal.py class

The Animal.py file will contain the definition of what an Animal is.

We will define the Animal attributes as follows:

You should write a constructor that allows the user to construct an animal object by passing in values for all of the fields. Your constructor should set these attributes with the value None by default.

In addition to your constructor, your class definition should also support “setter” methods that can update the state of the Animal objects:

Each Animal object should be able to call a method toString that you will implement, which returns a str with all the animal attributes. The string should contain all attributes in the following EXACT format:

a = Animal("dog", 12.2, 2, "Ruffles")
print(a.toString())

Output:

Species: DOG, Name: RUFFLES, Age: 2, Weight: 12.2

Note: The a.toString() return value in the example above does not contain a newline character (\n) at the end.

AnimalShelter.py

The AnimalShelter.py file will contain the definition of what a single AnimalShelter object is.

An AnimalShelter object will contain a dictionary structure where the keys of the dictionary will be a str type representing an Animal’s species (all upper-case characters).

The dictionary value will be a list of Animal objects that the AnimalShelter contains. Note that the order of the Animals in the list is based on when the Animal object was inserted into the dictionary structure (most recent Animal inserted will be at the end of the list).

Your code should support the following constructor / methods:

testFile.py pytest

This file should import your Animal and AnimalShelter classes so you can write unit tests using pytest to test if your functionality is correct. Think of various scenarios and edge cases when testing your code (provide at least three cases (assert statements) per method for Animal and AnimalShelter). Even though Gradescope will not use this file when running the automated tests, it is important to provide this file with various test cases (testing is important!!).

Submission

Once you’re done with writing your class definition and tests, Submit your Animal.py, AnimalShelter.py, and testFile.py to the Lab01 assignment on Gradescope. There will be various unit tests Gradescope will run to ensure your code is working correctly based on the specifications given in this lab.

If the tests don’t pass, you may get some error message that may or may not be obvious at this point. Don’t worry - if the tests didn’t pass, take a minute to think about what may have caused the error. If your tests didn’t pass and you’re still not sure why you’re getting the error, feel free to ask your TAs or Learning Assistants.