首页 > 解决方案 > python 2.6 linecache.getline() 和标准输入。它是如何工作的?

问题描述

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

为了合理清楚起见,我已经包含了与此相关的脚本部分,但不要期望能够运行它。这只是为了向您展示正在发生的事情(任何从事 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 是如何工作的?如果给定一个常规文件名,它与它的工作方式有什么不同吗?

标签: pythonfile-iopython-2.6

解决方案


linecache.getline() 接受文件名,而不是文件对象。当文件名被传递给诸如 open() 和 os.stat() 之类的调用时,它的设计并不是这样工作的。

供参考:https ://github.com/python/cpython/blob/2.6/Lib/linecache.py


推荐阅读