python 中根据python版本(2或3)定义函数

Posted bonelee

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 ❀ 类

Python ❀ 类

Mac Python多版本共存

python学习--如何在列表字典集合中根据条件筛选数据

python基础2018.12.3