【发布时间】:2016-12-13 17:42:03
【问题描述】:
我知道这很笨拙,我是初学者,但有没有更好的方法来做到这一点,我觉得有,但我不知道。我正在尝试做的是,只要分数变化高于 9,就发送电子邮件提醒。
代码如下:
#!/usr/bin/env python
"""
Script to send any pending alerts via email
"""
import sys
from pymongo import MongoClient
from datetime import datetime
#### begin
def send_email():
print "Begin GAMER-ALERT-SEND-EMAILS %s " % (datetime.now()).
if 'score_by_cat' >=9:
send_email()
if 'score_by_cat' >=9:
print( "Send Email")
else:
print('Nothing")
print "Begin GAMER-ALERT-SEND-EMAILS %s " % (datetime.now())
client = MongoClient('mongodb://localhost:28057/')
db = client.factor
email_rcpt_list = ['example@momo.com']
['example2@momo.com']
#process the alert.. if it is high enough risk and the makes sense (not a security control etc)
for alert in db.risk_alerts.find({"$and" : [{'sent': False}]}):
if float(alert['metric'])>=9 and alert['category']!='security control':
#Call email send here
for email in email_rcpt_list:
print 'Call email and pass in the email address from email_rcpt_list'
print ' Sample alert: Vendor: %s Has an new item %s on %s of elevated risk %s in the category %s link: https://Gamer-dev.momo.net/profile/view/detail/%s' % (alert['vendor_name'],alert['key'],alert['source'],alert['metric'],alert['category'],alert['profile_id'])
#mark as sent always
db.risk_alerts.update_one({"_id":alert['_id']},{"$set": {"sent":True}})
print "End GAMER-ALERT-SEND-EMAILS %s" % (datetime.now())
【问题讨论】:
-
你确定这个脚本一开始就可以工作吗?
if 'score_by_cat' >=9不带冒号:? -
是的,这里似乎存在格式问题。而在python中,格式化很重要,所以我们需要看到它的格式是否正确。
-
好的,很抱歉我会编辑它
-
我的个人风格总是将大量重复分解,例如使用
.format( *( alert(x) for x in ("vendor_name","source","key",...) ))的alerts。格式特定的你也可以"...{0[vendor_name]} ... {0[source]} ... ".format(alert)。不过,这可能与问题无关。 -
@jon 另外,如果您已经有了想要改进的可行代码,您可能需要考虑code review
标签: python email scripting alerts