''' 1、new方法和init方法执行的执行顺序? 答:先执行new方法,开辟内存,创建对象,再执行init ''' # class Person(): # def __init__(self, name, age): # self.name = name # self.age = age # print("我是init方法.") # # def __new__(cls, *args, **kwargs): # print("我是new方法.") # return object.__new__(cls) # # p = Person("lily", 18) # p2 = Person("lucy", 20) # print(id(p), id(p2)) ''' 2、call方法在什么时候被调用? 答:当对象()的时候被调用 ''' # class Person(): # def __init__(self): # print("我是init方法.") # # def __call__(self): # print("我是call方法.") # # p = Person() # p() '''' 3、请用写一个类,用反射为这个类添加一个静态属性 ''' # class Person(): # pass # # setattr(Person, "age", 30) # print(Person.age) ''' 4、请用反射为上题的类的对象添加一个属性name,值为你的名字 ''' # class Person(): # pass # # p = Person() # setattr(Person, "age", 30) # setattr(p, "name", "肖岩岩") # print(Person.age) # print(p.name) ''' 5、请使用new方法实现一个单例模式 ''' # class Singleton(): # __instance = None # 保存对象 # # def __init__(self): # print("我是init方法.") # # def __new__(cls, *args, **kwargs): # if not Singleton.__instance: # 判断是否不为None # Singleton.__instance = object.__new__(cls) # 创建对象并更新类变量 # return Singleton.__instance # # s = Singleton() # s1 = Singleton() # print(id(s), id(s1)) ''' 6、校验两个文件的一致性 ''' # import hashlib # # def get_file_md5(file): # """返回文件内容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("两个文件的内容一致.") # else: # print("两个文件的内容不一致.") ''' 7、加盐的密文登陆 ''' # import hashlib # # username = "" # password = "" # # class Auth(): # __salt = "谁言寸草心,报得三春晖." # 盐字符串 # # def get_md5_str(self, str1): # """md5加密""" # md5_obj = hashlib.md5(self.__salt.encode("utf-8")) # md5_obj.update(str1.encode("utf-8")) # return md5_obj.hexdigest() # # def register(self): # """用户注册""" # while 1: # name = input("Register Username: ").strip() # pwd = input("Register Password: ").strip() # if name != "" and pwd != "": # global username, password # username = name # password = self.get_md5_str(pwd) # print("[%s]注册成功" % username) # return True # else: # print("用户名和密码不能为空.") # # def login(self): # """用户登录""" # while 1: # name = input("Login Username: ").strip() # if name == "": # print("用户名不能为空.") # continue # pwd = input("Login Password: ").strip() # if pwd == "": # print("密码不能为空.") # continue # # if username == name and password == self.get_md5_str(pwd): # print("[%s]登录成功." % name) # return True # else: # print("用户名或密码错误.") # return False # # # auth = Auth() # 创建一个对象 # auth.register() # 注册 # auth.login() # 登录 ''' 8、完成一个既可以向文件输出又可以向屏幕输出的日志设置 ''' import logging from logging import handlers class Logger(object): level_relations = { 'debug': logging.DEBUG, 'info': logging.INFO, 'warning': logging.WARNING, 'error': logging.ERROR, 'crit': logging.CRITICAL }#日志级别关系映射 def __init__(self, filename, level='info', when='D', backCount=3, fmt='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'): self.logger = logging.getLogger(filename) format_str = logging.Formatter(fmt) # 设置日志格式 self.logger.setLevel(self.level_relations.get(level)) # 设置日志级别 sh = logging.StreamHandler() # 往屏幕上输出 sh.setFormatter(format_str) # 设置屏幕上显示的格式 # 往文件里写入#指定间隔时间自动生成文件的处理器 th = handlers.TimedRotatingFileHandler(filename=filename, when=when, backupCount=backCount, encoding='utf-8') th.setFormatter(format_str) # 设置文件里写入的格式 self.logger.addHandler(sh) # 把对象加到logger里 self.logger.addHandler(th) if __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')