我如何使用timesince

发布于 2024-11-14 08:52:36 字数 2194 浏览 1 评论 0原文

我找到了这个片段:

def timesince(dt, default="just now"):
        now = datetime.utcnow()
        diff = now - dt
        periods = (
                (diff.days / 365, "year", "years"),
        (diff.days / 30, "month", "months"),
        (diff.days / 7, "week", "weeks"),
        (diff.days, "day", "days"),
        (diff.seconds / 3600, "hour", "hours"),
        (diff.seconds / 60, "minute", "minutes"),
        (diff.seconds, "second", "seconds"),
    )
    for period, singular, plural in periods:
            if period:
                        return "%d %s ago" % (period, singular if period == 1 else plural)
    return default

并且想在 Google Appegine 中对我的数据库进行查询时在输出中使用它。 我的数据库看起来像这样:

class Service(db.Model):
    name = db.StringProperty(multiline=False)
    urla = db.StringProperty(multiline=False)
    urlb = db.StringProperty(multiline=False)
    urlc = db.StringProperty(multiline=False)
    timestampcreated = db.DateTimeProperty(auto_now_add=True)
    timestamplastupdate = db.DateTimeProperty(auto_now=True)

在 web 应用程序请求处理程序的主页中我想做:

            elif self.request.get('type') == 'list':
                    q = db.GqlQuery('SELECT * FROM Service')
                    count = q.count()
                    if count == 0:
                            self.response.out.write('Success: No services registered, your database is empty.')
                    else: 
                            results = q.fetch(1000)
                            for result in results:
                                    resultcreated = timesince(result.timestampcreated)
                                    resultupdated = timesince(result.timestamplastupdate)
                                    self.response.out.write(result.name + '\nCreated:' + resultcreated + '\nLast Updated:' + resultupdated + '\n\n')

我做错了什么?我在使用代码片段格式化代码时遇到问题。

我应该做哪一项?

这?

def timesince:
class Service
class Mainpage
  def get(self):

或者这个?

class Service
class Mainpage
  def timesince:
  def get(self):

我对 Python 不太熟悉,希望能提供有关如何解决此问题的任何意见。谢谢!

I found this snippet:

def timesince(dt, default="just now"):
        now = datetime.utcnow()
        diff = now - dt
        periods = (
                (diff.days / 365, "year", "years"),
        (diff.days / 30, "month", "months"),
        (diff.days / 7, "week", "weeks"),
        (diff.days, "day", "days"),
        (diff.seconds / 3600, "hour", "hours"),
        (diff.seconds / 60, "minute", "minutes"),
        (diff.seconds, "second", "seconds"),
    )
    for period, singular, plural in periods:
            if period:
                        return "%d %s ago" % (period, singular if period == 1 else plural)
    return default

and want to use it in an output when doing a query to my database in Google Appegine.
My database looks like so:

class Service(db.Model):
    name = db.StringProperty(multiline=False)
    urla = db.StringProperty(multiline=False)
    urlb = db.StringProperty(multiline=False)
    urlc = db.StringProperty(multiline=False)
    timestampcreated = db.DateTimeProperty(auto_now_add=True)
    timestamplastupdate = db.DateTimeProperty(auto_now=True)

In the mainpage of the webapp requesthandler I want to do:

            elif self.request.get('type') == 'list':
                    q = db.GqlQuery('SELECT * FROM Service')
                    count = q.count()
                    if count == 0:
                            self.response.out.write('Success: No services registered, your database is empty.')
                    else: 
                            results = q.fetch(1000)
                            for result in results:
                                    resultcreated = timesince(result.timestampcreated)
                                    resultupdated = timesince(result.timestamplastupdate)
                                    self.response.out.write(result.name + '\nCreated:' + resultcreated + '\nLast Updated:' + resultupdated + '\n\n')

What am I doing wrong? I'm having troubles with formatting my code using the snippet.

Which one of these should I do?

this?

def timesince:
class Service
class Mainpage
  def get(self):

or this?

class Service
class Mainpage
  def timesince:
  def get(self):

I'm not too familiar with Python and would appreciate any input on how to fix this. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青瓷清茶倾城歌 2024-11-21 08:52:36

这对我来说效果很好:

from datetime import datetime 
def timesince(dt, default="now"):
    now = datetime.now()
    diff = now - dt
    periods = (
        (diff.days / 365, "year", "years"),
        (diff.days / 30, "month", "months"),
        (diff.days / 7, "week", "weeks"),
        (diff.days, "day", "days"),
        (diff.seconds / 3600, "hour", "hours"),
        (diff.seconds / 60, "minute", "minutes"),
        (diff.seconds, "second", "seconds"),
    )
    for period, singular, plural in periods:
        if period >= 1:
            return "%d %s ago" % (period, singular if period == 1 else plural)
    return default

timesince(datetime(2016,6,7,12,0,0))
timesince(datetime(2016,6,7,13,0,0))
timesince(datetime(2016,6,7,13,30,0))
timesince(datetime(2016,6,7,13,50,0))
timesince(datetime(2016,6,7,13,52,0))

This works fine for me:

from datetime import datetime 
def timesince(dt, default="now"):
    now = datetime.now()
    diff = now - dt
    periods = (
        (diff.days / 365, "year", "years"),
        (diff.days / 30, "month", "months"),
        (diff.days / 7, "week", "weeks"),
        (diff.days, "day", "days"),
        (diff.seconds / 3600, "hour", "hours"),
        (diff.seconds / 60, "minute", "minutes"),
        (diff.seconds, "second", "seconds"),
    )
    for period, singular, plural in periods:
        if period >= 1:
            return "%d %s ago" % (period, singular if period == 1 else plural)
    return default

timesince(datetime(2016,6,7,12,0,0))
timesince(datetime(2016,6,7,13,0,0))
timesince(datetime(2016,6,7,13,30,0))
timesince(datetime(2016,6,7,13,50,0))
timesince(datetime(2016,6,7,13,52,0))
以酷 2024-11-21 08:52:36

我不太清楚您遇到的问题是什么,所以请耐心等待。回溯会很有帮助。 :)

timesince() 不需要任何成员变量,所以我认为它不应该位于其中一个类中。如果我处于您的情况,我可能会将 timessince 放入其自己的文件中,然后将该模块导入到定义 Mainpage 的文件中。

如果您将它们全部放在同一个文件中,请确保间距一致并且没有任何制表符。

I'm not totally clear what the problem you're having is, so bear with me. A Traceback would be helpful. :)

timesince() doesn't require any member variables, so I don't think it should be inside one of the classes. If I were in your situation, I would probably put timesince in its own file and then import that module in the file where Mainpage is defined.

If you're putting them all in the same file, make sure that your spacing is consistent and you don't have any tabs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文