如何将数据存储中值为 True 的所有属性移动到另一个数据存储?
有没有办法将模型中值为 True 的所有属性移动到另一个模型? 我正在用 Python 编写并具有以下内容:
class crimechecker(webapp.RequestHandler):
def get(self):
#Checks for crime
articles = Article.all().filter('crime = ', None)
for article in articles:
crime = False
for word in triggers:
body = article.body
if body.find(word) != -1:
crime = True
article.crime = crime
a = article.put()
然后运行一个单独的 cron:并且每个犯罪故事及其位置都添加到 Story 中。 但故事并没有出现在故事模型中?!
class place(webapp.RequestHandler):
def post(self):
# Check for any article which was classified as "TRUE" therefore it is a crime document
crimes = Article.all().filter('crime = ', True)
for crimestory in crimes:
if Story.all().filter('title = ', crimestory.title).count() == 0:
#Yahoo Placemaker key
p = placemaker('HSnG9pPV34EUBcexz.tDYuSrZ8Hnp.LowswI7TxreF8sXrdpVyVIKB4uPGXBYOA9VjjF1Ca42ipd_KhdJsKYjI5cXRo0eJM-')
#Encoding for symbols and euro signs etc.
print p.find_places(crimestory.body.encode('utf-8'))
for place in p.places:
splitted = place.name.split()
#Check for locations within Ireland (IE)
if 'IE' in splitted:
story = Story(long=place.centroid.longitude, lat=place.centroid.latitude, link=crimestory.link, loc_name=place.name, title=crimestory.title, date=crimestory.date).put()
logging.info(story)
我有 2 个模型:文章和故事。 所有文章都存储在文章模型中,任何带有crime = True 的文章都设置在Story 模型中。 由于某种原因,它并没有推动故事的发展。 cron 正在运行并且没有任何日志错误。 我可以在我的仪表板中执行此任务吗? 我查询过这两种型号:
从文章中选择 * 按日期 DESC
文章模型包含今天(5 月 2 日)以来的故事,
故事包含 4 月 19 日以来的文章,此后不再有文章。 我可以查询模型并说将所有犯罪设置为真实的实体转移到故事模型吗?
Is there a way of moving all attributes within a model with a value set to True to another model?
I am writing in Python and have the following:
class crimechecker(webapp.RequestHandler):
def get(self):
#Checks for crime
articles = Article.all().filter('crime = ', None)
for article in articles:
crime = False
for word in triggers:
body = article.body
if body.find(word) != -1:
crime = True
article.crime = crime
a = article.put()
Then a separate cron is run: and each crime story is added to Story with their location.
But the stories are not appearing in the Story model?!
class place(webapp.RequestHandler):
def post(self):
# Check for any article which was classified as "TRUE" therefore it is a crime document
crimes = Article.all().filter('crime = ', True)
for crimestory in crimes:
if Story.all().filter('title = ', crimestory.title).count() == 0:
#Yahoo Placemaker key
p = placemaker('HSnG9pPV34EUBcexz.tDYuSrZ8Hnp.LowswI7TxreF8sXrdpVyVIKB4uPGXBYOA9VjjF1Ca42ipd_KhdJsKYjI5cXRo0eJM-')
#Encoding for symbols and euro signs etc.
print p.find_places(crimestory.body.encode('utf-8'))
for place in p.places:
splitted = place.name.split()
#Check for locations within Ireland (IE)
if 'IE' in splitted:
story = Story(long=place.centroid.longitude, lat=place.centroid.latitude, link=crimestory.link, loc_name=place.name, title=crimestory.title, date=crimestory.date).put()
logging.info(story)
I have 2 models: an Article and Story.
All articles are stored in the article model and any article with crime = True is set to be in the Story model.
For some reason it is not moving the stories.
The cron is running and not having any log errors.
Can I do this task in my dashboard?
I have queried both models :
SELECT * FROM Article ORDER BY date DESC
The Article model has stories from todays date (May 2nd)
Story has articles from April 19th and no more since then.
Can I query the models and say move all entities with crime set to true to the Story model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有看到你的代码有任何明显的错误。由于我们已经确定不存在错误,因此我的建议是在更高的位置添加更多的logging.info调用,以查看哪个if语句的计算结果为false,或者哪个for循环正在空集上迭代。另外,您是否确认 Crimechecker 已成功将新的犯罪故事设置为 True?听起来您并没有确定两个 cron 作业中的哪一个有问题。
更根本的是,我认为你应该重新考虑这个任务的基本设计。我们将其分为 3 个步骤:
为什么不在保存文章时在一个处理程序中完成所有这些工作呢?为什么要把它分成三个不同的部分?除了使操作变得复杂之外,您的第二个 cron 作业在规模上也效率低下。您将从一开始就获取每个犯罪故事,并对每个故事进行一次查询,看看是否有相应的故事。如果您积累了大量文章,则该过程将无法及时完成。
如果您担心首次保存文章时执行所有这些任务会对性能产生影响,请使用任务队列。首次保存文章时,创建一个任务来扫描其中是否有犯罪关键字。如果找到关键字,则创建一个任务来存储相应的故事实体。在任务参数中传递实体键,这样您就不必查询任何内容。
I don't see anything obviously wrong with your code. Since we've already established that there are no errors, my advice would be to add more logging.info calls higher up to see which if statement is evaluating to false, or which for loop is iterating over an empty set. Also, have you confirmed that crimechecker is successfully setting new crime stories to True? It doesn't sound like you've determined which of your two cron jobs is at fault.
More fundamentally, I think you should re-consider the basic design of this task. Let's classify it as 3 steps:
Why not just do all of this work in one handler when the article is saved? Why break it out three distinct parts? Besides making the operation complex, your 2nd cron job is also inefficient at scale. You're fetching every crime story since the beginning of time and doing one query for each to see if there's a corresponding story. If you accumulate any significant number of articles, this won't complete in time.
If you're worried about the performance impact of doing all of these tasks when the article is first saved, use the task queue. When the article is first saved, create one task to scan it for crime keywords. If the keywords are found, create one task to store the corresponding story entity. Pass the entity key around in the task parameters so you don't have to query for anything.