Python's binding, encryption, and logging modules

Source: Internet
Author: User
Tags throw exception

First, what is constraint?

The constraint in life is probably the reason why you can't do it, call it a constraint. The constraint in Python is that when you need to use some method in multiple classes, you need to throw an exception or use base class + exception handling to constrain it.

1 classBasemessage:2     defSend (self):3         RaiseNotimplementederror ("send must be rewritten")4 #constraint unspoken rule, subclass must have send method or throw exception actively5 classMSG (basemessage):6     defSend (self):7         Print("Send SMS")8     Pass9 Ten  One deffunc (ARG): A arg.send () -obj =MSG () - func (obj) the  -  - constrained by abstract methods, abstract classes -  fromAbcImportAbcmeta,abstractmethod +  - classBase (metaclass=Abcmeta): +     defF1 (self): A         Print("3") at @abstractmethod -     defF2 (self): -         Pass - classFoo (Base): -     defF2 (self): -         Print(66) inobj =Foo () - obj.f1 () to obj.f2 () +  -When you need to use some methods in multiple classes, you need to use the base class +exception handling to constrain theSummary: Python can be used to throw exceptions and abstract classes + abstract methods to constrain

Second, the encryption mechanism

Our password only we know others do not know, but in Python the existence of the password is too exposed, we need a way to make the password complex can not be cracked

1 ImportHashlib#to import the encryption module first2 3obj = Hashlib.md5 (b"ASDAOIJSO")#add salt to prevent the collision of the library, resulting in password leakage4Obj.update ("Admin". Encode ("Utf-8"))#encode and write the bytes that will be encrypted5 6 #Get redaction7 Print(Obj.hexdigest ())

Third, logging module related

In our daily life, someone will write a diary to record what they have done every day, when we can not remember the time to turn over the notes to recall, and then in Python also has the function of "diary", that is the logging module.

1 Log Related2 ImportLogging3 ImportTraceback4 5Logger = Logging.basicconfig (filename="Log.txt",6format="% (asctime) s-% (name) s-% (levelname) s-% (module) s:% (message) s",7DATEFMT ="%y-%m-%d%h:%m:%s",8level=109                              )TenLogging.debug ("first-level exception") level=10 OneLogging.info ("Level Two exception") level=20 ALogging.warning ("Level Three exception") level=30 -Logging.error ("level Four Exception") level=40 -Logging.critical ("Highest Exception") level=50 the  - deffunc (): -     Try: -A = A+1 +     exceptException as E: -         Print(Logging.error (Traceback.format_exc ()))#gets the stack information for the current error +Func ()

Python's binding, encryption, and logging modules

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.