下面的代碼有效,并且完全按照它應該做的,我將它提交給我的老板審查,他喜歡它,但認為它可以更干凈,并建議我應該將此代碼包裝在一個輔助函式中,然后傳入不同的引數。作為編程和 Python 的新手,我了解 OOP,但我對這個目標有點困惑。這是代碼:
供參考(這只是偽造的示例資料,請忽略電話號碼和電子郵件,哈哈):
notify_users = [
{'Name': 'Timmy Turner', 'Number':'3245234523', 'Email':'[email protected]','Days': 15},
{'Name': 'Jimmy Fallon', 'Number':'3245234523', 'Email':'[email protected]','Days': 30}
]
expired_users = [
{'Name': 'James Issac', 'Number':'3245234523', 'Email':'[email protected]','Days': -4},
{'Name': 'Roger Lewis', 'Number':'3245234523', 'Email':'[email protected]','Days': -5}
]
msg = "Hello %s, %s. \n \n***To change your password, press CTRL ALT DEL and select change password***.\n\n\n- MY COMPANY IT"
def send_notifications():
# 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=msg % (d['Name'], f"you have {d['Days']} days left to reset your password."))
print(message.sid)
# Send emails
requests.post(
self.app.config.get("https://api.mailgun.net/v3/MYDOMAIN/messages"),
self.app.config.get(auth=("api", mailgun_api)),
self.app.config.get(data={"from": "IT <mailgun@MYDOMAIN>",
"to": d['Email'],
"subject": "Password Reminder",
"text": msg % (d['Name'], f"you have {d['Days']} days left to reset your password.")}))
# Send notifications for expired users
# Send SMS
for d in expired_users:
message = client.messages.create(
to=d['Number'],
from_=twilio_from_phone_number,
body=msg % (d['Name'], "your password has expired, please change it asap to avoid any issues that could occur with your account"))
print(message.sid)
# Send expired emails
requests.post(
self.app.config.get("https://api.mailgun.net/v3/MYDOMAIN/messages"),
self.app.config.get(auth=("api", mailgun_api)),
self.app.config.get(data={"from": "IT <mailgun@MYDOMAIN>",
"to": d['Email'],
"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")}))
# Determine if lists contain users
send_notifications()
我完全理解他來自哪里,因為我基本上是在重復相同的代碼兩次。我在想的是某種可以包裝這段代碼的輔助函式,所以我可以只傳遞引數, def notify(user_list, msg_template)但我不確定如何真正實作它。
這基本上就是我想要完成的,2 個呼叫來執行過期的邏輯并通知用戶:
notify(notify_users, notify_msg)
notify(expired_users, expired_msg)
如果有人能提供一些關于我如何去做的提示或見解,我將不勝感激。謝謝!
uj5u.com熱心網友回復:
我認為這大致是您想要的——因為沒有 MRE,我實際上無法運行此代碼來驗證它是否有效。對于這種型別的重構,您真正需要做的就是獲取您復制和粘貼的代碼,并制作它的單一版本,其中更改的部分是一個變數。
def send_notifications():
client = Client(twilio_account_sid, twilio_auth_token)
def notify(user_list, msg_template):
for d in user_list:
message = client.messages.create(
to=d['Number'],
from_=twilio_from_phone_number,
body=msg % (d['Name'], msg_template.format(**d))
)
print(message.sid)
# Send emails
requests.post(
self.app.config.get("https://api.mailgun.net/v3/MYDOMAIN/messages"),
self.app.config.get(auth=("api", mailgun_api)),
self.app.config.get(data={
"from": "IT <mailgun@MYDOMAIN>",
"to": d['Email'],
"subject": "Password Reminder",
"text": msg % (d['Name'], msg_template.format(**d))
})
)
# Send warnings for users who are about to expire
notify(notify_users, "you have {Days} days left to reset your password.")
# Send notifications for expired users
notify(expired_users, "your password has expired, please change it asap to avoid any issues that could occur with your account")
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/350740.html
下一篇:呼叫后Python設定類變數
