python3 day07 exercises

'' ' 
? 1, the execution order of new methods and init method performed 

A: The first implementation of new methods, open up the memory, create objects, then execute init 
' '' 
# class the Person (): 
#      DEF __init __ (Self, name, Age) : 
#          self.name = name 
#          self.age Age = 
#          Print ( "I am the init method.") 
# 
#      DEF __new __ (CLS, * args, ** kwargs): 
#          Print ( "I am the new method.") 
#          return Object .__ new new __ (CLS) 
# 
# P = the Person ( "Lily", 18 is) 
# P2 = the Person ( "Lucy", 20 is) 
# Print (ID (P), ID (P2)) 

'' ' 
2, Call method is invoked at what time? 

A:When the object () is called when 
'' ' 
#class the Person (): 
#      DEF __init __ (Self): 
#          Print ( "I am the init method.") 
# 
#      DEF __call __ (Self): 
#          Print ( "I call method.") 
# 
# the p-= the Person () 
# the p-() 

'' ' ' 
3, Please write a class, add a static attribute for the class with reflection 
'' ' 
# class the Person (): 
#      Pass 
# 
# setattr (the Person, "Age", 30) 
# Print ( person.age) 

'' ' 
4, add an attribute name for an object on the title of the class of reflection, is your name 
' '' 
# class the Person ():
#     pass
#
# p = Person()
#setattr (the Person, "Age", 30) 
# setattr (P, "name", "Xiao Yanyan") 
# Print (person.Age) 
# Print (p.name) 

'' ' 
. 5, using the new method to achieve a singleton 
'' ' 
# class the singleton (): 
#      __instance = None # storage target 
# 
#      DEF the __init __ (Self): 
#          Print (. "I init method") 
# 
#      DEF __new __ (CLS, * args, ** kwargs): 
#          iF not Singleton .__ instance: # is not to determine whether None 
#              Singleton instance = Object .__ .__ new new __ (CLS) # create objects and update class variables 
#          return Singleton.__instance
#
# s = Singleton()
# S1 = the Singleton () 
# Print (ID (S), ID (S1)) 

'' ' 
. 6, the consistency check two files 
' '' 
# Import hashlib 
# 
# DEF get_file_md5 (File): 
#      "" " returns the file contents of the string "" after the encrypted md5 " 
#      obj = hashlib.md5 () 
#      obj.update (Open (file) .read (). encode (" UTF-. 8 ")) 
#      return obj.hexdigest () 
# 
# IF get_file_md5 ( "file1.py") == get_file_md5 ( "file2.py"): 
#      Print ( "consistent with the contents of two files.") 
# the else: 
#      Print ( "the contents of two files are inconsistent." ) 

'' '
7, the ciphertext salt login 
'' ' 
# Import hashlib 
#
# Username = "" 
# password = "" 
# 
# class Auth (): 
#      __salt = "Who made the inch of grass, reported in the apartments." Salt string # 
# 
#      DEF get_md5_str (Self, str1): 
#          "" "md5 encryption" "" 
#          md5_obj = hashlib.md5 (Self .__ salt.encode ( "UTF-. 8")) 
#          md5_obj.update (str1.encode ( "UTF-. 8")) 
#          return md5_obj.hexdigest () 
# 
#      DEF the Register (Self): 
#          "" "user registration" "" 
#          the while 1: 
#              name = the INPUT ( "the Register the Username:").strip()
#             the INPUT = pwd ( "the Register Password:") .strip () 
#              ! IF name = "" and pwd =! "": 
#                  , Ltd. Free Join username, password 
#                  username = name 
#                  password = self.get_md5_str (pwd) 
#                  Print ( " [% s] successfully registered "% username) 
#                  return True 
#              the else: 
#                  Print (" user name and password can not be empty "). 
# 
#      DEF the Login (Self): 
#          " "" user login "" " 
#          the while 1: 
#              name = the INPUT ( "the Login the Username:").strip()
#             name == IF "": 
#                  Print (. "User name can not be null") 
#                  the Continue 
#              pwd = the INPUT ( "the Login Password:") .strip () 
#              IF pwd == "": 
#                  Print ( "password can not to empty ") 
#                  the Continue 
# 
#              IF username == == self.get_md5_str name and password (pwd): 
#                  Print (" [% S] successful login "% name). 
#                  return True 
#              the else: 
#                  Print (" wrong user name or password.")
#                 return False
#
#
#auth = Auth () # create an object 
# auth.register () # registration 
# auth.login () # login 

'' ' 
Log Settings 8, a complete and can either output to the screen to output file 
' '' 
Import logging
 from the logging Import handlers 

class Logger (Object): 
    level_relations = {
         ' Debug ' : logging.DEBUG,
         ' info ' : logging.info,
         ' warning ' : logging.WARNING,
         ' error ' : logging.error,
         ' Crit ': Logging.CRITICAL 
    } # log level mapping relationship 

    DEF  the __init__ (Self, filename, Level = ' info ' , When = ' D ' , backCount =. 3 , 
                 FMT = ' % (the asctime) S -% (pathname) S [Line: % (lineno) D] -% (levelname) S:% (Message) S ' ): 
        self.logger = logging.getLogger (filename) 
        format_str = logging.Formatter (FMT)   # set the log format 
        self.logger.setLevel (self .level_relations.get (level))   # set the log level 
        SH = logging.StreamHandler ()   #To output on the screen 
        sh.setFormatter (format_str)    # format on the setting screen 
        # to # file written at a specified interval automatically generated file processor 
        th = handlers.TimedRotatingFileHandler (filename = filename, when = when, backupCount = backCount , encoding = ' UTF-. 8 ' ) 
        th.setFormatter (format_str)   # formatting file write 
        self.logger.addHandler (SH)   # to the object to a logger in 
        self.logger.addHandler (TH) 


IF  the __name__ == ' __main__ ' : 
    log = Logger ( ' all.log ' , Level = ' Debug ' )
    log.logger.debug('debug')
    log.logger.info('info')
    log.logger.warning('警告')
    log.logger.error('报错')
    log.logger.critical('严重')
    Logger('error.log', level='error').logger.error('error')

 

Guess you like

Origin www.cnblogs.com/lilyxiaoyy/p/10929114.html