datetime.datetime.now()返回旧值 [英] datetime.datetime.now() returns old value

查看:218
本文介绍了datetime.datetime.now()返回旧值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过匹配日期查找python中的数据存储区条目。我想要的是每天选择今天的条目。但是由于某些原因,当我将代码上传到gae服务器时,它将只工作一天,第二天仍然返回相同的值。



例如当我上传我的代码并在07-01-2014执行它返回的值为07-01-2014,但第二天08-01-2014它仍然返回07-01-2014。



如果我重新部署相同的代码并再次执行它,它会移动到08-01-2014,但会在第二天再次失败。



在我的开发环境中,它的工作正常...



为什么?感谢您的帮助!

  class ClosingValueHandler(webapp2.RequestHandler):
def get(self):
sct()

(...)



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ (日期):

query =SELECT * FROM Value WHERE date = DATE('%s')%str(date)
try:
q = db.GqlQuery查询)
value = q.get()

除了异常作为e:
logging.error(无法获取日期'%s'的值值\\\
错误消息:%s \\\
%(str(date),str(e)))
返回

返回值


解决方案

正如@ blackbullet4749所说,这是代码导致您出现问题的一部分:

  def sct(from_date = datetime.datetime.now()。date()):
value = getValueByDate(from_date)

具体来说,你期望这段代码运行 datetime.datetime.now()。date() / code>每次你调用这个函数。



实际发生的是 datetime.datetime.now()。date 运行一次,而不是当函数被调用时,但是当它被首先定义时 - 也就是说,当函数对象被实例化时。执行代码时,Python实际上会运行这些代码行,并创建一个像任何其他对象一样存在的函数对象。默认参数在一个分配为功能对象属性的元组中。



要自己看这个,请尝试:

  def sct(from_date = datetime.datetime.now()。date()):
value = getValueByDate(from_date)

print sct.func_defaults

无论您在打印出的元组中看到的是from_date值,不要重复使用你的代码。



如果你想能够调用该函数的默认行为是当前的时间,但是可以选择覆盖,然后这样做:

  def sct(from_date = None):
如果from_date为无:
from_date = datetime.datetime.now()。date()
value = getValueByDate(from_date)

有关默认参数值的更多详细信息,请参阅此 effbot post



btw,不确定这个功能应该做什么似乎没有返回任何东西。


I'm looking up a datastore entry in python by matching dates. What i want is pick the entry for "today" on each day. But for some reason when i upload my code to the gae server it'll work just for one day and on the next day it still returns the same value.

e.g. when i upload my code and execute it on 07-01-2014 it returns the value of 07-01-2014 but the next day on 08-01-2014 it still returns 07-01-2014.

If i redeploy the same code and execute it again it moves on to the 08-01-2014 but will again fail on the next day.

In my development environment it works fine...

Why? Thanks for your help!

class ClosingValueHandler(webapp2.RequestHandler):
    def get(self):
        sct()

(...)

def sct(from_date=datetime.datetime.now().date()):
    value = getValueByDate(from_date)

def getValueByDate(date):

    query = "SELECT * FROM Value WHERE date = DATE('%s')" % str(date)
    try:
        q = db.GqlQuery(query)
        value = q.get()

    except Exception as e:
        logging.error("Could not fetch Value value for date '%s' \n Error message: %s \n" % (str(date), str(e)))
        return

    return value

解决方案

As @blackbullet4749 mentions, it's this bit of the code causing you problems:

def sct(from_date=datetime.datetime.now().date()):
    value = getValueByDate(from_date)

Specifically, you're expecting this code to run datetime.datetime.now().date() every time you call the function.

What actually happens is that datetime.datetime.now().date() is run once, not when the function is called, but when it is first defined - which is to say, when the function object is instantiated. When executing your code, Python actually runs those lines of code and creates a function object that exists just like any other object. The default parameters live in a tuple assigned as an attribute of the function object.

To see this for yourself, try this:

def sct(from_date=datetime.datetime.now().date()):
    value = getValueByDate(from_date)

print sct.func_defaults

Whatever you see in that tuple that gets printed out is the "from_date" value that keeps being reused in your code.

If you want to be able to call the function with the default behavior being that it takes the current time, but with the option to override, then do this instead:

def sct(from_date=None):
    if from_date is None:
        from_date = datetime.datetime.now().date()
    value = getValueByDate(from_date)

For more details on default parameter values, see this effbot post.

btw, not sure what this function is supposed to do. Doesn't seem to return anything.

这篇关于datetime.datetime.now()返回旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆