简体   繁体   English

如何将数据存储中值为 True 的所有属性移动到另一个数据存储?

[英]How to move all attributes in a datastore with value True to another datastore?

Is there a way of moving all attributes within a model with a value set to True to another model?有没有办法将 model 中的所有属性设置为 True 到另一个 model? I am writing in Python and have the following:我正在写 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()

Then a separate cron is run: and each crime story is added to Story with their location.然后运行一个单独的 cron:每个犯罪故事都被添加到 Story 及其位置。 But the stories are not appearing in the Story model?!但是故事没有出现在故事 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.我有 2 个模型:文章和故事。 All articles are stored in the article model and any article with crime = True is set to be in the Story model.所有文章都存储在文章 model 中,并且任何带有 crime = True 的文章都设置为故事 model 中。 For some reason it is not moving the stories.出于某种原因,它没有移动故事。 The cron is running and not having any log errors. cron 正在运行并且没有任何日志错误。 Can I do this task in my dashboard ?我可以在我的仪表板中执行此任务吗? I have queried both models:我已经查询了两个模型:

SELECT * FROM Article ORDER BY date DESC SELECT * FROM Article 按日期顺序排列

The Article model has stories from todays date (May 2nd)文章 model 有今天(5 月 2 日)的故事

Story has articles from April 19th and no more since then.故事从 4 月 19 日开始有文章,此后不再有文章。 Can I query the models and say move all entities with crime set to true to the Story model?我可以查询模型并说将所有犯罪设置为 true 的实体移动到 Story model 吗?

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.由于我们已经确定没有错误,我的建议是在更高的位置添加更多的 logging.info 调用,以查看哪个 if 语句的评估结果为 false,或者哪个 for 循环正在迭代一个空集。 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.听起来您并没有确定您的两个 cron 作业中的哪一个有问题。

More fundamentally, I think you should re-consider the basic design of this task.更根本的是,我认为你应该重新考虑这个任务的基本设计。 Let's classify it as 3 steps:让我们将其分类为 3 个步骤:

  1. User creates a new Article entity with a title and body用户创建具有标题和正文的新文章实体
  2. If the body contains certain keywords, flag it as containing a crime.如果正文包含某些关键字,请将其标记为包含犯罪。
  3. If it contains a crime, create a corresponding Story entity.如果它包含犯罪,则创建相应的 Story 实体。

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.除了使操作复杂之外,您的第二个 cron 作业在规模上也效率低下。 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.在任务参数中传递实体键,这样您就不必查询任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM