学习内容
1.configparser 处理配置文件的模块
2.login 登陆
log 日志——记录用户行为或者代码的执行过程
logging——1.“一键”控制
2.排错时候打印细节
3.严重的错误记录下来
4.一些用户行为,有没有错都要记录
3.log对象配置
1.程序充分解耦
2.程序高可定制
4.logging
有5种级别的日志记录模式
两种配置方式:basicconfig log对象
代码区
1.configparper logging模块
# configparser 模块 ''' import configparser config = configparser.ConfigParser() config['DEFAULT'] = {'one': '1'} config['address'] = {'two':'2'} with open('example.ini','w') as f: config.write(f) config.read('example.ini') print(config.sections()) print(config['address']['one']) for key in config['address']: print(key) print(config.items('address')) ''' # logging 模块 ''' import logging logging.debug('1') logging.info('2') logging.warning('3') logging.error('4') logging.critical('5') import logging logger = logging.getLogger() fh = logging.FileHandler('log.log',encoding='utf-8') sh = logging.StreamHandler() formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) # sh.setFormatter(formatter) logger.addHandler(fh) logger.addHandler(sh) logging.debug('一') logging.info('二') logging.warning('三') logging.error('四') logging.critical('五')
2.1234组成无重复的三位数
def comb(): count = 0 for i in range(1,5): for j in range(1,5): for k in range(1,5): if i == j or i == k or j == k: continue count += 1 yield str(i)+str(j)+str(k), count for i,j in comb(): print(i,j)
3.实例化10各类,找出年纪最大的
class Person: def __init__(self,name,age): self.name = name self.age = age l = [] for i in range(10): l.append(Person('alex'+str(i),i+10)) ret = max(l,key=lambda obj:obj.age) print(ret.age)
4.CS
class Person: def __init__(self,name, blood, weapon, gender): self.name = name self.blood = blood self.weapon = weapon self.gender = gender def attack(self, obj): obj.blood -= self.weapon class Police(Person): def __init__(self,name,blood,weapon,gender,role): self.role = role super().__init__(name,blood,weapon,gender) class Terrorist(Person): def __init__(self, name, blood, weapon, gender, role): self.role = role super().__init__(name, blood, weapon, gender) a = Police('alex',100,1,'man','police') b = Terrorist('alex',100,10,'man','police') a.attack(b) print(b.blood)