python 类的继承

 

XNginxLog.py 注意:Python2 父类 XNginxLog(object) 必须要加object

import datetime, time
import urlparse
import re


class XNginxLog(object):
    """
    """
    __IP = r"?P<ip>[\d.]*"
    __DATE = r"?P<date>\d+"
    __MONTH = r"?P<month>\w+"
    __YEAR = r"?P<year>\d+"
    __LOG_TIME = r"?P<time>\S+"
    __METHOD = r"?P<method>\S+"
    __REQUEST = r"?P<request>\S+"
    __STATUS = r"?P<status>\d+"
    __BODY_BYTES_SENT = r"?P<bodyBytesSent>\d+"
    __REFER = r"""?P<refer>
         [^\"]*
         """
    __USER_AGENT = r"""?P<userAgent>
          .*
          """
    __PATTERN = re.compile(r'(%s)\ -\ -\ \[(%s)/(%s)/(%s)\:(%s)\ [\S]+\]\ \"(%s)?[\s]?(%s)?.*?\"\ (%s)\ (%s)\ \"(%s)\"\ \"(%s).*?\"' % (
        __IP, __DATE, __MONTH, __YEAR, __LOG_TIME, __METHOD, __REQUEST, __STATUS, __BODY_BYTES_SENT, __REFER, __USER_AGENT), re.VERBOSE)

    ip = None
    date = None
    month = None
    year = None
    log_time = None
    method = None
    request = None
    status = None
    bodyBytesSent = None
    refer = None
    userAgent = None

    def __init__(self, p_line=None):
        arr = re.findall(XNginxLog.__PATTERN, p_line)[0]
        self.ip = arr[0]
        self.date = arr[1]
        self.month = arr[2]
        self.year = arr[3]
        self.log_time = arr[4]
        self.method = arr[5]
        self.request = arr[6]
        self.status = arr[7]
        self.bodyBytesSent = arr[8]
        self.refer = arr[9]
        self.userAgent = arr[10]

    def get_query_dict(self):
        query = urlparse.urlparse(self.request).query
        return dict([(k, v[0]) for k, v in urlparse.parse_qs(query).items()])

 

XAdNginxLog.py

from common import XNginxLog


class XAdNginxLog(XNginxLog):

    def __init__(self, p_line=None):
        #super(XAdNginxLog., self).__init__(p_line)
        super(XAdNginxLog, self).__init__(p_line)
        self.abc = 1


    def has_the_same_device_id(self, p_nginx_log=None):
        v = self.get_query_dict().get('device_id') is not None and self.get_query_dict().get('device_id') == p_nginx_log.get_query_dict().get(
            'device_id')
        return v

    def has_the_same_request_id(self, p_nginx_log=None):
        v = self.get_query_dict().get('request_id') is not None and self.get_query_dict().get('request_id') == p_nginx_log.get_query_dict().get(
            'request_id')
        return v

    def is_xad_request(self):
        v = self.get_query_dict().get('channel_name', None) is not None and self.get_query_dict().get('version', None) is not None
        return v

    def is_xad_tracking(self):
        event_type = self.get_query_dict().get('event_type', None)
        v = event_type is not None and event_type.find('xad_') >= 0
        return v

 

 

使用

log_obj = XAdNginxLog(p_line=line)

 

posted on 2018-04-11 19:38  星河赵  阅读(159)  评论(0编辑  收藏  举报

导航