美文网首页
状态模式

状态模式

作者: nummycode | 来源:发表于2017-07-07 14:36 被阅读11次

    状态模式就是根据不同的状态调用不同的方法

    def outputparser(loglines):
        state = 'header'
        program,end_time,send_failure= None,None,False
    
        for line in loglines:
            if state == 'header':
                program = line.split(',')[0]
                state = 'body'
            elif state == 'body':
                if 'send_failure' in line:
                    send_failure = True
                if '======' in line:
                    state = 'footer'
            elif state == 'footer':
                end_time = line.split(',')[0]
        return program, end_time, send_failure
    
    print(outputparser(['sampleapp,only a sampleapp',
                  'logline1  sadfsfdf',
                  'logline2 send_failure',
                  '=====================',
                  '30th Jul 2016,END']))
    

    输出如下:

    ('sampleapp', '30th Jul 2016', True)
    

    相关文章

      网友评论

          本文标题:状态模式

          本文链接:https://www.haomeiwen.com/subject/bgnlhxtx.html