Python implements the interface class in two ways + email reminder + Dynamic Import Module + reflection (refer to Django middleware source code)

Source: Internet
Author: User

Two ways to implement an interface class
 from Import Abcmeta  from Import Abstractmethod class Basemessage (metaclass=abcmeta):    @abstractmethod    def  Send (Self,subject,body,to, Name):        Pass

Way Two
class Basemessage (object):     def Send (self, subject, body, to, name):         Raise Notimplementederror (' not implemented Send method ')

Email alerts
ImportSmtplib fromEmail.mime.textImportMimetext fromEmail.utilsImportformataddr from. baseImportBasemessageclassEmail (basemessage):def __init__(self): Self.email="Sender's email address"Self.user="Sender's name"self.pwd=' mailbox password or SMTP authorization code'    defSend (self,subject,body,to,name):" ":p Aram Subject: Subject:p Aram Body: Content:p Aram to: Recipient:p Aram Name: Recipient's name: return: " "msg= Mimetext (Body,'Plain','Utf-8')#Send contentmsg[' from'] = FORMATADDR ([Self.user,self.email])#Sendermsg[' to'] = FORMATADDR ([name, to])#Recipientmsg['Subject'] = subject#ThemeServer= Smtplib. SMTP ("smtp.126.com", 25)#SMTP ServiceServer.login (Self.email, Self.pwd)#mailbox user name and passwordServer.sendmail (Self.email, [to,], msg.as_string ())#Sender and receiverServer.quit ()

Dynamic Import Module + reflection

Settings

# ################## Configuration of Custom message reminders ################### message_classes = [    'utils.message.email.Email',    ' UTILS.MESSAGE.MSG.MSG ' ,     ' Utils.message.wx.WeChat ' ,     ' utils.message.dingding.DingDing ' ,]

Importimportlib fromDjango.confImportSettingsdefsend_message (to,name,subject,body):"""SMS, email,:p Aram to: Recipient:p Aram Name: Recipient name:p Aram Subject: Subject:p Aram Body: Content: return:"""     forCls_pathinchsettings. Message_classes:#Cls_path is a stringModule_path,class_name = Cls_path.rsplit ('.', Maxsplit=1) M=importlib.import_module (module_path) obj=GetAttr (M,class_name) () obj.send (Subject,body,to,name,)

Python implements the interface class in two ways + email reminder + Dynamic Import Module + reflection (refer to Django middleware source code)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.