3

对于git 别名问题,我希望能够按名称从文件中选择单个 Python 函数。例如:

  ...
  def notyet():
      wait for it

  def ok_start(x):
      stuff
      stuff
      def dontgettrickednow():
         keep going
  #stuff
      more stuff

  def ok_stop_now():

在算法方面,以下将足够接近:

  1. 当您找到匹配的行时开始过滤/^(\s*)def $1[^a-zA-Z0-9]/
  2. 继续匹配,直到找到不是 ^\s*#或的行^/\1\s](即,可能缩进的注释,或者比前一个长的缩进)

(我真的不在乎以下函数之前的装饰器是否被拾取。结果是供人类阅读的。)

我试图用 Awk(我几乎不知道)来做到这一点,但这比我想象的要难一些。对于初学者,我需要一种将缩进长度存储在原始def.

4

2 回答 2

4

为什么不让python来做呢?我认为inspection模块可以打印出函数的来源,所以你可以导入模块,选择函数并检查它。不挂断。正在为您寻找解决方案...

好的。事实证明,该inspect.getsource函数不适用于交互定义的内容:

>>> def test(f):
...     print 'arg:', f
...
>>> test(1)
arg: 1
>>> inspect.getsource(test)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\inspect.py", line 699, in getsource
    lines, lnum = getsourcelines(object)
  File "C:\Python27\lib\inspect.py", line 688, in getsourcelines
    lines, lnum = findsource(object)
  File "C:\Python27\lib\inspect.py", line 529, in findsource
    raise IOError('source code not available')
IOError: source code not available
>>>

但是对于您的用例,它将起作用:对于保存到磁盘的模块。test.py以我的文件为例:

def test(f):
    print 'arg:', f

def other(f):
    print 'other:', f

并与此交互式会话进行比较:

>>> import inspect
>>> import test
>>> inspect.getsource(test.test)
"def test(f):\n    print 'arg:', f\n"
>>> inspect.getsource(test.other)
"def other(f):\n    print 'other:', f\n"
>>>

所以......您需要编写一个简单的python 脚本,它接受python 源文件的名称和函数/对象名称作为参数。然后它应该导入模块并检查函数并将其打印到 STDOUT。

于 2012-05-09T08:39:43.550 回答
4

一种使用方式awk。代码注释很好,所以我希望它很容易理解。

内容infile

  ...
  def notyet():
      wait for it

  def ok_start(x):
      stuff
      stuff
      def dontgettrickednow():
         keep going
  #stuff
      more stuff

  def ok_stop_now():

内容script.awk

BEGIN {
        ## 'f' variable is the function to search, set a regexp with it.
        f_regex = "^" f "[^a-zA-Z0-9]"

        ## When set, print line. Otherwise omit line.
        ## It is set when found the function searched.
        ## It is unset when found any character different from '#' with less
        ## spaces before it.
        in_func = 0
}

## Found function.
$1 == "def" && $2 ~ f_regex {

        ## Get position of first 'd' in the line.
        i = index( $0, "d" )

        ## Sanity check. Never should success because the condition was
        ## checked before.
        if ( i == 0 ) {
                next
        }

        ## Get characters until matched index before, check that all of
        ## them are spaces, and get its length.
        indent = substr( $0, 0, i - 1 )
        if ( indent ~ /^[[:space:]]*$/ ) {
                num_spaces = length( indent )
        }

        ## Set variable, print line and read next one.
        in_func = 1
        print
        next
}

## When we are inside the function, line doesn't begin with '#' and
## it's not a blank line (only spaces).
in_func == 1 && $1 ~ /^[^#]/ && $0 ~ /[^[:space:]]/ {

        ## Get how many characters there are until first non-space. The result
        ## is the position of first non-blank, so substract one to get the number
        ## of spaces.
        spaces = match( $0, /[^[:space:]]/ )
        spaces -= 1

        ## If current indent is less or equal that the indent of function definition, then
        ## end of function found, so end processing.
        if ( spaces <= num_spaces ) {
                in_func = 0
        }
}

## Self-explanatory.
in_func == 1 { 
        print
}

像这样运行它:

awk -f script.awk -v f="ok_start" infile

具有以下输出:

  def ok_start(x):
      stuff
      stuff
      def dontgettrickednow():
         keep going
  #stuff
      more stuff
于 2012-05-09T09:56:06.157 回答