bash脚本从文件中选择一个Python函数 [英] Bash script to select a single Python function from a file

查看:124
本文介绍了bash脚本从文件中选择一个Python函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For a git alias problem, I'd like to be able to select a single Python function from a file, by name. eg:

  ...
  def notyet():
      wait for it

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

  def ok_stop_now():

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

In algorithmic terms, the following would be close enough:


  1. 开始,当你发现一个匹配 / ^(\\ S *)DEF $ 1号线过滤[^ A-ZA-Z0-9] /

  2. 保持匹配,直到找到一条线,是的 ^ \\ S *# ^ / \\ 1 \\ S] (即,无论是可能的,缩进评论,或缩进超过previous一里)

  1. Start filtering when you find a line that matches /^(\s*)def $1[^a-zA-Z0-9]/
  2. Keep matching until you find a line that is not ^\s*# or ^/\1\s] (that is, either a possibly-indented comment, or an indent longer than the previous one)

(我真的不关心,如果下面的函数之前被装饰回升,结果是人类阅读。)

(I don't really care if decorators before the following function are picked up. The result is for human reading.)

我是想用awk的(我几乎不知道),要做到这一点,但它比我想象的有点困难。首先,我需要存储原始 DEF 前缩进长度的一种方式。

I was trying to do this with Awk (which I barely know) but it's a bit harder than I thought. For starters, I'd need a way of storing the length of the indent before the original def.

推荐答案

使用的一种方法 AWK 。 code有很好的注释,所以我希望这是很容易理解。

One way using awk. Code is well commented, so I hope it's easy to understand.

的内容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

这篇关于bash脚本从文件中选择一个Python函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆