I am having some trouble passing a variable into a function and having it return a desired value. For clarity, I am obtaining a record from a PostgreSQL table as a tuple and then passing the relevant index of that tuple into a class variable. Everything else seems to work but this and I have no idea why.
The code is as follows:
from datetime import date
from decimal import Decimal
from config import config
import psycopg2
conn = None
try:
params = config()
conn = psycopg2.connect(**params)
cur = conn.cursor()
cur.execute('SELECT * FROM testdb.vitals ORDER BY "vitalsID"')
except (Exception, psycopg2.DatabaseError) as error:
print(error)
row = cur.fetchone()
class Vitals:
birth = date(1990, 12, 12)
today = date.today()
years = today.year - birth.year
age = today.year - birth.year - ((today.month, today.day) < (birth.month, birth.day))
weight = row[2]
height = row[3]
activity = row[4]
goal = row[5]
fatRatio = row[6]
carbohydrateRatio = row[7]
proteinRatio = row[8]
def __init__(self):
# connect and initialise vitals
pass
# display on gui
@classmethod
def activityText(cls):
if cls.activity == Decimal(1.20):
return testActivity * 2
@classmethod
def bmr(cls):
return (Decimal(10) * cls.weight) + (Decimal(6.25) * cls.height) - (Decimal(5) * cls.age) + Decimal(5)
@classmethod
def tdee(cls):
return Decimal(outputBmr * cls.activity)
@classmethod
def net(cls):
if cls.goal == 1:
return outputTdee - (outputTdee * Decimal(0.1))
else:
return outputTdee
importVitals = Vitals
testActivity = importVitals.activity
print(testActivity)
testActivityfunc = importVitals.activityText()
print(testActivityfunc)
outputBmr = round(importVitals.bmr())
print(outputBmr)
outputTdee = round(importVitals.tdee())
print(round(outputTdee))
outputNet = round(importVitals.net())
print(outputNet)
if importVitals.activity == Decimal(1.20):
print('Hello')
else:
print("\n")
print(importVitals.activity)
print(type(importVitals.activity))
The key class method is here:
@classmethod
def activityText(cls):
if cls.activity == Decimal(1.20):
return testActivity * 2
Output (following alteration of the final if statement in the module) is as follows:
1.20
<class 'decimal.Decimal'>
None
1751
2101
1891
1.20
<class 'decimal.Decimal'>
Process finished with exit code 0
For readability, here are the print statements:
testActivity = importVitals.activity
print(testActivity)
print(type(importVitals.activity))
testActivityfunc = importVitals.activityText()
print(testActivityfunc)
outputBmr = round(importVitals.bmr())
print(outputBmr)
outputTdee = round(importVitals.tdee())
print(round(outputTdee))
outputNet = round(importVitals.net())
print(outputNet)
if importVitals.activity == Decimal(1.20):
print('Hello')
else:
print("\n")
print(importVitals.activity)
print(type(importVitals.activity))
The class method in question always returns None
. Also note the if statement at the end of the module. It always runs the else branch which, at least to me, bizarrely prints 1.20
and <class 'decimal.Decimal'>
. So if Python recognises the instanced class variable of importVitals.activity
as having the value 1.20
and the variable type of <class 'decimal.Decimal'>
, then why is the if statement or the class method not returning what I would like it to? Is there something I am doing wrong here?
Just to reiterate, all of the other methods and print statements are working as expected so I am quite puzzled by this.
Thank you to anybody who took the time to read this. I would appreciate any help you can offer.
if Vitals.activity == Decimal(1.20):
Shouldn't that beif importVitals.activity == Decimal(1.20):
?