REPIC.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:REPIC 作者: dpinney 项目源码 文件源码
def REPIC(obToPrint):
    '''REPIC stands for Read, Evaluate, Print In Comment. Call this function with an object obToPrint and it will rewrite the current file with the output in the comment in a line after this was called.'''
    cf = inspect.currentframe()
    callingFile = inspect.getfile(cf.f_back)
    callingLine = cf.f_back.f_lineno
    # print 'Line I am calling REPIC from:', callingLine
    for line in fileinput.input(callingFile, inplace=1):
        if callingLine == fileinput.filelineno():
            # Make results, but get rid of newlines in output since that will break the comment:
            resultString = '#OUTPUT: ' + str(obToPrint).replace('\n','\\n') +'\n'
            writeIndex = line.rfind('\n')
            # Watch out for last line without newlines, there the end is just the line length.
            if '\n' not in line:
                writeIndex = len(line)
            # Replace old output and/or any comments:
            if '#' in line:
                writeIndex = line.rfind('#')
            output = line[0:writeIndex] + resultString
        else:
            output = line # If no REPIC, then don't change the line.
        sys.stdout.write(output)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号