Reputation: 5
# import json # print('Loading Clinsj functions') # def lambda_handler(event, context): # print("Received event: " + json.dumps(event,indent=2)) # return {"message":"Hello from Clinsj!"} import json # import boto3 # from flask_lambda import FlaskLambda # from flask import request # app = FlaskLambda(__name__) # ddb=boto3.resource('dynamodb') # table=dob.Table('clinsj-api2') # @app.route("/", methods=['POST', 'GET']) # def calculate_total_income(): def lambda_handler(event,context): total = '' T_T_I = '' W_C_I = '' T_B = '' T_F_I = '' C_T = '' S_A_T = '' H_C = '' S_A_C = '' W_C_I_C = '' T_D_C = '' T_E_L = '' Score = '' Score_2 = '' Score_3 = '' Knockout_11 = '' Knockout_22 = '' Knockout_33 = '' Knockout_44 = '' Knockout_55 = '' Knockout_66 = '' Factor = '' WT_score11 = '' ET_score11 = '' TI_score11 = '' WT_score22 = '' ET_score22 = '' TI_score22 = '' Score_of_PB1 = '' Score_of_SB1 = '' TOS = '' Anyknock_outs1 = '' Set_score_to_zero1 = '' Speedometer1 = '' Age_Score1 = '' Age_Score2 = '' # if request.method == 'POST' and 'salary/wages' in request.form and 'other salary/wages' in request.form and 'tax_free_rental_income' in request.form and 'other_tax_free_income' in request.form: # Salary_or_wages = int(request.form.get('salary/wages')) # Other_salary_wages = int(request.form.get('other salary/wages')) # Tax_free_rental_income = int( # request.form.get('tax_free_rental_income')) # Other_tax_free_income = int(request.form.get('other_tax_free_income')) # total = Salary_or_wages + Other_salary_wages + \ # Tax_free_rental_income + Other_tax_free_income Salary_or_wages = event["queryStringParameters"]['salary/wages'] Other_salary_wages = event["queryStringParameters"]['other salary/wages'] Tax_free_rental_income = event["queryStringParameters"]['tax_free_rental_income'] Other_tax_free_income = event["queryStringParameters"]['other_tax_free_income'] total = int(Salary_or_wages) + int(Other_salary_wages) + int(Tax_free_rental_income) + int(Other_tax_free_income) # calculate_total_debts(): Total_Debt = '' # if request.method == 'POST' and 'car_loans' in request.form and 'credit_card' in request.form and 'student_loans' in request.form and 'mortgage_loans' in request.form: # Car_Loans = int(request.form.get('car_loans')) # Credit_Cards = int(request.form.get('credit_card')) # Student_Loans = int( # request.form.get('student_loans')) # Mortgage_Loans = int(request.form.get('mortgage_loans')) # Total_Debt = Car_Loans + Credit_Cards + \ # Student_Loans + Mortgage_Loans Car_Loans = event["queryStringParameters"]['car_loans'] Credit_Cards = event["queryStringParameters"]['credit_card'] Student_Loans = event["queryStringParameters"]['student_loans'] Mortgage_Loans = event["queryStringParameters"]['mortgage_loans'] Total_Debt = int(Car_Loans) + int(Credit_Cards) + int(Student_Loans) + int(Mortgage_Loans) # Existing rate and Worst case rate(): E_Rate = '' WC_Rate = '' # if request.method == 'POST' or 'existing_rate' in request.form and 'worst_case_rate' in request.form: # Existing_Rate = int(request.form.get('existing_rate')) # Wrost_Case_Rate = 5 # E_Rate = Existing_Rate # WC_Rate = Existing_Rate + Wrost_Case_Rate Existing_Rate = event["queryStringParameters"]['existing_rate'] Wrost_Case_Rate = 5 E_Rate = Existing_Rate WC_Rate = int(Existing_Rate) + int(Wrost_Case_Rate) # calculate_total_security(): total2 = '' # if request.method == 'POST' or 'secured_in_property' in request.form and 'other_security' in request.form: # Secured_In_Property = int(request.form.get('secured_in_property')) # Other_Security = int(request.form.get('other_security')) # total2 = Secured_In_Property + Other_Security Secured_In_Property = event["queryStringParameters"]['secured_in_property'] Other_Security = event["queryStringParameters"]['other_security'] total2 = int(Secured_In_Property) + int(Other_Security) # house_located_in_oslo and primary_residence(): HLIO = '' PR = '' # if request.method == 'POST' or 'house_located_in_oslo' in request.form or 'primary_residence' in request.form: # House_Located_In_Oslo = str(request.form.get('house_located_in_oslo')) # Primary_Residence = str(request.form.get('primary_residence')) # HLIO = House_Located_In_Oslo # PR = Primary_Residence House_Located_In_Oslo = event["queryStringParameters"]['house_located_in_oslo'] Primary_Residence = event["queryStringParameters"]['primary_residence'] HLIO = House_Located_In_Oslo PR = Primary_Residence # calculate_annual_household_cost(): total3 = '' # if request.method == 'POST' or 'number_of_adults' in request.form or 'number_of_childerns' in request.form and 'number_of_cars' in request.form: # Number_Of_Adults = int(request.form.get('number_of_adults')) # Number_Of_Childerns = int(request.form.get('number_of_childerns')) # Number_Of_Cars = int(request.form.get('number_of_cars')) # Other_Household_spend = ( # Number_Of_Adults + Number_Of_Childerns)*393 + int(2590) + Number_Of_Cars * 2608 # total3 = (Number_Of_Adults * 7500 + Number_Of_Childerns * # 5500 + Other_Household_spend) * 12 Number_Of_Adults = event["queryStringParameters"]['number_of_adults'] Number_Of_Childerns = event["queryStringParameters"]['number_of_childerns'] Number_Of_Cars = event["queryStringParameters"]['number_of_cars'] Other_Household_spend = (int(Number_Of_Adults) + int(Number_Of_Childerns))*393 + int(2590) + int(Number_Of_Cars) * 2608 total3 = (int(Number_Of_Adults) * 7500 + int(Number_Of_Childerns) *5500 + int(Other_Household_spend)) * 12 # d = date.today().day # m = date.today().month # y = date.today().year # # Primary_Borrower(): # DOB1 = '' # WT1 = '' # ET1 = '' # TI1 = '' # age1 = '' # Day11 = '' # Month11 = '' # Year11 = '' # # if request.method == 'POST' or 'date_of_birth1' in request.form or 'worktype1' in request.form and 'employer_type1' in request.form and 'total_income1' in request.form: # # if request.method == 'POST' or 'day1' in request.form or 'month1' in request.form or 'year1' in request.form or 'worktype1' in request.form and 'employer_type1' in request.form and 'total_income1' in request.form: # # Day1 = int(request.form.get('day1')) # # Month1 = int(request.form.get('month1')) # # Year1 = int(request.form.get('year1')) # # Day11 = Day1 # # Month11 = Month1 # # Year11 = Year1 # Day1 = event["queryStringParameters"]['day1'] # Month1 = event["queryStringParameters"]['month1'] # Year1 = event["queryStringParameters"]['year1'] # Day11 = int(Day1) # Month11 = int(Month1) # Year11 = int(Year1) # if Day1 > d and Month1 >= m: # # dd = (d+30)- Day1 # # mm = ((m - 1) + 12) - Month1 # yy = (y-1) - Year1 # age1 = int(yy) # elif Day1 > d and Month1 < m: # # dd = (d+30)- Day1 # # mm = ((m - 1) + 12) - Month1 # yy = y - Year1 # age1 = int(yy) # elif Day1 < d and Month1 > m: # # dd = (d+30)- Day1 # # mm = ((m - 1) + 12) - Month1 # yy = (y-1) - Year1 # age1 = int(yy) # else: # yy = y - Year1 # age1 = int(yy) # # Age score calculation for PB # if age1 < 30: # Age_Score = 0 # Age_Score1 = Age_Score # elif age1 < 50: # Age_Score = 20 # Age_Score1 = Age_Score # elif age1 < 60: # Age_Score = -10 # Age_Score1 = Age_Score # elif age1 < 110: # Age_Score = 0 # Age_Score1 = Age_Score # # Wt score calculation: # Worktype = str(request.form.get('worktype1')) # if Worktype == "Fixed": # WT_score1 = 20 # WT_score11 = WT_score1 # else: # WT_score1 = 0 # WT_score11 = WT_score1 # Employer_Type = str(request.form.get('employer_type1')) # if Employer_Type == "Public Sector": # ET_score1 = 20 # ET_score11 = ET_score1 # else: # ET_score1 = 0 # ET_score11 = ET_score1 # # TI score calculation: # Total_Income = int(request.form.get('total_income1')) # if Total_Income < 500000: # TI_score1 = 0 # TI_score11 = TI_score1 # elif Total_Income < 700000: # TI_score1 = 20 # TI_score11 = TI_score1 # elif Total_Income < 900000: # TI_score1 = 50 # TI_score11 = TI_score1 # elif Total_Income < 1100000: # TI_score1 = 100 # TI_score11 = TI_score1 # # DOB1 = Date_Of_Birth # WT1 = Worktype # ET1 = Employer_Type # TI1 = Total_Income # # Other Elements - attractiveness of customer # # Secondary_Borrower(): # DOB2 = '' # WT2 = '' # ET2 = '' # TI2 = '' # age2 = '' # Day22 = '' # Month22 = '' # Year22 = '' # if request.method == 'POST' or 'date_of_birth2' in request.form or 'worktype2' in request.form and 'employer_type2' in request.form and 'total_income2' in request.form: # # Date_Of_Birth2 = date(request.form.get(int('date_of_birth2'))) # Day2 = event["queryStringParameters"]['day12'] # Month2 = event["queryStringParameters"]['month2'] # Year2 = event["queryStringParameters"]['year2'] # Day22 = Day2 # Month22 = Month2 # Year22 = Year2 # if Day2 > d and Month2 >= m: # # dd = (d+30)- Day1 # # mm = ((m - 1) + 12) - Month1 # yy2 = (y-1) - Year2 # age2 = int(yy2) # elif Day2 > d and Month2 < m: # # dd = (d+30)- Day1 # # mm = ((m - 1) + 12) - Month1 # yy2 = y - Year2 # age2 = int(yy2) # elif Day2 < d and Month2 > m: # # dd = (d+30)- Day1 # # mm = ((m - 1) + 12) - Month1 # yy2 = (y-1) - Year2 # age2 = int(yy2) # else: # yy2 = y - Year2 # age2 = int(yy2) # # Age score calculation for SB # if age2 < 30: # Age_Score_2 = 0 # Age_Score2 = Age_Score_2 # elif age2 < 50: # Age_Score_2 = 20 # Age_Score2 = Age_Score_2 # elif age2 < 60: # Age_Score_2 = -10 # Age_Score2 = Age_Score_2 # elif age2 < 110: # Age_Score_2 = 0 # Age_Score2 = Age_Score_2 # # age_2 = d - Year2 - ((m, d) < (Month2, Day2)) # # age2 = age_2 # # today = date.today() # # age_2 = today.year - Date_Of_Birth2.year - \ # # ((today.month, today.day) < (Date_Of_Birth2.month, Date_Of_Birth2.day)) # # age2 = age_2 # # Date_Of_Birth2.year # # particular_date = datetime(request.form.get('date_of_birth2')) # # new_date = datetime.today() - particular_date # # age=new_date.days # Worktype2 = str(request.form.get('worktype2')) # if Worktype2 == "Fixed": # WT_score2 = 20 # WT_score22 = WT_score2 # else: # WT_score2 = 0 # WT_score22 = WT_score2 # Employer_Type2 = str(request.form.get('employer_type2')) # if Employer_Type2 == "Public Sector": # ET_score2 = 20 # ET_score22 = ET_score2 # else: # ET_score2 = 0 # ET_score22 = ET_score2 # Total_Income2 = int(request.form.get('total_income2')) # if Total_Income2 < 500000: # TI_score2 = 0 # TI_score22 = TI_score2 # elif Total_Income2 < 700000: # TI_score2 = 20 # TI_score22 = TI_score2 # elif Total_Income2 < 900000: # TI_score2 = 50 # TI_score22 = TI_score2 # elif Total_Income2 < 1100000: # TI_score2 = 100 # TI_score22 = TI_score2 # # DOB2 = Date_Of_Birth2 # WT2 = Worktype2 # ET2 = Employer_Type2 # TI2 = Total_Income2 # Total Score of Other Elements - attractiveness of customer # Score of primary borrower # Score_of_PB = WT_score1+ET_score1+TI_score1+Age_Score1 # Score_of_PB1 = Score_of_PB # Score of Secondary borrower # Score_of_SB = WT_score2+ET_score2+TI_score22+Age_Score2 # Score_of_SB1 = Score_of_SB # Qualifying - ability to pay worst case rates # Total taxable income Total_Taxable_Income = Salary_or_wages + Other_salary_wages T_T_I = Total_Taxable_Income # Worst case interest: Wrost_Case_Interest = (Total_Debt * (WC_Rate/100)) W_C_I = Wrost_Case_Interest if(W_C_I < 25): Score = 0 Knockout_1 = 'True' Knockout_11 = Knockout_1 Knockscore = 0 elif(W_C_I >= 25 and W_C_I < 50): Score = 250 Knockout_2 = 'False' Knockout_22 = Knockout_2 Knockscore = 0 elif(W_C_I >= 50 and W_C_I < 75): Score = 260 Knockout_2 = 'False' Knockout_22 = Knockout_2 Knockscore = 0 elif(W_C_I >= 75 and W_C_I < 100): Score = 270 Knockout_2 = 'False' Knockout_22 = Knockout_2 Knockscore = 0 elif (W_C_I >= 100): Score = 300 Knockout_2 = 'False' Knockout_22 = Knockout_2 Knockscore = 0 # Taxable_Base Taxable_Base = Total_Taxable_Income - Wrost_Case_Interest T_B = Taxable_Base # Calculated_Tax Calculated_Tax = round((T_B*0.4054)-62000) C_T = Calculated_Tax # Tax_free_income Tax_Free_Income = Tax_free_rental_income + Other_tax_free_income T_F_I = Tax_Free_Income # Surplus After Tax Surplus_After_Tax = Total_Taxable_Income - \ Wrost_Case_Interest - Calculated_Tax + Tax_Free_Income S_A_T = Surplus_After_Tax # Household Cost Household_Cost = total3 H_C = Household_Cost # Surplus After Costs Surplus_After_Costs = S_A_T - H_C S_A_C = Surplus_After_Costs # Worst case interest cover: Worst_Case_Interest_Cover = round(-(S_A_C / -W_C_I)*100) W_C_I_C = Worst_Case_Interest_Cover # Qualifying - total debt cover Total_Debt_Cover = round((Total_Debt / total), 2) T_D_C = (Total_Debt_Cover) if(T_D_C >= 4.00): Score_2 = 0 Knockout_3 = 'True' Knockout_33 = Knockout_3 Knockscore2 = 1 elif(T_D_C > 3.50 and T_D_C <= 4.00): Score_2 = 250 Knockout_4 = 'False' Knockout_44 = Knockout_4 Knockscore2 = 0 elif(T_D_C > 2.50 and T_D_C <= 3.50): Score_2 = 260 Knockout_4 = 'False' Knockout_44 = Knockout_4 Knockscore2 = 0 elif(T_D_C > 2.00 and T_D_C <= 2.50): Score_2 = 270 Knockout_4 = 'False' Knockout_44 = Knockout_4 Knockscore2 = 0 elif (T_D_C <= 2.00): Score_2 = 300 Knockout_4 = 'False' Knockout_44 = Knockout_4 Knockscore2 = 0 # Qualifying - Equity levels Total_Equity_Level = round((1-(Total_Debt/total2))*100) T_E_L = Total_Equity_Level if(T_E_L < 15): Score_3 = 0 Knockout_5 = 'True' Knockout_55 = Knockout_5 Knockscore3 = 1 elif(T_E_L >= 15 and T_E_L < 30): Score_3 = 100 Knockout_6 = 'False' Knockout_66 = Knockout_6 Knockscore3 = 0 elif(T_E_L >= 30 and T_E_L < 50): Score_3 = 150 Knockout_6 = 'False' Knockout_66 = Knockout_6 Knockscore3 = 0 elif(T_E_L >= 50 and T_E_L < 100): Score_3 = 230 Knockout_6 = 'False' Knockout_66 = Knockout_6 Knockscore3 = 0 elif (T_E_L >= 100): Score_3 = 300 Knockout_6 = 'False' Knockout_66 = Knockout_6 Knockscore3 = 0 # Total_Overall_Score Total_Overall_Score = Score + Score_2 + Score_3 + Score_of_PB1 + Score_of_SB1 TOS = Total_Overall_Score # Any knock outs - e.g. where they are below minimum regulatory: if(Knockscore and Knockscore2 and Knockscore3 == 0): Anyknock_outs = 0 Anyknock_outs1 = Anyknock_outs else: Anyknock_outs = Knockscore + Knockscore2 + Knockscore3 # If there was a knock out - set score to zero: if(Anyknock_outs > 0): Set_score_to_zero = 0 Set_score_to_zero1 = Set_score_to_zero else: Set_score_to_zero = Total_Overall_Score Set_score_to_zero1 = Set_score_to_zero # Current rate assesment(Factor): if(E_Rate < 1.50): Score_4 = 10 Factor = Score_4 elif(E_Rate >= 1.5 and E_Rate < 1.75): Score_4 = 25 Factor = Score_4 elif(E_Rate >= 1.75 and E_Rate < 2.0): Score_4 = 50 Factor = Score_4 elif(E_Rate >= 2.0 and E_Rate < 3.0): Score_4 = 75 Factor = Score_4 elif (E_Rate >= 3.0): Score_4 = 100 Factor = Score_4 # Speedometer: Speedometer = (Score_4/100) * Total_Overall_Score Speedometer1 = Speedometer # return render_template("index.html", Age_Score11=Age_Score1, Age_Score22=Age_Score2, Day_1=Day11, Month_1=Month11, Year_1=Year11, Day_2=Day22, Month_2=Month22, Year_2=Year22, age11=age1, age22=age2, Speedometer11=Speedometer1, Set_score_to_zero11=Set_score_to_zero1, Anyknock_outs11=Anyknock_outs1, TOS1=TOS, Score_of_PB11=Score_of_PB1, Score_of_SB11=Score_of_SB1, WT_score_1=WT_score11, ET_score_1=ET_score11, TI_score_1=TI_score11, WT_score_2=WT_score22, ET_score_2=ET_score22, TI_score_2=TI_score22, Score1=Score, Knockout_1=Knockout_11, Knockout_2=Knockout_22, Score2=Score_2, Knockout_3=Knockout_33, Knockout_4=Knockout_44, Score3=Score_3, Knockout_5=Knockout_55, Knockout_6=Knockout_66, Factor_1=Factor, total_income=total, total_debts=Total_Debt, total_security=total2, annual_household_cost=total3, output_wt1=WT1, output_et1=ET1, output_ti1=TI1, output_wt2=WT2, output_et2=ET2, output_ti2=TI2, hlio=HLIO, pr=PR, ER=E_Rate, WCR=WC_Rate, TTI=T_T_I, WCI=W_C_I, TB=T_B, TFI=T_F_I, CT=C_T, SAT=S_A_T, HC=H_C, SAC=S_A_C, WCIC=W_C_I_C, TDC=T_D_C, TEL=T_E_L) # output_dob1=DOB1, output_dob2=DOB2, # if __name__ == '__main__': # app.run(host="127.0.0.1", port=80, debug=True,) response["headers"] = event["headers"] return ( json.dumps(Age_Score11=Age_Score1, Age_Score22=Age_Score2, Day_1=Day11, Month_1=Month11, Year_1=Year11, Day_2=Day22, Month_2=Month22, Year_2=Year22, age11=age1, age22=age2, Speedometer11=Speedometer1, Set_score_to_zero11=Set_score_to_zero1, Anyknock_outs11=Anyknock_outs1, TOS1=TOS, Score_of_PB11=Score_of_PB1, Score_of_SB11=Score_of_SB1, WT_score_1=WT_score11, ET_score_1=ET_score11, TI_score_1=TI_score11, WT_score_2=WT_score22, ET_score_2=ET_score22, TI_score_2=TI_score22, Score1=Score, Knockout_1=Knockout_11, Knockout_2=Knockout_22, Score2=Score_2, Knockout_3=Knockout_33, Knockout_4=Knockout_44, Score3=Score_3, Knockout_5=Knockout_55, Knockout_6=Knockout_66, Factor_1=Factor, total_income=total, total_debts=Total_Debt, total_security=total2, annual_household_cost=total3, output_wt1=WT1, output_et1=ET1, output_ti1=TI1, output_wt2=WT2, output_et2=ET2, output_ti2=TI2, hlio=HLIO, pr=PR, ER=E_Rate, WCR=WC_Rate, TTI=T_T_I, WCI=W_C_I, TB=T_B, TFI=T_F_I, CT=C_T, SAT=S_A_T, HC=H_C, SAC=S_A_C, WCIC=W_C_I_C, TDC=T_D_C, TEL=T_E_L), 200, {'Content-Type': "application/json"} )
#the error im getting is :
Test Event Name test
Response { "errorMessage": "'queryStringParameters'", "errorType": "KeyError", "stackTrace": [ " File "/var/task/lambda_function.py", line 67, in lambda_handler\n Salary_or_wages = event["queryStringParameters"]['salary/wages']\n" ] }
Function Logs START RequestId: 0c3fcadc-3967-4381-b176-1ec50045b28c Version: $LATEST [ERROR] KeyError: 'queryStringParameters' Traceback (most recent call last): File "/var/task/lambda_function.py", line 67, in lambda_handler Salary_or_wages = event["queryStringParameters"]['salary/wages']
Upvotes: 0
Views: 4769
Reputation: 2400
is your API gateway set up as a Proxy Lambda Integration or a Lambda Integration? Each has a different looking Event structure which changes where the keys are located.
Log the event ( print(event)
will work just fine for a test run) then hit your endpoint and see what the event structure looks like. That will tell you where you keys are supposed to be or if you have the wrong key value (Event structures dont get updated often but it does happen, and tutorial videos become out of date pretty quick)
The Event for a Lambda is (and indeed, all events in all of AWS) are just JSON objects. using python, that directly translates to a Dictionary so having the right key structure can be a little weird to figure out because documentation on what various Events look like in the AWS Sphere are scattered througout the documentation and hard to find.
I usually keep a lambda on my console that all it does is print(event)
then hook it up to whatever trigger I am trying to use at the time so i can see what it looks like nice and clear.
Upvotes: 3