Question

In: Computer Science

Programming language is python 3 For this project, you will import the json module. Write a...

Programming language is python 3

For this project, you will import the json module.

Write a class named NeighborhoodPets that has methods for adding a pet, deleting a pet, searching for the owner of a pet, saving data to a JSON file, loading data from a JSON file, and getting a set of all pet species. It will only be loading JSON files that it has previously created, so the internal organization of the data is up to you.

  • The init method initializes all the data members, which must all be private.
  • The add_pet method takes as parameters the name of the pet, the species of the pet, and the name of the pet's owner. If a pet has the same name as a pet that has already been added, then the function should not add the new pet.
  • The delete_pet method takes as a parameter the name of the pet and deletes that pet.
  • The get_owner method takes as a parameter the name of the pet and returns the name of its owner.
  • The save_as_json method takes as a parameter the name of the file and saves it in JSON format with that name. You can assume the extension (if any) will be part of the provided name. You can organize your JSON file however you want.
  • The read_json method takes as a parameter the name of the file to read and loads that file. This will replace all of the pets currently in memory.
  • The get_all_species method takes no parameters and returns a set of the species of all pets.

For example, your class could be used like this:

np = NeighborhoodPets()
np.add_pet("Fluffy", "gila monster", "Oksana")
np.add_pet("Tiny", "stegasaurus", "Rachel")
np.add_pet("Spot", "zebra", "Farrokh")
np.save_as_json("pets.json")
np.delete_pet("Tiny")
spot_owner = np.get_owner("Spot")
np.read_json("other_pets.json")  # where other_pets.json is a file it saved in some previous session
species_set = np.get_all_species()

If you implement a Pet class (which is a natural option, but not required), then when you save it, you'll want to translate the information into one of the built-in object types the json module recognizes, and translate it back the other way when you read it.

The file must be named: NeighborhoodPets.py

Solutions

Expert Solution

Code:

solution
import json
class NeighborhoodPets:
def __init__(self):
self.petLibrary = {}
  
# check if pet in neghibourhood
# if present do nothing
# else add pet
def add_pet(self, name, species, owner):
if name not in self.petLibrary.keys():
self.petLibrary[name] = {"name": name,
"species": species,
"owner": owner}
#if pet name in pet library then delete it else do nothing
def delete_pet(self, name):
if name in self.petLibrary.keys():
self.petLibrary.pop(name)
#get the name of the owner
def get_owner(self, name):
if name in self.petLibrary.keys():
return self.petLibrary[name]["owner"]
else:
return "pet not present"
#save the pet data into the file
def save_as_json(self, file_name):
data = self.petLibrary
with open(file_name, 'w') as outfile:
json.dump(data, outfile)
# obtain data from json file
def read_json(self, file_name):
with open(file_name) as json_file:
self.petLibrary = json.load(json_file)
# get the species of each pet into a set and return it
def get_all_species(self):
petSpecies = {pet["species"] for pet in self.petLibrary.values()}
return petSpecies
#test lines
np = NeighborhoodPets()
np.add_pet("Fluffy", "gila monster", "Oksana")
np.add_pet("Tiny", "stegasaurus", "Rachel")
np.add_pet("Spot", "zebra", "Farrokh")
np.save_as_json("pets.json")
np.delete_pet("Tiny")
spot_owner = np.get_owner("Spot")
np.read_json("other_pets.json")
print(spot_owner)
species_set = np.get_all_species()
print(species_set)

Snapshots:


Related Solutions

Programming language is in python 3 For this project, you will import the json module. Write...
Programming language is in python 3 For this project, you will import the json module. Write a class named NobelData that reads a JSON file containing data on Nobel Prizes and allows the user to search that data. It just needs to read a local JSON file - it doesn't need to access the internet. Specifically, your class should have an init method that reads the file, and it should have a method named search_nobel that takes as parameters a...
This lab problem demonstrates the use of import module. The Python programming language has many strengths,...
This lab problem demonstrates the use of import module. The Python programming language has many strengths, but one of its best is the availability to use many existing modules for various tasks, and you do not need to be an experienced computer programmer to start using these modules. We have given you some incomplete code; note that the very first line of that code contains an import statement as follows: import math This statement enables your program to use a...
Python Language: Similar to Project 3, write a program that loops a number from 1 to...
Python Language: Similar to Project 3, write a program that loops a number from 1 to 10 thousand and keeps updating a count variable according to these rules: if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if none of the above conditions match for the number, increase count by the number. Before the loop begins,...
Programming language in Python Suppose, for Jane, n1 = 3, n2 = 4, and n3 =...
Programming language in Python Suppose, for Jane, n1 = 3, n2 = 4, and n3 = 5. Also suppose, Jane iterates the number from 1 to 15. At the beginning, Jane sets count to 0, and then proceeds iterating the number from 1 to 15 and for each iteration does the following: for 1, count is increased by 1 because it is not divisible by 3, 4, and 5; count is now: 1 for 2, count is increased by 2...
Python programming problem! Suppose that there is a json file called data from the desktop. I...
Python programming problem! Suppose that there is a json file called data from the desktop. I want to print the value with the key of "year" and "country". Json file below: { "A":{ "year":11, "grade":A, "country":America}, "B":{ "year":18, "grade":C, "country":England}, "C":{ "year":19, "grade":B, "country":China},} I want a code that I can only replace the name of key from year to grade and the code could be work.
Module and Import For these 3 problems, -You are writing a function to calculate a formula...
Module and Import For these 3 problems, -You are writing a function to calculate a formula in the module. -You are going to import the modules to the main. -Write 3 more functions in the main -Don't do any formula calculation in the main just use module functions and return your result. -Call each main function twice to test the provided values. 1. Meter to Feet and Inches One meter equals 39.37 inches and 1 foot equals 12 inches. Write...
The Programming Language is C++ Objective: The purpose of this project is to expose you to:...
The Programming Language is C++ Objective: The purpose of this project is to expose you to: One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions. Problem Specification: Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three quizzes for each student during the term. Each student is identified by a four-digit student...
Write a program in python programming language to implement/simulate a finite automaton that accepts (only): odd...
Write a program in python programming language to implement/simulate a finite automaton that accepts (only): odd Binary numbers // 00000000, 0101, 111111, etc. Show: Finite Automaton Definition, Graph, Table
Write a program in python programming language to implement/simulate a finite automaton that accepts (only): unsigned...
Write a program in python programming language to implement/simulate a finite automaton that accepts (only): unsigned integer numbers // 123, 007, 4567890, etc. Show: Finite Automaton Definition, Graph, Table.
PYTHON Suppose you need to import a single class named GeneTools from a module named bioinformatics,...
PYTHON Suppose you need to import a single class named GeneTools from a module named bioinformatics, in Python. Which of the following represents the correct use of an import statement? import GeneTools from bioinformatics from bioinformatics import GeneTools from bioinformatics import * import GeneTools
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT