我如何使用timesince
我找到了这个片段:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对我来说效果很好:
This works fine for me:
我不太清楚您遇到的问题是什么,所以请耐心等待。回溯会很有帮助。 :)
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.