python 中根据python版本(2或3)定义函数
示意代码如下:
#_*_coding:UTF-8_*_ import time
import socket
import os
import sys if sys.version_info.major == 2:
reload(sys)
sys.setdefaultencoding('utf8') class LogLevel(object):
debug = 'DEBUG'
info = 'INFO'
warning = 'WARN'
error = 'ERROR'
fatal = 'FATAL' class Log4P(object):
def __init__(self, *args_arr, **args_dict):
self.module_name = "unknown"
self.task_id = "unknown"
if "module_name" in args_dict:
self.module_name = args_dict["module_name"]
if "task_id" in args_dict:
self.task_id = str(args_dict["task_id"])
self.host_name = socket.gethostname() def debug(self, *message):
return self.log(*message, level=LogLevel.debug) def info(self, *message):
return self.log(*message, level=LogLevel.info) def warn(self, *message):
return self.log(*message, level=LogLevel.warning) def error(self, *message):
return self.log(*message, level=LogLevel.error) def fatal(self, *message):
return self.log(*message, level=LogLevel.fatal) def static(self, case):
if case == 1:
info = "run success"
elif case == 2:
info = "run failed"
elif case == 3:
info = "no master"
elif case == 4:
info = "error master"
elif case == 5:
info = "run fail before switch master"
else:
info = "unknown error"
return self.info(info) if sys.version_info.major == 2:
def log(self, message, level=LogLevel.info):
curr_time = time.localtime(time.time())
log_context = format('[%d-%02d-%02d %02d:%02d:%02d][%s][%s][%s-%s]' \
% (curr_time.tm_year, curr_time.tm_mon, curr_time.tm_mday,
curr_time.tm_hour, curr_time.tm_min, curr_time.tm_sec,
str(level), self.host_name, self.module_name, self.task_id))
print(log_context, message, os.linesep)
else:
def log(self, *message, level=LogLevel.info):
curr_time = time.localtime(time.time())
log_context = format('[%d-%02d-%02d %02d:%02d:%02d][%s][%s][%s-%s]' \
% (curr_time.tm_year, curr_time.tm_mon, curr_time.tm_mday,
curr_time.tm_hour, curr_time.tm_min, curr_time.tm_sec,
str(level), self.host_name, self.module_name, self.task_id))
print(log_context, *message, os.linesep) if __name__ == "__main__":
logger = Log4P(module_name="dga", task_id="dga_id")
logger.log("hello world")
logger.log("hello world", "this is a test")
a = 100
logger.log("hello world", "var:", a)
python 中根据python版本(2或3)定义函数的更多相关文章
- python python中那些双下划线开头的那些函数都是干啥用用的
1.写在前面 今天遇到了__slots__,,所以我就想了解下python中那些双下划线开头的那些函数都是干啥用用的,翻到了下面这篇博客,看着很全面,我只了解其中的一部分,还不敢乱下定义. 其实如果足 ...
- 如何在Python中快速画图——使用Jupyter notebook的魔法函数(magic function)matplotlib inline
如何在Python中快速画图--使用Jupyter notebook的魔法函数(magic function)matplotlib inline 先展示一段相关的代码: #we test the ac ...
- python中while循环和for循环的定义和详细的使用方法
1. 循环的定义,反复做某事,具有明确的开始和结束. 2. 在Python中循环有while和for两种方式: While循环:1) 语法结构 >>> while 条件: ... ...
- python 中进制转换及format(),int()函数用法
python中数值型变量好像只能是十进制形式表示,其他类型变量只能以字符串形式存在,可以通过format函数将int类型变量转换成其他进制字符串,如下所示: v_code=15 # 2进制 x=for ...
- Python中倒转输入序列元素顺序的reversed函数
reversed函数将输入的序列的元素倒转后存储到一个类型为"reversed"可迭代对象,不能直接访问,可以转换为其他对象如列表或通过for循环方法访问. 注意:这里是倒转不是倒 ...
- python中的类的成员变量以及property函数
1 python类的各种变量 1.1 全局变量 在类外定义的变量. 1.2 类变量 定义在类里面,所有的函数外面的变量.这个变量只有一份,是所有的对象共有的.在类外用“类.”来引用. 1.3 实例变量 ...
- 记录一些Python中不常用但非常好用的函数
zfill(): 方法返回指定长度的字符串,原字符串右对齐,前面填充0. print('Helloworld'.zfill(50))0000000000000000000000000000000000 ...
- python中json文件处理涉及的四个函数json.dumps()和json.loads()、json.dump()和json.load()的区分
一.概念理解 1.json.dumps()和json.loads()是json格式处理函数(可以这么理解,json是字符串) (1)json.dumps()函数是将一个Python数据类型列表进行js ...
- python中获取python版本号的方法【转】
原文 python3 #!/usr/bin/python # 第1种方法 import platform print(platform.python_version()) >>> i ...
随机推荐
- [HeadFrist-HTMLCSS学习笔记]第五章认识媒体:给网页添加图像
[HeadFrist-HTMLCSS学习笔记]第五章认识媒体:给网页添加图像 干货 JPEG.PNG.GIF有何不同 JPEG适合连续色调图像,如照片:不支持透明度:不支持动画:有损格式 PNG适合单 ...
- 20165230田坤烨网络对抗免考报告_基于WIN10的渗透攻击
目录 简单信息收集 主机发现 ping nmap 端口扫描 nmap OS及服务版本探测 nmap -sV 绕过防火墙尝试 诱饵 随机数据长度 随机顺序扫描目标 MAC地址欺骗 实现win10的渗透攻 ...
- Eureka比Zookeeper好在哪里?
Eureka遵守AP,Zookeeper遵守CP RDBMS(oracle/mysql.sqlServer) ====> ACID, 关系型数据库遵循ACID原则: NoSQL(redis/mo ...
- centos下导出docx为html
yum -y install libreoffice.x86_64 libreoffice --invisible --convert-to html --outdir /root/demo_html ...
- idea 本地调试,修改代码,代码自动生效
修改1: 修改2: ctrl+shift+a 弹出页面中搜索“registry” 修改3:修改configerations
- 60 网络编程(二)——URL
认识URI.URL.URN 详细请参考:https://blog.51cto.com/xoyabc/1905492 URI:uniform resource Indent 统一资源标识符 URL:un ...
- 用这个模型去理解CPU?
- 2019 拉卡拉java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.拉卡拉等公司offer,岗位是Java后端开发,因为发展原因最终选择去了拉卡拉,入职一年时间了,也成为了面试官 ...
- Django--模型层进阶
目录 QuerySet对象 可切片 可迭代 惰性查询 缓存机制 何时查询集不会被缓存? exists()与iterator()方法 exists() iterator() 中介模型 查询优化 表数据 ...
- 第一次Git上传本地项目到github上 的命令
1.下载Git软件:https://git-scm.com/downloads, 2.下载之后安装就很简单了, 3.邮箱注册 在git bash界面输入如下内容即可完成邮箱的注册: $ git con ...