【问题标题】:How do I create a dynamic list of variables for a GAE datastore query?如何为 GAE 数据存储查询创建动态变量列表?
【发布时间】:2013-06-05 23:10:18
【问题描述】:

我正在使用 GAE 数据存储来处理滚动注册研究的电子邮件作业。每行(在 emailJobs 中)代表一个参与者。我要查询 12 个变量。每一个代表一个随访期(从 1 到 12)。变量保存了在特定后续期间向某人发送电子邮件的日期。由于人们在不同时间开始研究,因此并非每个人在任何给定时间都会处于相同的随访期。

我想确定处于特定跟进期的人,看看他们是否收到了他们在研究中本应收到的所有跟进电子邮件。

我试图通过只关注感兴趣的变量并忽略绝对是 None 类型的变量来减少查询数据存储所需的时间。

我试图解决这个问题的方法是创建一个动态变量列表,我将在我的查询中定位这些变量。

fu1_email_sent, fu2_email_sent, ...fu12_email_sent

这是代码的删减版本:

class emailJobs(db.Model):
    """ Models a list of email jobs for each participant """

    date_modified = db.DateTimeProperty(auto_now_add=True, name='Date modified')
    author = db.StringProperty() # Google account which made changes (for testing)

    triggerid = db.StringProperty()  # TriggerResponseID (connects participants across both panels)
    recipientid_po = db.StringProperty() # RecipientID - Original Panel
    recipientid_pdi = db.StringProperty() # RecipientID - De-identified Panel

    unsubscribed = db.IntegerProperty() # Subscription status 

    recipientlang = db.StringProperty()  # RecipientLanguage
    consent_date = db.DateTimeProperty() # CONSENTDATE datetime (timestamp)
    test = db.StringProperty() # Identifies test data (TESTDATA = 1)
    fuperiod = db.IntegerProperty() #Follow-up period (0 - 13: 0-before follow-up, 13-past fu12)

    last_fu_sent = db.DateTimeProperty() # Datetime last follow-up email was sent
    fu1_email_sent = db.DateTimeProperty() # ****************
    fu2_email_sent = db.DateTimeProperty()
    fu3_email_sent = db.DateTimeProperty()
    fu4_email_sent = db.DateTimeProperty()
    fu5_email_sent = db.DateTimeProperty()
    fu6_email_sent = db.DateTimeProperty()
    fu7_email_sent = db.DateTimeProperty()
    fu8_email_sent = db.DateTimeProperty()
    fu9_email_sent = db.DateTimeProperty()
    fu10_email_sent = db.DateTimeProperty()
    fu11_email_sent = db.DateTimeProperty()
    fu12_email_sent = db.DateTimeProperty()



class sendMissedFU(webapp2.RequestHandler):
    """ Queries Datastore for follow-up dates that were missed, then adds them to a task queue so that an email can be sent out """
    def get(self):

        now = datetime.datetime.now()
        now_dt = now.date() #today's date to compare with follow-up dates

        q = emailJobs.all()
        q.filter('consent_date !=', None ) # weeds out anyone who doesn't have a consent date
        q.order('consent_date')

        for part in q:

            trigid = part.triggerid
            lang = part.recipientlang
            guid = part.recipientid_po

            consent = part.consent_date
            consent_date = consent.date()
            calcdTime = now_dt - consent_date
            dayssince = calcdTime.days
            fup = dayssince/30


            fuemailsent_dict = {}
            fuemailsent_list = []

            for i in range(1, fup + 1):
                i = str(i)
                followup = 'fu' + i + '_email_sent'

                fuemailsent_list.append(followup)

                fu_check = part.followup

            if fu_check == None:
                fuemailsent_dict[followup] = 0  
            else:
                fuemailsent_dict[followup] = 1

我得到的错误是:

AttributeError: 'emailJobs' 对象没有属性 'followup'

不喜欢:fu_check = part.followup ...但我不知道该怎么做。

【问题讨论】:

  • 我们无法从您提供的代码中分辨出来。您应该包含 emailJobs 的模型。 emailJobs 有这样的属性吗?
  • 所以看看你的代码,emailJobs 没有属性followup 那么这个fu_check = part.followup 应该如何工作?这就是异常告诉你的。
  • 现在我明白要做什么了,你应该尝试做fu_check = getattr(part,followup)
  • 谢谢!我试试看。
  • 成功了!再次感谢!

标签: google-app-engine python-2.7 error-handling google-cloud-datastore


【解决方案1】:
 for i in range(1, fup + 1):
        i = str(i)
        followup = 'fu' + i + '_email_sent'

        fuemailsent_list.append(followup)

        fu_check = part.followup

表示您正在尝试访问名为“跟进”的属性。与线fu_check = part.followup。要使用存储在变量followup 中的名称,请使用getattr

fu_check = getattr(part,followup)

【讨论】:

    猜你喜欢
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-12
    相关资源
    最近更新 更多