0

I have a Python file which I am reading using another python file. I have a line.

CP.TEST.AppendList(Name='MANHOLE', Values=[ 1, 2, ],
        var='MV')
CP.TEST.AppendList(Name='CANHOLE', Values=[ 3, 4, ],
        var='LV')

I am searching this line using

if 'LV' in line:
     print line

Thus, I am getting output as :

            var='LV')

But, I want to get complete line which contains that string. There are many of such multiline python code in my files.

How I can retrieve such python multi lines in codes using python?

Forexample, in this code, I need output as

CP.TEST.AppendList(Name='MANHOLE', Values=[ 1, 2, ],
        var='LV')

using value 'LV'

2 Answers 2

1

To understand that multiple lines of code are actually a single line of python code, you'll need something that understands python.

In python, you can get at the compiler from code. The "compile" command can take text and return a code object, or throw an error if the code is not syntactically correct:

try:
  compile(candidate_code, 'fake filename', 'exec')
  # got here? candidate_code compiles
except:
  # got here? candidate_code doesn't compile

Compiling is completely tolerant of missing definitions for symbols. However, it doesn't like to see extra indentation.

Using this, we can step through the lines of code, looking for multiline statements (groups of lines that successfully compile) and testing any that do compile for the target string. We have to keep compiling all lines up to the current line (otherwise if your statement is indented, it wont compile) and keep track of how many lines we are actually testing (using numcandidatelines). An alternate approach might be simply to detect and strip off extra indentation.

def findPythonLines(aContainsStr, lines):
  numcandidatelines = 0
  for index, line in enumerate(lines):
    numcandidatelines += 1

    try:
      candidate_code = "\n".join(lines[:index+1])
      candidate_statement = "\n".join(lines[index+1-numcandidatelines: index+1])

      compile(candidate_code, 'fake filename', 'exec') # or 'exec'

      # if we get here, this is a new possible result
      if aContainsStr in candidate_statement:
        return candidate_statement
      else:
        # no dice. Well, we need to look for the next candidate
        numcandidatelines = 0
    except:
      pass

  return None

# say input.txt contains the following:
# x = 10
#
# while (x 
#       > 0):
#   g()
#   
#   y = f(Name='MANHOLE', Values=[ 1, 2, ],
#       var='LV')
#  
#   x -= 1

with open ("input.txt", "r") as myfile:
    full_code_lines=myfile.readlines()

print "result: %s" % findPythonLines("'LV'", full_code_lines)

# prints out the following:
#
# result:   
#    y = f(Name='MANHOLE', Values=[ 1, 2, ],
#          var='LV')
Sign up to request clarification or add additional context in comments.

6 Comments

It is not working properly. can you explain me what I need to do If I have to add a file instead of string?
I've edited it to take an array of lines instead of a big string.
Oh, you might get extra blank lines in the output. If so, change the places where it says "\n".join to "".join .
If I have to change string which is taken as example with any complete file. please let me know how can I add it? If I change any string in f(), it gives output as NONE
What are you passing in to it? The revised code above works with a sequence (list) of lines (strings).
|
1

You need to concatenate the single lines which have coded in multiple lines but they all end with ','(if your following PEP 8 in code) EG:

CP.TEST.AppendList(Name='CANHOLE', Values=[ 3, 4, ],
        var='LV')

convert it temporarily to:

CP.TEST.AppendList(Name='CANHOLE', Values=[ 3, 4, ],var='LV')

And search now

with open('colorlabel.py','r') as file:
    temp=''
    for i in file:
        if i.strip().endswith(','):
            temp+=i
        else:
            temp+=i
            if 'lv' in temp:
                print '  >>',temp
            temp=""

4 Comments

This question is not particular about that example. I need to know how to capture the multiline python code if code is into 2-3 or multilines. can you please let me know any generic way?
@sam This generic way .see Python have multiple lines since 79 letter allowed so all the lines end with ',' so try this once.you will get the result and i got right oupput
No, that's not right, you can split python lines explicitly using "\", and you can split lines anywhere inside an expression.
it contains unnecessary new lines. how can we remove it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.