python 2.6版linecache.getline()和标准。它是如何工作的?

2024-09-30 16:37:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个脚本,它通过输入行来查找ID字符串的出现,同时跟踪行号。 然后它向后运行输入以跟踪parentID/childID关系。该脚本接受使用'-f'标志作为参数的日志文件或来自管道的stdin内容。 作为输入部分的日志文件工作得很好,但是从stdin读取似乎不起作用。你知道吗

为了合理的清晰起见,我已经包含了脚本中与此相关的部分,但不希望能够运行它。它只是向您展示一下发生了什么(任何在FIX协议周围从事金融服务的人都会认识到一些事情):

import os
import sys
import linecache

from types import *
from ____ import FixMessage   # custom message class that is used throughout

# Feel free to ignore all the getArgs and validation crap
def getArgs():
    import argparse
    parser = argparse.ArgumentParser(
               description='Get amendment history.')
    parser.add_argument('-f', '--file',
               help="input logfile.'")
    args = parser.parse_args()

    return validateArgs(args)


def validateArgs(args):
    try:
        if sys.stdin.isatty():
            if args.file:
                assert os.path.isfile(args.file.strip('\n')), \
                    'File "{0}" does not exist'.format(args.file)
                args.file = open(args.file, 'r')
        else:
            args.file = sys.stdin
        assert args.file, \
            "Please either include a file with '-f' or pipe some text in"
    except AssertionError as err:
        print err
        exit(1)

    return args    


defGetMessageTrail(logfile, orderId):
    # some input validation
    if isinstance(logfile, StringType):
        try: logfile = open(logfile, 'r')
        except IOError as err: exit(1)
    elif not isinstance(logfile, FileType):
        raise TypeError(
              'Expected FileType and got {0}'.format(type(logfile)))

    linenum  = 0

    # This retrieves the message containing the orderID as well as the linenum
    for line in logfile:
        linenum += 1
        if orderId in line:
            # FixMessage is a custom class that is treated here like
            # a dictionary with some metadata
            # Missing dict keys return 'None'
            # .isvalid is bool results of some text validation
            # .direction is either incoming or outgoing
            # thats all you really need to know
            msg = FixMessage(line)
            if msg.isvalid and msg.direction == 'Incoming':
                yield msg
                break

    # If there is a message parentID, it would be in msg['41']
    if msg['41']:
        messages = findParentMessages(logfile, startline=linenum, msg['41'])
        for msg in messages: yield msg



def findParentMessages(logfile, startline, targetId):
    # Some more input validation
    assert isinstance(logfile, FileType)
    assert isinstance(startline, IntType)
    assert isinstance(targetId, StringType)

    # should just make a integer decrementing generator,
    # but this is fine for the example
    for linenum in range(startline)[::-1]:
        # *** This is where the question lies... ***
        # print(logfile.name) # returns "<stdin>"
        line = linecache.getline(logfile.name, linenum)
        if 'Incoming' in line and '11=' + targetId in line:
            msg = FixMessage(line)
            yield msg
            if msg['41']: findParentMessages(logfile, linenum, msg['41'])
            else: break


def main():
    log = getArgs().file
    trail = getMessageTrail(log, 'ORDER123')


if __name__ == '__main__': main()

问题是,你怎么做linecache.getline将stdin作为文件读取时是否有效?它是否与给定常规文件名时的工作方式不同?你知道吗


Tags: theinimportifisstdinlineargs