Home > Mobile >  Create a msg variable to use instead of repeating multiline strings
Create a msg variable to use instead of repeating multiline strings

Time:10-30

I have several functions in my script that use multiline strings for messages currently.

My script is a password reminder that checks if a user is within 30, 15, 3 days from expiration, if they are they are sent sms and email reminders to change their password. In this function I have the body section as a multiline string at the moment:

        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)

basically I want to replace the body sections of these functions with a notify_msg or expired_msg variable

I tried to define this before the function like so:

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")

but obviously this isn't going to work because "d" isn't defined. I thought about doing this within the forloop, but I want the same variables to be able to be used in my send_email() functions as well. What could I do to pass this in, so I don't have to repeat the same multiline strings several times?

Here is the same thing for the email function:

 #  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"})

As you see, I am repeating the same multiline strings 4 times in my script. Is there anything I can do to pass multiline string variables in the body instead of repeating the same lines? Thanks all.

CodePudding user response:

Use formatted strings:

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"))

Output:

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

Note: no need to put line breaks on separated strings

CodePudding user response:

The two messages seem to have the same underlying general format. You can move that out into a generic template which is the same for all message types. Only perform the actual string-formatting/string-interpolation in the send_emails and send_exp_emails functions, when you have all necessary 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()
  • Related