Why is this code not able to find the database in the directory above the current one?…
- User
Why is this code not able to find the database in the directory above the current one? from plot_operations import PlotOperations import sys import logging from datetime import datetime from scrape_weather import WeatherScraper from db_operations import DBOperations from plot_operations import PlotOperations
class WeatherProcessor: def init(self): self.db_operations = DBOperations("mydatabase.db") self.plot_operations = PlotOperations() self.logger = logging.getLogger(name)
def main_menu(self): while True: print("\nWeather Data Processor") print("1. Download a full set of weather data") print("2. Update weather data") print("3. Generate box plot based on year range") print("4. Generate line plot based on a month and year") print("5. Exit") selection = input("Make a selection: ") if selection == "1": self.download_full_set() input("Press Enter to continue...") elif selection == "2": self.update_weather_data() input("Press Enter to continue...") elif selection == "3": self.generate_box_plot() input("Press Enter to continue...") elif selection == "4": self.generate_line_plot() input("Press Enter to continue...") elif selection == "5": sys.exit(0) else: print("Invalid choice. Select another option.") #TODO def download_full_set(self): return None #TODO def update_weather_data(self): return None #TODO def generate_box_plot(self): start_year = input("Enter the year to start gathering data from...") end_year = input("Enter the year to end the search for data at...") new_db = DBOperations("mydatabase.db") data = new_db.fetch_data_year_to_year(start_year, end_year) PlotOperations().create_box_plot(start_year, end_year, data) #TODO def generate_line_plot(self): month = input("Enter a month to search for weather data... (January = 1, February = 2)") year = input("Enter the year of the month you wish to view data from...") new_db = DBOperations("mydatabase.db") data = new_db.fetch_data_single_month(month, year) PlotOperations().create_line_plot(data)if name == 'main': processor = WeatherProcessor() processor.main_menu()
While this code is able to
''' A module that creates graphs from data pulled from a database. ''' import logging import matplotlib.pyplot as plt from db_operations import DBOperations
class PlotOperations(): '''A class that plots data based on user input.''' logger = logging.getLogger("main." + name)
def create_box_plot(self, start_year, end_year, data): ''' A function that creates a box plot of data pulled from the database based off of user input. Parameters: - start_year: int - The year in which the user wants the range to begin. - end_year: int - The year in which the user wants the range to end.''' try: data_to_plot = list(data.values()) plt.boxplot(data_to_plot) #Feed the data plt.title(f'Monthly temperature distribution for: {start_year} to {end_year}') plt.xlabel('Month') # Label the x-axis plt.ylabel('Temperature (Celcius)') # Label the y-axis plt.show() # Show the graph except Exception as error: self.logger.error("PlotOps:boxplot:%s", error) def create_line_plot(self, data): """ Creates a line plot based on the data provided by the user. Parameters: - data: dict - A collection of data stored in a dictionary.""" try: dates = list(data.keys()) # Dates are the keys in the dictionary temps = list(data.values()) # Temperatures are the values in the dictionary plt.plot(dates, temps) # Feed the data plt.title('Daily Avg Temperatures') # Create the title plt.xlabel('Days of Month') # Label the x axis plt.ylabel('Avg Daily Temp') # Label the y axis # Create text rotation on the x axis so they all fit properly plt.xticks(rotation = 50, horizontalalignment = 'right') plt.show() # Show the graph except Exception as error: self.logger.error("PlotOps:lineplot:%s", error) - Assistant
The issue may be related to how the database file path is specified. Your code initializes the
DBOperationswith the database file name"mydatabase.db". If the database is located one directory level above the current directory, you should specify the path as"../mydatabase.db".So, update the
initmethod and other usages ofDBOperationsin yourWeatherProcessorclass like this:def init(self): self.db_operations = DBOperations("../mydatabase.db") ...And similarly:
# In generate_box_plot method new_db = DBOperations("../mydatabase.db") # In generate_line_plot method new_db = DBOperations("../mydatabase.db")This should ensure that your code looks for the database file in the directory above the current one.