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...
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.
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...
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
The programming language is Python Instructions: Create a function that will delete a node in a...
The programming language is Python Instructions: Create a function that will delete a node in a Linked List based on position number. On below example, if you want to delete position #2, it will remove the Banana (arrangement of nodes below is Apple, Banana, Cherry, Grapes, Orange). myLinkedList = LinkedList() myLinkedList.append("Banana") myLinkedList.append("Cherry") myLinkedList.append("Grapes") myLinkedList.append("Orange") myLinkedList.prepend("Apple") myLinkedList.deleteByPositionNum(2) node = myLinkedList.head while node: print(node.value, " ") node = node.next_node You may start with the function head: def deleteByPositionNum(self, positionNum):
in python programming language, please include the proper identation This homework will allow you to demonstrate...
in python programming language, please include the proper identation This homework will allow you to demonstrate understanding and engagement with the following topics: graph representations object oriented programming graph processing,such as finding shortest paths and finding tree traversals Your task is to implement a Graph class. The edges in the graph are undirected, but you must implement an Edge class. In addition, you will have a Graph class, and a Node class. You can choose to implement graphs with any...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that sorts the array below in ascending order.  LISP is a recursive language so the program will use recursion to sort. Since there will be no loops, you will not need the variables i, j, and temp, but still use the variable name array for the array to be sorted.             Array to be sorted is 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 The...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
Python Assignment You will be writing an inventory system that is backed by JSON data and...
Python Assignment You will be writing an inventory system that is backed by JSON data and will be working with a starter file that contains a JSON string. The code you write will need to follow the following guidelines. The what You’re at work one day and your boss asks you about that fancy programming language you’ve been learning, Python. She asks you if you can use it to read JSON data from a supplier and build an inventory. “Of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT