尝试使用辅助函数而不是使用重复代码来清理我的代码
Trying to clean up my code with helper function instead of using repeated code
下面的代码有效,并且完全按照它应该做的,我将它提交给我的老板审查,他喜欢它但认为它可以更简洁,并建议我应该将这段代码包装在一个辅助函数中并且然后传入不同的参数。作为编程和 python 的新手,我了解 OOP,但对此 objective 有点困惑。这是代码:
供参考(这只是假的示例数据,请忽略电话号码和电子邮件 lol):
notify_users = [
{'Name': 'Timmy Turner', 'Number':'3245234523', 'Email':'timmyt@gmail.com','Days': 15},
{'Name': 'Jimmy Fallon', 'Number':'3245234523', 'Email':'jimmyf@gmail.com','Days': 30}
]
expired_users = [
{'Name': 'James Issac', 'Number':'3245234523', 'Email':'jamesi@gmail.com','Days': -4},
{'Name': 'Roger Lewis', 'Number':'3245234523', 'Email':'rogerl@gmail.com','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)
如果有人可以提供一些提示或见解来帮助我解决这个问题,我将不胜感激。谢谢!
我认为这大致就是您想要的——因为没有 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")
下面的代码有效,并且完全按照它应该做的,我将它提交给我的老板审查,他喜欢它但认为它可以更简洁,并建议我应该将这段代码包装在一个辅助函数中并且然后传入不同的参数。作为编程和 python 的新手,我了解 OOP,但对此 objective 有点困惑。这是代码:
供参考(这只是假的示例数据,请忽略电话号码和电子邮件 lol):
notify_users = [
{'Name': 'Timmy Turner', 'Number':'3245234523', 'Email':'timmyt@gmail.com','Days': 15},
{'Name': 'Jimmy Fallon', 'Number':'3245234523', 'Email':'jimmyf@gmail.com','Days': 30}
]
expired_users = [
{'Name': 'James Issac', 'Number':'3245234523', 'Email':'jamesi@gmail.com','Days': -4},
{'Name': 'Roger Lewis', 'Number':'3245234523', 'Email':'rogerl@gmail.com','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)
如果有人可以提供一些提示或见解来帮助我解决这个问题,我将不胜感激。谢谢!
我认为这大致就是您想要的——因为没有 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")