-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
163 lines (134 loc) · 6.61 KB
/
main.py
1
#Python Financial Analysis#Python script to create a simple Profit/Loss financial analysis of records for a company#Financial data from budget_data.csv #Reads in csv file and writes the analysis data results to a text file 'analysis.txt' in the 'analysis' folder#ASSUMPTIONS: file is clean and sorted by date, there are no months with zero (0) for profit/loss, # folder structure outlined in Readme.md, code finds the relative paths# Pyhton 3.8.8 #Dependenciesimport osimport csvfinance_data = []#path for financial file readingcsvpath_finances = os.path.join('Resources','budget_data.csv')txtpath = os.path.join('analysis','analysis.txt')def csvFinancialData(csvpath_finances): #function to get the financial data from the csv file #calculating: Number of months, total $, average $ amt., the month that had the greatest increase/decrease and that amount. #inputs the csvpath_finances as the csv file path #returns the data in a list num_months = 0 net_total = 0 previous_net = 0 change = [] max_change = 0 min_change = 0 index_max = 0 index_min = 0 previous_date ='' average = 0 data =[] if os.path.exists(csvpath_finances): with open(csvpath_finances,'r', newline='', encoding='utf-8') as csvfilef: #create a reader object csv_readerf= csv.reader(csvfilef, delimiter=',') #read in header headerf = next(csv_readerf) #check if file is not empty if headerf != None: #iterate over each row after header for line in csv_readerf: #count months, ensuring not duplicated if line[0] != previous_date: num_months += 1 previous_date = line[0] #calculate total net_total = net_total + float(line[1]) #get the change of profit/loss and add date change occurred if previous_net != 0: change.append(line[0]) change.append(float(line[1]) - previous_net) #set current value of profit/loss to be the previous when loop again previous_net = float(line[1]) #get/set values need to return average = round(sum(change[1::2]) / (len(change)/2),2) max_change = max([i for i in change[1::2]]) min_change =min([i for i in change[1::2]]) #get index of max and min index_max = change.index(max_change) index_min = change.index(min_change) #get the corresponding date before this index max_date = change[index_max -1] min_date = change[index_min -1] #put data values in a list to return data.append(num_months) data.append(net_total) data.append(average) data.append(max_date) data.append(max_change) data.append(min_date) data.append(min_change) #clear the change list change.clear() #no header, no information in fie, should have one else: print("No header found, this may not be the correct file. It may be empty") #no file to read else: print("Either the file is missing or the file is not readable.") #return the data return data def writeData(finance_data,txtpath, **kwargs): #function creates a file if it is not there, then append data (allows for several runs with a file of the same name) #Appends to a text file: the financial analysis data in the list, total months, Total $, average $, greatest increase/decrease #inputs: the finance_data as a list, textpath as the text file path for the results #Out: Writes results to a text file : in analysis folder, called analysis.txt #set default kwargs option to print both to a file(f) and to the screen(s) #when no kwarg option is passed #TODO:: have an acceptable list of kwarg keys if not kwargs: defaultKwargs = { 'prnt_option1': 'f', 'prnt_option2': 's'} kwargs = { **defaultKwargs, **kwargs } for key, value in kwargs.items(): if value == 'f': if not os.path.exists(txtpath): with open(txtpath, "w", newline="") as f: pass if(os.path.exists(txtpath)): with open(txtpath, "a", newline="", encoding='utf-8') as f: #appends to end of file, add an extra line f.write("\n") # Write the title f.write("Financial Analysis\n") f.write("-" * 30 + "\n") #Print data f.write(f"Total Months: {finance_data[0]}\n") f.write("Total: ${:,.2f}".format(finance_data[1])) f.write("\n") f.write("Average Change: ${:,.2f}".format(finance_data[2]) + "\n") f.write(f"Greatest Increase in Profits: {finance_data[3]} " + "(${:,.2f}".format(finance_data[4]) + ")\n") f.write(f"Greatest Decrease in Profits: {finance_data[5]} " + "(${:,.2f}".format(finance_data[6]) + ")\n") f.write("-" * 30 + "\n\n") elif value == 's': #appends to end of file, add an extra line # Write the title print("Financial Analysis\n") print("-" * 10) #Print data print(f"Total Months: {finance_data[0]}") print("Total: ${:,.2f}".format(finance_data[1])) print("Average Change: ${:,.2f}".format(finance_data[2])) print(f"Greatest Increase in Profits: {finance_data[3]} " + "(${:,.2f}".format(finance_data[4]) + ")") print(f"Greatest Decrease in Profits: {finance_data[5]} " + "(${:,.2f}".format(finance_data[6]) + ")") print("-" * 10) #get the finance data: finance_data = csvFinancialData(csvpath_finances)#check for dataif len(finance_data): #write the finance data to a text file val1 = 'f', val2 ='s' #choose one print option or no print option will default to printing to file and screen writeData(finance_data,txtpath) finance_data.clear() print("Your financial analysis is complete.") else: print("Your financial data is not there.")