Python之单例模式于几个常用模块

# 构造方法__new__()用于创建一个对象
# class A:
#     def __init__(self):
#         print("in __init__ function")
#     def __new__(self,*args,**kwargs):
#         print("in __new__ function")
#         return object.__new__(A,*args,**kwargs)
#     def show(self):
#         print("I am A")
# a = A()
# a1 = A()
# a2 = A()
# '''
# 输出
# in __new__ function  -- 先由__new__()生成一个对象
# in __init__ function
# '''
# a.show()
# print(id(a),id(a1),id(a2)) # 2127545927776 2127545927720 2127545929512 内存地址不同,不是同一个对象

# 单例模式 -- 一次执行过成中只生成一个类
# class A:
#     __istance = None
#     def __new__(cls, *args, **kwargs):
#         if not cls.__istance:
#             cls.__istance = object.__new__(cls)
#         return cls.__istance
#     def __init__(self,arg1):
#         self.arg1 = arg1
# a1 = A('a1')
# a2 = A('a2')
# a3 = A('a3')
# print(id(a1),id(a2),id(a3)) # 2303327178424 2303327178424 2303327178424
# print(a1.arg1,a2.arg1,a3.arg1) # a3 a3 a3

# from collections import namedtuple
# Card = namedtuple('Card',['rank','suit'])
#
# class FranchDeck:
#     _ranks = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
#     _suits = ['黑桃','红心','方块','梅花']
#     def __init__(self):
#         self._cards = [Card(rank,suit) for rank in FranchDeck._ranks for suit in FranchDeck._suits]
#     def __len__(self):
#         return len(self._cards)
#     def __getitem__(self, item): # 至此随机抽牌(像字典一样调用)
#         return self._cards[item]
#     def __setitem__(self, key, value): # 支持洗牌
#         self._cards[key] = value
# deck = FranchDeck()
# # print(deck[0])
#
# from random import choice
# print(choice(deck))
#
# from random import shuffle
# shuffle(deck)
# print(deck[:5])

# hashlib--摘要算法加密
# 可用作给密码加密,校验文件一次性
# import hashlib
# md5 = hashlib.md5()
# md5.update(bytes('123456',encoding='utf-8'))
# print(md5.hexdigest()) # e10adc3949ba59abbe56e057f20f883e
# 可以多次update一次记过获取,使用于文件一致性校验,读取每一行update最终获取一次结果
# md5.update(bytes('1',encoding='utf-8'))
# md5.update(bytes('2',encoding='utf-8'))
# md5.update(bytes('3',encoding='utf-8'))
# md5.update(bytes('4',encoding='utf-8'))
# md5.update(bytes('5',encoding='utf-8'))
# md5.update(bytes('6',encoding='utf-8'))
# print(md5.hexdigest()) # e10adc3949ba59abbe56e057f20f883e

# md5 加盐方式让加密更复杂
# import hashlib
# md5 = hashlib.md5(bytes('aaa',encoding='utf-8'))
# md5.update(bytes('123456',encoding='utf-8'))
# print(md5.hexdigest()) # 88316675d7882e3fdbe066000273842c
'''
除了MD5摘要算法,还有以下:
sha3_224
sha3_256
sha3_384
sha3_512
shake_128
shake_256
blake2b
blake2s
'''

# logging
# import logging
# 五个日志级别,默认输出比info级别高的
# logging.debug('debug message')
# logging.info('info message')
# logging.warning('warning message')
# logging.error('error message')
# logging.critical('critical message')

# basicconfig # 简单配置模式,文件编码问题不好处理
# import logging
# logging.basicConfig(
#     level=logging.DEBUG, # 设置日志级别
#     format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
#     datefmt='%a,%d %b %Y %H:%M:%S',
#     # filename='test.log', # 默认输出到标准输出
#     # filemode='w' # 默认追加模式
# )
#
# logging.debug('debug message')
# logging.info('info message')
# logging.warning('warning message')
# logging.error('error message')
# logging.critical('critical message')
'''
输出到屏幕正常
Fri,05 Apr 2019 15:49:26 单例模式与几个常用模块.py[line:111] DEBUG debug message
Fri,05 Apr 2019 15:49:26 单例模式与几个常用模块.py[line:112] INFO info message
Fri,05 Apr 2019 15:49:26 单例模式与几个常用模块.py[line:113] WARNING warning message
Fri,05 Apr 2019 15:49:26 单例模式与几个常用模块.py[line:114] ERROR error message
Fri,05 Apr 2019 15:49:26 单例模式与几个常用模块.py[line:115] CRITICAL critical message
写入文件乱码test.log
Fri,05 Apr 2019 15:44:10 ����ģʽ�뼸������ģ��.py[line:111] DEBUG debug message
Fri,05 Apr 2019 15:44:10 ����ģʽ�뼸������ģ��.py[line:112] INFO info message
Fri,05 Apr 2019 15:44:10 ����ģʽ�뼸������ģ��.py[line:113] WARNING warning message
Fri,05 Apr 2019 15:44:10 ����ģʽ�뼸������ģ��.py[line:114] ERROR error message
Fri,05 Apr 2019 15:44:10 ����ģʽ�뼸������ģ��.py[line:115] CRITICAL critical message
'''
# 配置log对象的模式
# import logging
# log = logging.getLogger() # 实例化一个日志操作对象
# fh = logging.FileHandler('test1.log',encoding='utf-8') # 创建一个文件操作符,日志写入文件
# sh = logging.StreamHandler() # 创建一个文件描述符,输出到标准输出
# fm = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s') # 创建一个输出格式
#
# fh.setFormatter(fm) # 绑定输出格式
# sh.setFormatter(fm) # 绑定输出格式
#
# fh.setLevel(logging.DEBUG) # 设置记录到文件的日志级别
# sh.setLevel(logging.WARN) # 设置输出到屏幕的日志级别
#
# log.addHandler(fh) # 绑定文件操作符
# log.addHandler(sh) # 绑定标准输出操作符
#
# logging.debug('debug message')
# logging.info('info message')
# logging.warning('warning message')
# logging.error('error message')
# logging.critical('critical message')

'''
屏幕输出:
2019-04-05 16:07:38,898 单例模式与几个常用模块.py[line:148] WARNING warning message
2019-04-05 16:07:38,899 单例模式与几个常用模块.py[line:149] ERROR error message
2019-04-05 16:07:38,899 单例模式与几个常用模块.py[line:150] CRITICAL critical message
test1.log文件:
2019-04-05 16:07:10,668 单例模式与几个常用模块.py[line:148] WARNING warning message
2019-04-05 16:07:10,677 单例模式与几个常用模块.py[line:149] ERROR error message
2019-04-05 16:07:10,677 单例模式与几个常用模块.py[line:150] CRITICAL critical message
2019-04-05 16:07:38,898 单例模式与几个常用模块.py[line:148] WARNING warning message
2019-04-05 16:07:38,899 单例模式与几个常用模块.py[line:149] ERROR error message
2019-04-05 16:07:38,899 单例模式与几个常用模块.py[line:150] CRITICAL critical message
'''

'''
format格式可用参数:
%(name)s  Logger的名字
%(levelno)s 数字形式的日志级别
%(levelname)s 文本形式的日志级别
%(pathname)s 调用日志输出函数的模块的完整路径
%(filename)s 调用日志输出函数的模块的文件名
%(module)s 调用日志输出函数的模块名
%(funcName)s 调用日志输出函数的函数名
%(lineno)d 调用日志输出函数的语句所在的代码行
%(created)f 当前时间,用UNIX标准的表示时间的浮点数表示
%(relativeCreated)d 自Log对象创建以来到输出日志信息时的毫秒数
%(asctime)s 字符串形式的当前时间
%(thread)d 线程ID,可能没有
%(threadName)s 线程名,可能没有
%(process)d 进程ID,可能没有
%(message)s 日志信息
'''

# 配置log对象的模式--记录不同级别的日志
# import logging
# fhlog = logging.getLogger(name='oracle') # 实例化一个日志操作对象
# fhlog.setLevel(logging.DEBUG) # 设置记录到文件的日志级别
#
# shlog = logging.getLogger(name='mysql') # 实例化一个日志操作对象
# shlog.setLevel(logging.WARN) # 设置输出到屏幕的日志级别
#
# fh = logging.FileHandler('test2.log',encoding='utf-8') # 创建一个文件操作符,日志写入文件
# sh = logging.StreamHandler() # 创建一个文件描述符,输出到标准输出
# fm = logging.Formatter('%(asctime)s %(name)s[line:%(lineno)d] %(levelname)s %(message)s') # 创建一个输出格式
#
# fh.setFormatter(fm) # 绑定输出格式
# sh.setFormatter(fm) # 绑定输出格式
#
#
# fhlog.addHandler(fh) # 绑定文件操作符
# fhlog.addHandler(sh) # 绑定文件操作符
#
# shlog.addHandler(fh) # 绑定标准输出操作符
# shlog.addHandler(sh) # 绑定标准输出操作符
#
# fhlog.debug('debug message')
# fhlog.info('info message')
# fhlog.warning('warning message')
# fhlog.error('error message')
# fhlog.critical('critical message')
#
# shlog.debug('debug message')
# shlog.info('info message')
# shlog.warning('warning message')
# shlog.error('error message')
# shlog.critical('critical message')

'''
oracle用户输出debug级别,mysql输出warning级别
2019-04-05 16:47:56,522 oracle[line:207] DEBUG debug message
2019-04-05 16:47:56,522 oracle[line:208] INFO info message
2019-04-05 16:47:56,522 oracle[line:209] WARNING warning message
2019-04-05 16:47:56,522 oracle[line:210] ERROR error message
2019-04-05 16:47:56,523 oracle[line:211] CRITICAL critical message
2019-04-05 16:47:56,523 mysql[line:215] WARNING warning message
2019-04-05 16:47:56,523 mysql[line:216] ERROR error message
2019-04-05 16:47:56,523 mysql[line:217] CRITICAL critical message
'''

# 日志切割
# import logging
# import logging.handlers
# log = logging.getLogger(name='mysql')
# log.setLevel(logging.DEBUG)
# fh = logging.handlers.RotatingFileHandler('new_test.log',maxBytes=2048,backupCount=9,encoding='utf-8') # 指定日志最大大小,最大备份数
# fm = logging.Formatter('%(asctime)s %(name)s %(process)d [%(threadName)s] %(levelname)s : %(message)s')
# 
# fh.setFormatter(fm)
# log.addHandler(fh)
# 
# log.debug("我是debug报信息")
# log.info("我是info报信息")
# log.warning("我是warning报信息")
# log.error("我是error报信息")
# log.critical("我是critical报信息")

猜你喜欢

转载自blog.csdn.net/qq_40199698/article/details/89045408