Google App Engine中的工作日期时间对象 [英] Working datetime objects 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 =真的)
mWEIGHT = db.IntegerProperty()
我想做:
mWEIGHT = mCOUNT / mDATE0
$ b $ l
$ Rep.all()
C_QUERY.filter(mAUTHOR =,user)
C_QUERY.filter(mUNIQUE =,UNIQUES [i])
C_RESULT = C_QUERY.fetch(1)
如果C_RESULT:
rep = C_RESULT [0]
rep.mCOUNT + = COUNTS [i]
#如何将mDATE0转换为整数,以便我可以划分:
#rep.mWEIGHT = rep.mCOUNT / rep.mDATE0
rep.put()
else:
C = COUNTS [i]
S = UNIQUES [i]
write_to_db(S,C)
我在其他几个论坛中提到了同样的问题,我收到了很有价值的建议,但我仍然无法做出这个代码工作,因为我对(对象,实例,datetime.datetime,秒,秒...等等)感到困惑。例如,我以为
mWEIGHT = mCOUNT / rep.mDATE0.second
会将mDATE0转为秒;但是它没有,它只是从 2010-11-12 18:57:27.338000
的第二部分,即27。
和
mWEIGHT = mCOUNT / mDATE0.date
给出类型不匹配错误消息。
我也尝试过
rep.mWEIGHT = rep.mCOUNT / rep.mDATE0.toordinal()
这样一个数字,如 734088
,但所有项目都有相同的数字。
另请参阅我在同一主题上的上一个问题。
感谢您的帮助。
EDIT3
这个工程,谢谢!
如果C_RESULT:
rep = C_RESULT [0]
rep.mCOUNT + = COUNTS [i]
utc_tuple = rep.mDATE0.utctimetuple()
#这实际上是float不是整数
mDATE0_integer = time.mktime(utc_元组)
mDATE0_day = mDATE0_integer / 86400
rep.mWEIGHT = float(rep.mCOUNT / mDATE0_day)
rep.put()
EDIT2
@Constantin:我意识到数字需要是浮点数:
>>> mCOUNT = 35
>>> div = mCOUNT / mDATE0
>>>> div
0
>>> div = float(mCOUNT)/ float(mDATE0)
>>> div
2.7140704010987625e-08
>>>>
不知道如何将它合并到脚本中。任何建议?
编辑
@Constantin:
对于项目
C_RESULT [0] =新项目
这是我得到的结果。
新项目:
rep:&_ _ main __。Rep对象在0x052186D0>
mDATE0_integer:1289575981
rep.mCOUNT:35
rep.mWEIGHT:0
所以
mDATE0_integer = int(time.mktime(rep.mDATE0.utctimetuple()))
的作品,并给出整数 1289575981
但这个部门
rep.mWEIGHT = rep.mCOUNT / mDATE0_integer
结果为0.任何建议?
这应该做你想要的: p>
导入时间
mWEIGHT = mCOUNT / time.mktime(mDATE0.utctimetuple())
请参阅 mktime
。
mCOUNT / mDATE0.date
失败,因为
int
和 date
中没有除法运算符。
toordinal
不适合您,因为它的运行时间完全不考虑时间。
In this model
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.IntegerProperty()
I want to do:
mWEIGHT = mCOUNT / mDATE0
within this for loop
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]
# how to convert mDATE0 to integer so that I can divide:
# rep.mWEIGHT = rep.mCOUNT / rep.mDATE0
rep.put()
else:
C = COUNTS[i]
S = UNIQUES[i]
write_to_db(S, C)
I asked the same question in several other forums and I got good and valuable advice but I am still unable to make this code work because I am confused about (objects, instance, datetime.datetime, seconds, second ... and so on) For instance, I thought that
mWEIGHT = mCOUNT / rep.mDATE0.second
would turn mDATE0 into seconds; but it does not, it just take the second part from 2010-11-12 18:57:27.338000
ie, 27.
And
mWEIGHT = mCOUNT / mDATE0.date
gives an type mismatch error message.
I also tried
rep.mWEIGHT = rep.mCOUNT / rep.mDATE0.toordinal()
this gives a number like 734088
but all items had the same number.
See also my previous question on the same subject.
Thank you for your help.
EDIT3
This works, thanks!
if C_RESULT:
rep = C_RESULT[0]
rep.mCOUNT+=COUNTS[i]
utc_tuple = rep.mDATE0.utctimetuple()
# this is actually float not integer
mDATE0_integer = time.mktime(utc_tuple)
mDATE0_day = mDATE0_integer / 86400
rep.mWEIGHT = float(rep.mCOUNT / mDATE0_day)
rep.put()
EDIT2 @Constantin: I realized that numbers need to be floats:
>>> mCOUNT = 35
>>> div = mCOUNT / mDATE0
>>> div
0
>>> div = float(mCOUNT) / float(mDATE0)
>>> div
2.7140704010987625e-08
>>>
Not sure how to incorporate this to the script. Any suggestions?
EDIT
@Constantin:
For the item
C_RESULT[0] = "new item"
this is the result I get.
new item:
rep: <__main__.Rep object at 0x052186D0>
mDATE0_integer: 1289575981
rep.mCOUNT: 35
rep.mWEIGHT: 0
So
mDATE0_integer = int(time.mktime(rep.mDATE0.utctimetuple()))
works and gives the integer 1289575981
but this division
rep.mWEIGHT = rep.mCOUNT / mDATE0_integer
results in 0. Any suggestions?
This should do what you want:
import time
mWEIGHT = mCOUNT / time.mktime(mDATE0.utctimetuple())
See mktime
.
mCOUNT / mDATE0.date
fails because there is no division operator for int
and date
.
toordinal
doesn't suit you because it operates on dates and disregards time completely.
这篇关于Google App Engine中的工作日期时间对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!