jameskane
asked on
Python - problem with if/elif and use of default table value
I have a mysql table with one of the columns ALPAID_ytd acting as a switch. The default value for the column is 0 (zero) and this represents the off position. The column is changed to a value of 1 when the switch is on. This switch is programmed on/of as the user deploys the application. Below is an image of the mysql panel showing the setup.
While I have no problems switching on/off and updating the table, I have a major probLem in using the values in a set of if/elif filters which feed a table with purchase information. For example
In the above, !=0 gets me the value 1. No problem
However, != 1 does not resolve to 'NOT EQUAL 1' (zero) but rather to !=0 which is 1. ==0 resolves exactly the same way.
Consequently it is not possible to run the filters as I cannot catch the value 0.
Here is an output example showing the problem. Note that the actual value of ALPAID_ytd is 0, but 1 gets inserted into the if/elif code.
Finally, here is the code from my testing page. I have set the value of ALPAID_ytd to 0 (the problem value). The variable "status" is a descriptor for the order (mylist). There are three possibilities which are defined by the mylist sublists
2 ........ eg mylist = ['1008', [4], [4], [4]]
0......... eg mylist = ['1008', [4], [29], [4]]
1 ..........eg mylist = ['1008', [29], [29], [29]]
You could plug these into line 88 of the code to use different values of "status" - although I do not believe this is connected to the problem
Hopefully you can spot a solution (excluding changing the default value of ALPAID_ytd to 0 !!)
James
While I have no problems switching on/off and updating the table, I have a major probLem in using the values in a set of if/elif filters which feed a table with purchase information. For example
if ALPAID_ytd != 0 and status == 1:
print ("elif ALPAID_ytd != 0 and status == 1: fired")
elif ALPAID_ytd != 1 and status == 1:
print("elif ALPAID_ytd != 1 and status == 1: fired")
In the above, !=0 gets me the value 1. No problem
However, != 1 does not resolve to 'NOT EQUAL 1' (zero) but rather to !=0 which is 1. ==0 resolves exactly the same way.
Consequently it is not possible to run the filters as I cannot catch the value 0.
Here is an output example showing the problem. Note that the actual value of ALPAID_ytd is 0, but 1 gets inserted into the if/elif code.
Finally, here is the code from my testing page. I have set the value of ALPAID_ytd to 0 (the problem value). The variable "status" is a descriptor for the order (mylist). There are three possibilities which are defined by the mylist sublists
2 ........ eg mylist = ['1008', [4], [4], [4]]
0......... eg mylist = ['1008', [4], [29], [4]]
1 ..........eg mylist = ['1008', [29], [29], [29]]
You could plug these into line 88 of the code to use different values of "status" - although I do not believe this is connected to the problem
Hopefully you can spot a solution (excluding changing the default value of ALPAID_ytd to 0 !!)
James
#!C:\Python34\python.exe
import collections
import cgi,cgitb
import mysql.connector as conn
def htmlTop():
print("""Content-type:text/html\n\n
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title> My Server-side template</title>
</head>
<body>""")
def htmlTail():
print(""" its done</body>
</html>""")
def connectDB():
db=conn.connect(host='localhost' ,user='root' ,passwd='844cheminduplan' ,db='office')
cursor = db.cursor()
return db, cursor
def activityinfo(db,cursor,courseNumber):
cursor.execute("""select courseNumber,Activity, tariffstwelve, tariffsthirtysix from activities WHERE courseNumber = "%s" """
%(courseNumber))
activitydata = cursor.fetchall()
return activitydata
def SelectALPAID_ytd(db,cursor):
cursor.execute("""select ALPAID_ytd from registrations WHERE memberID = "%s" """
%(memberID))
ALPAID_ytd=cursor.fetchall()
return ALPAID_ytd
def OrderProfiler(mylist,db,cursor):
# provides a list of 1s nd 0eros to describe the list in terms of paid and activity libre activities
OrderProfile = []
#del mylist[0] # chopping off the memberID
mylist = mylist[1] + mylist[2] + mylist[3]
print(mylist, "this is mylist")
for each in mylist:
courseNumber = each
activitydata = activityinfo(db,cursor,courseNumber)
#print(activitydata, "this is activity data")
#Note activitydata[0][2] is price for 12 trim and activitydata[0][3] is for 36 trim
if activitydata[0][2] == 0 and activitydata[0][3] == 0:
OrderProfile.append(0) #activityLibre
elif activitydata[0][2] >0 and activitydata[0][3] > 0:
OrderProfile.append(1) # paid
return OrderProfile
def StatusFinder (OrderProfile):
# declares an order as being mixed (0), activity libre only(1) or paid only(2) items
# uses OrderProfile list from OrderProfiler(mylist, db, cursoro) above
# this status along with the ALPAID_YTD flag in registrations table determines if DELTA ORDER should be
# charged the activity libre registration fee, or should be reimboursed the activity libre registration fee
print (collections.Counter(OrderProfile))
OrderProfileCount = (collections.Counter(OrderProfile))
if OrderProfile.count(1) >0 and OrderProfile.count(0) >0:
print(OrderProfile, "OrderProfile")
status = 0 #mixed
elif OrderProfile.count(1) >0 and OrderProfile.count(0)== 0:
status = 2 #paid_only
elif OrderProfile.count(1) ==0 and OrderProfile.count(0)>0:
status = 1 #free_only
return status
#main program
if __name__== "__main__":
try:
htmlTop()
print("--------------------------------------------------------------")
db,cursor=connectDB()
mylist = ['1008', [29], [29], [29]]
OrderProfile = OrderProfiler(mylist,db,cursor)
status = StatusFinder (OrderProfile)
print(status, "this is order status")
memberID = mylist[0]
print (memberID, "this is memberID")
ALPAID_ytd = SelectALPAID_ytd(db,cursor)
ALPAID_ytd = ALPAID_ytd [0][0]
print(ALPAID_ytd, "this is ALPAID_ytd - the on/off switch. Default in table is 0(off) and 1(on)")
print("..........................................")
if ALPAID_ytd != 0 and status == 1:
print ("elif ALPAID_ytd != 0 and status == 1: fired")
elif ALPAID_ytd != 1 and status == 1:
print("elif ALPAID_ytd != 1 and status == 1: fired")
elif ALPAID_ytd != 0 and status == 2:
print ("elif ALPAID_ytd != 0 and status == 2: fired")
elif ALPAID_ytd != 1 and status == 2:
print ("elif ALPAID_ytd != 1 and status == 2:fired")
elif ALPAID_ytd != 0 and status == 0:
print ("elif ALPAID_ytd != 0 and status == 0: fired")
elif ALPAID_ytd != 1 and status == 0:
print ("elif ALPAID_ytd != 1 and status == 0:fired")
else:
print("no result")
htmlTail()
except:
cgi.print_exception()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Mark and Nas,
PROBLEM SOLVED !!!!
Many thanks to you both for the help. and insight.
Guess I need to swot up some on mysql server !!
James
PROBLEM SOLVED !!!!
Many thanks to you both for the help. and insight.
Guess I need to swot up some on mysql server !!
James
Open in new window
In your code as-is, just change to check for '0' and '1' strings. Long term, maybe check for equality rather than inequality (that might have helped you to spot the problem earlier. maybe)