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 ...
随机推荐
- 热点Key问题的发现与解决
热点问题概述 产生原因 热点问题产生的原因大致有以下两种: 用户消费的数据远大于生产的数据(热卖商品.热点新闻.热点评论.明星直播). 在日常工作生活中一些突发的的事件,例如:双十一期间某些热门商品的 ...
- libevent笔记5:水位watermarks
bufferevent中提供了对读写回调的触发条件及最大缓存长度的设置,即低高水位: 低水位:是读写回调函数的最低触发数据长度,当输入/输出缓存区中的数据长度小于低水位时,读/写回调函数不会被触发: ...
- pytest 学习笔记一 入门篇
前言 之前做自动化测试的时候,用的测试框架为Python自带的unittest框架,随着工作的深入,发现了另外一个框架就是pytest (官方地址文档http://www.pytest.org/en/ ...
- Kafka Offset Monitor页面显示空白
下载包:https://github.com/Morningstar/kafka-offset-monitor.git 解决:jar包内\KafkaOffsetMonitor-assembly-0.2 ...
- EmotiW 历年竞赛冠军数据及论文信息
date Baseline paper Champion paper Model & Feature Test_acc 2013 [1] [2] EmoNets 41.03 2014 [3] ...
- Excel VBA业余爱好者心得及总结
我不是IT专业人士,而是一位医务工作者,当初学习Excel VBA时,是因为读研究生时的课题需要:实验仪器每天产生4个Word文件,每个文件有9个表格,总计近百个数据.为了对这些数据进行统计分析,则需 ...
- 递推 + 高精度 --- Tiling
Tiling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7264 Accepted: 3528 Descriptio ...
- requests获取响应时间(elapsed)与超时(timeout)、小数四舍五入
前言 requests发请求时,接口的响应时间,也是我们需要关注的一个点,如果响应时间太长,也是不合理的.如果服务端没及时响应,也不能一直等着,可以设置一个timeout超时的时间 elapsed官方 ...
- 【题解】有限制的排列 [51nod1296]
[题解]有限制的排列 [51nod1296] 传送门:有限制的排列 \([51nod1296]\) [题目描述] 给出 \(n,m_1,m_2\) 和 \(\{a[1],a[2]...a[m_1]\} ...
- 100道MySQL常见面试题总结
原文链接:https://juejin.im/post/5d351303f265da1bd30596f9 前言 本文主要受众为开发人员,所以不涉及到MySQL的服务部署等操作,且内容较多,大家准备好耐 ...