Animal nutrition database creator

Language: Python

Task:

The program was designed to create databases of feeds and their nutrient contents, as well as databases of the animals on an agricultural establishment.

Efficiency and improvement

The areas I would look to improve and expand are:

  1. A means for the user to pull up the data, other than in open office as a csv.
  2. A choice for the user to correct errors or delete entries.
  3. A warning that there is already a csv file with the same name.
  4. Calculation options to work out how long food will last, cost per month, how much the animal needs etc
class DatabaseCreator:

"""The class allows the creation of a database by the user, one of the possible choices in the main menu"""

def __init__(self,name):
self.name = name

def create_name(self,name):

"""Method create_name allows the computer to create a variable that gives the file name and type (feed or animal)
to work with"""

my_dict={}
my_dict["File name"]= name+'.csv'
return
my_dict

def choose_type(self, my_dict):

"""Method choose type allows the user to decide whether they are creating a feed database or an animal
database, this allows the computer to work out which field to create in the csv"""

type = input ("Do you want (1) an feed database or (2) and animal database?")
if type == "1":
my_dict["Type"]="feed"
if
type == "2":
my_dict["Type"]="animal"
return
my_dict

def create_database(self,my_dict, fieldnames):

"""create_database method sets up the csv with the correct headers. It uses the field_name_creator method to
work out which fieldnames to use, depending on the database type (stored in my_dict)"""

name = my_dict["File name"]
import csv
with open(name, 'w', newline='') as database:
writer = csv.DictWriter(database, fieldnames=fieldnames)
writer.writeheader()

def field_name_creator (self, my_dict):

"""field_name_creator uses my_dict Type to decide on the fieldnames to be used. These can then be given to the
relevant methods"""

if my_dict["Type"] == "feed":
fieldnames = ["Name","Protein","Fat","Fibre","Starch","Energy","Calcium","Potassium","Magnesium","Phosphorus","Sulphur","Boron","Copper","Iron","Manganese","Zinc", "Weight", "Cost"]
if my_dict["Type"] == "animal":
fieldnames = ["Type","Name","Weight","Protein","Fat","Fibre","Starch","Energy","Calcium","Potassium","Magnesium","Phosphorus","Sulphur","Boron","Copper","Iron","Manganese","Zinc"]
return fieldnames

def create_inputs (self,my_dict, fieldnames):

"""Create feed allows the input of name feeds into the csv file and their nutrient content, name, weight etc"""
name = my_dict["File name"]
import csv
#Loop to add new feed if more than one wants to be added
while True:
answer = input ("Would you like to add another item to the database? y/n")
if answer == "n":
return
if
answer == "y":
#This method creates a list of the nutrients and a dictionary that shows the corresponding values.
#The list is to loop through, asking the user for each value, thus saving multiple statements. The list
#is created from the fieldnames. The dictionary is created from the list and a value is added to each
#key from the user's inputs.
list_of_nutrients_dictionary = {i : "empty" for i in fieldnames}
print ("Please give the:")
for i in list(fieldnames):
answer = input (i+":")
list_of_nutrients_dictionary[i]=answer
#The csv file line is written, using the dictionary created above
with open (name, "a",newline='') as database:
writer = csv.DictWriter(database,fieldnames=fieldnames)
writer.writerow(list_of_nutrients_dictionary)

def main_database_creator():

name = input("Please give a name for your database.")
database = DatabaseCreator (name)
my_dict = database.create_name(name)
my_dict = database.choose_type(my_dict)
fieldnames = database.field_name_creator(my_dict)
database.create_database(my_dict, fieldnames)
database.create_inputs(my_dict, fieldnames)

main_database_creator()