This is the model in Google App Engine:
class Rep(db.Model):
mAUTHOR = db.UserProperty(auto_current_user=True)
mUNIQUE = db.StringProperty()
mCOUNT = db.IntegerProperty()
mDATE = db.DateTimeProperty(auto_now=True)
mDATE0 = db.DateTimeProperty(auto_now_add=True)
mWEIGHT = db.FloatProperty()
So, mCOUNT is integer and mWEIGHT is float. I calculate the age of the item like this:
age1 = datetime.datetime.now() - rep.mDATE0
age_hour = float(age1.seconds) / 3600
rep.mWEIGHT = float((rep.mCOUNT - 1) / (age_hour + 2)**1.5)
But when I try a query like this:
QUERY = Rep.all()
QUERY.filter("mAUTHOR =", user)
QUERY.order("-mWEIGHT")
RESULTS = QUERY.fetch(10)
for result in RESULTS:
self.response.out.write("mUNIQUE: <b>%s</b> | "
"mWEIGHT: %f | "
"mCOUNT: %s | <br />"
% (result.mUNIQUE,
result.mWEIGHT,
# line 103
result.mCOUNT,
))
I get a TypeError on line 103 which is
result.mCOUNT,
TypeError: a float is required
Why would mCOUNT be float? By the way, the error is raised only if the item is not in the datastore and it is written for the first time by the else clause of the if loop.
Can you help me use the correct type? Thanks for your help.
-----------------------------------------------------
EDIT
I just noticed that I had %f for string formatting for mWEIGHT and changing that to %s seems to solve the problem (but I don't know why. Is it because mWEIGHT=None?):
for result in RESULTS:
self.response.out.write("mUNIQUE: <b>%s</b> | "
# changing %f to %s appears to solve the problem.
"mWEIGHT: %f | "
"mCOUNT: %s | <br />"
% (result.mUNIQUE,
result.mWEIGHT if result.mWEIGHT is not None else 0.0,
result.mWEIGHT,
result.mCOUNT,
))
And here are some values:
mUNIQUE: A | mWEIGHT: 0.299954933969 | mCOUNT: 2 |
mUNIQUE: Z | mWEIGHT: 0.0 | mCOUNT: 1 | # With answer by TokenMacGuy
mUNIQUE: R | mWEIGHT: None | mCOUNT: 1 | # with %f changed to %s
mUNIQUE: P | mWEIGHT: None | mCOUNT: 1 | # with %f changed to %s
Any suggestions how I can add rep.mWEIGHT to the else clause?
------------------------------------------------------------
The entire code is below:
K = []
s = self.request.get('sentence')
K.append(s)
K = f2.remove_empty(K[0].split('\r\n'))
UNIQUES = f2.f2(K)
COUNTS = f2.lcount(K, UNIQUES)
C_RESULT = "no results yet"
for i in range(len(UNIQUES)):
C_QUERY = Rep.all()
C_QUERY.filter("mAUTHOR =", user)
C_QUERY.filter("mUNIQUE =", UNIQUES[i])
C_RESULT = C_QUERY.fetch(1)
if C_RESULT:
rep = C_RESULT[0]
rep.mCOUNT+=COUNTS[i]
age1 = datetime.datetime.now() - rep.mDATE0
age_hour = float(age1.seconds) / 3600
rep.mWEIGHT = float((rep.mCOUNT - 1) / (age_hour + 2)**1.5)
self.response.out.write("<b>rep.UNIQUE: %s</b>: <br />"
# "rep.mCOUNT: %s <br />"
"rep.mWEIGHT: %s <br />"
# "C_RESULT: %s <br />"
# "rep: %s <br />"
# "utc_tuple: %s <br />"
# "mDATE0_epoch: %s <br />"
"rep.mDATE0: %s "
"age_hour: %s <br />"
#
% (rep.mUNIQUE,
# rep.mCOUNT,
rep.mWEIGHT,
# C_RESULT,
# rep,
# utc_tuple,
# mDATE0_epoch,
rep.mDATE0,
age_hour,
))
rep.put()
else:
rep = Rep()
rep.mCOUNT = COUNTS[i]
rep.mUNIQUE = UNIQUES[i]
rep.put()
self.response.out.write("<b>rep.UNIQUE: %s</b>: |"
"rep.mCOUNT: %s: <br />"
% (rep.mUNIQUE,
rep.mCOUNT,))
QUERY = Rep.all()
QUERY.filter("mAUTHOR =", user)
QUERY.order("-mWEIGHT")
RESULTS = QUERY.fetch(10)
for result in RESULTS:
self.response.out.write("mUNIQUE: <b>%s</b> | "
"mWEIGHT: %f | "
"mCOUNT: %s | <br />"
% (result.mUNIQUE,
result.mWEIGHT,
result.mCOUNT,
))
It looks like you are occasionally not setting mWEIGHT. How about changing
for result in RESULTS:
self.response.out.write("mUNIQUE: <b>%s</b> | "
"mWEIGHT: %f | "
"mCOUNT: %s | <br />"
% (result.mUNIQUE,
result.mWEIGHT,
result.mCOUNT,
))
to
for result in RESULTS:
self.response.out.write("mUNIQUE: <b>%s</b> | "
"mWEIGHT: %f | "
"mCOUNT: %s | <br />"
% (result.mUNIQUE,
result.mWEIGHT if result.mWEIGHT is not None else 0.0,
result.mCOUNT,
))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With