قطع أسفل قائمة على أساس الأحداث يجري بين اثنين datetimes
-
28-09-2019 - |
سؤال
لدي قائمة من وجوه أنه من بين أجزاء أخرى من البيانات ، وتشمل موعد و لا تحتاج إلى إنشاء قائمة من جميع الأشياء حيث أن تاريخ السقوط في أي وقت في الشهر الماضي ، أيمنتصف الليل على 1 من الشهر الماضي < البيانات المستهدفة < منتصف الليل يوم 1 من هذا الشهر.
أنا أيضا في حاجة إلى عدد من مجموع الكائنات التي تلبي هذه المعايير.
الآن أنا ذاهب عن ذلك في سلسلة من الحلقات ، ولكن أشعر أن هناك يجب أن يكون طريقة أفضل ، خاصة وأن السيناريو توقف:
post = 0 #the current post we're analyzing
posts = 0 #the total number of posts in the month we actually care about
lastmonthposts = [] #I think i can just get rid of this
blog = pyblog.WordPress()
date = blog.get_recent_posts(1 + posts)[0]['dateCreated']
while (date > startthismonth):
print "So far, there have been " + str(posts) + " posts this month we've counted."
post = post + 1
date = blog.get_recent_posts(1 + post)[0]['dateCreated']
while (date > startlastmonth):
print "So far, there have been " + str(posts) + " posts last month we've counted, which is " + str(date.timetuple().tm_mon) + "."
posts = posts + 1
post = post + 1
date = blog.get_recent_posts(1 + post)[0]['dateCreated']
lastmonthposts.append('blog')
for blogpost in lastmonthposts:
postnumber = blogpost['postid']
comments = comments + int(blog.get_comment_count(postnumber)['approved'])
المحلول
بدلا من get_recent_posts()
وأود أن استخدام get_page_list()
:
from datetime import datetime, timedelta
this_month_start = datetime.now().date().replace(day=1)
prev_month_start = (this_month_start - timedelta(days=1)).replace(day=1)
pages = blog.get_page_list()
last_month_pages = [
p for p in pages
if prev_month_start <= p['dateCreated'] < this_month_start]
last_month_approved_comment_count = sum(
blog.get_comment_count(page['page_id'])['approved']
for page in last_month_pages)
print "number of last month's pages:", len(last_month_pages)
print "number of approved comments for last month's pages:",
print last_month_approved_comment_count
لا تنتمي إلى StackOverflow