创建一个 msg 变量来代替重复多行字符串

Create a msg variable to use instead of repeating multiline strings

我的脚本中有几个函数当前使用多行字符串作为消息。

我的脚本是一个密码提醒器,用于检查用户是否在到期后的 30、15、3 天内,如果是,他们会收到短信和电子邮件提醒以更改密码。在这个函数中,我现在将正文部分作为多行字符串:

        def send_sms():
            client = Client(twilio_account_sid, twilio_auth_token)
            for d in notify_users:
                message = client.messages.create(
                    to=d['Number'],
                    from_=twilio_from_phone_number,
                    body=f"Hello {d['Name']}, you have {d['days']} days left to reset your password."
                         f"\n"
                         f"\n***To change your password, press CTRL + ALT + DEL and select change password***."
                         f"\n"
                         f"\n"
                         f"\n- MY COMPANY IT")
                print(message.sid)
            for d in expired_users:
                message = client.messages.create(
                    to=d['Number'],
                    from_=twilio_from_phone_number,
                    body=f"Hello {d['Name']}, your password has expired, please change it asap to avoid any issues that could occur with your account."
                         f"\n"
                         f"\n***To change your password, press CTRL + ALT + DEL and select change password***."
                         f"\n"
                         f"\n"
                         f"\n- MY COMPANY IT")
                print(message.sid)

基本上我想用 notify_msg 或 expired_msg 变量替换这些函数的主体部分

我试着在函数之前这样定义它:

notify_msg = (f"Hello {d['Name']}, you have {d['days']} days left to reset your password."
                     f"\n"
                     f"\n***To change your password, press CTRL + ALT + DEL and select change password***."
                     f"\n"
                     f"\n"
                     f"\n- MY COMPANY IT")
expired_msg = (f"Hello {d['Name']}, your password has expired, please change it asap to avoid any issues that could occur with your account."
                      f"\n"
                      f"\n***To change your password, press CTRL + ALT + DEL and select change password***."
                      f"\n"
                      f"\n"
                      f"\n- MY COMPANY IT")

但显然这行不通,因为没有定义“d”。我考虑过在 forloop 中执行此操作,但我希望相同的变量也能够在我的 send_email() 函数中使用。我该怎么做才能将其传入,这样我就不必多次重复相同的多行字符串?

电子邮件功能也是如此:

 #  Send email to users
        def send_emails():
            for d in notify_users:
                return requests.post(
                    "https://api.mailgun.net/v3/MYDOMAIN/messages", 
                    auth=("api", "MAILGUN_API_KEY"),
                    data={"from": "IT <mailgun@MYDOMAIN>",
                          "to": notify_list,
                          "subject": "Password Reminder",
                          "text": f"Hello {d['Name']}, you have {d['days']} days left to reset your password."
                                  f"\n"
                                  f"\n***To change your password, press CTRL + ALT + DEL and select change password***."
                                  f"\n"
                                  f"\n"
                                  f"\n- IT"})

        # Send email to expired users
        def send_exp_emails():
            for d in expired_users:
                return requests.post(
                    "https://api.mailgun.net/v3/MYDOMAIN/messages",
                    auth=("api", "MAILGUN_API_KEY"),
                    data={"from": "IT <mailgun@MYDOMAIN>",
                          "to": expired_list,
                          "subject": "Password Reminder",
                          "text": f"Hello {d['Name']}, your password has expired, please change it asap to avoid any issues that could occur with your account."
                                  f"\n"
                                  f"\n***To change your password, press CTRL + ALT + DEL and select change password***."
                                  f"\n"
                                  f"\n"
                                  f"\n- IT"})

如您所见,我在脚本中将相同的多行字符串重复了 4 次。 我可以做些什么来在正文中传递多行字符串变量而不是重复相同的行吗?谢谢大家

更新代码

users = [
{'Name': 'Timmy Turner', 'Number':'3245234523', 'Email':'timmyt@gmail.com','days': 15},    
{'Name': 'Jimmy Fallon', 'Number':'3245234523', 'Email':'jimmyfallon@gmail.com','days': 30}
]
expired_users = [
{'Name': 'Timmy Turner', 'Number':'3245234523', 'Email':'timmyt@gmail.com','days': -4},    
{'Name': 'Jimmy Fallon', 'Number':'3245234523', 'Email':'jimmyfallon@gmail.com','days': -5}
]

msg = "Hello % , %. \n \n***To change your password, press CTRL + ALT + DEL and select change password***.\n\n\n- MY COMPANY IT"

def send_email():
    for d in users:
        return requests.post(
            "https://api.mailgun.net/v3/MYDOMAIN/messages",
            auth=("api", "MAILGUN_API_KEY"),
            data={"from": "IT <mailgun@mail.MYDOMAIN.net>",
                "to": emails,
                "subject": "Password Reminder",
                "text": msg % (d['Name'], f"you have {d['days']} days left to reset your password.")})

def send_expired():
    for d in expired_users:
        return requests.post(
            "https://api.mailgun.net/v3/MYDOMAIN/messages",
            auth=("api", "MAILGUN_API_KEY"),
            data={"from": "IT <mailgun@mail.MYDOMAIN.net>",
                "to": expired,
                "subject": "Password Reminder",
                "text": msg% (d['Name'], "your password has expired, please change it asap to avoid any issues that could occur with your account")})

使用格式化字符串:

msg = "Hello %s, %s. \n \n***To change your password, press CTRL + ALT + DEL and select change password***.\n\n\n- MY COMPANY IT"
print(msg % ("Troy", "your password has expired, please change it asap to avoid any issues that could occur with your account"))

输出:

Hello Troy, your password has expired, please change it asap to avoid any issues that could occur with your account. 
 
***To change your password, press CTRL + ALT + DEL and select change password***.


- MY COMPANY IT

注意:不需要在分隔的字符串上放置换行符

这两条消息似乎具有相同的基本通用格式。您可以将其移出到通用模板中,该模板对所有消息类型都相同。只在 send_emailssend_exp_emails 函数中执行实际的 string-formatting/string-interpolation,当你拥有所有必要的 variables/data.

template = "Hello {}, {}" \
           "\n***To change your password, press CTRL + ALT + DEL and select change password***." \
           "\n\n\n- MY COMPANY IT"

name = "Bob"

def send_emails():
    remaining_days = 3
    message = "you have {} days left to reset your password.".format(remaining_days)
    print(template.format(name, message))

def send_exp_emails():
    message = "your password has expired, please change it asap to avoid any issues that could occur with your account."
    print(template.format(name, message))

send_emails()
send_exp_emails()