1

is it possible for me to get the line number which variable is defined in a different file. for example:

file1.py
x = 5
mylist = [ 1, 2, 3]


file2.py

execfile("file1.py")
# TODO
# get line number of 'x' or 'mylist'

I assume each variable is defined only once but just in case what if they defined multiple times?

Thanks

rantanplan
  • 7,283
  • 1
  • 24
  • 45
idanshmu
  • 5,061
  • 6
  • 46
  • 92
  • 1
    If they are defined multiple times then the last definition persists. So the question is what do you need this for? – rantanplan Nov 05 '12 at 09:57
  • 'file1.py' is being imported to 'file2.py' using excefile() command (just edited my question). I would like to know which file x (or mylist) is defined. – idanshmu Nov 05 '12 at 10:12

2 Answers2

4

A slightly improved version of Ashwini's answer, using the ast module and not regular expressions is:

import ast

class GetAssignments(ast.NodeVisitor):
    def visit_Name(self, node):
        if isinstance(node.ctx, ast.Store):
            print node.id, node.lineno

with open('testing.py') as fin:
    module = ast.parse(fin.read())
    GetAssignments().visit(module)

And I think something similar can be used on already compiled objects...

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • as I mentioned in my comments. I'd like to avoid reading 'file1.py' since it is already accessible in 'file2.py'. by the way I'm using python 2.3 so I don't think I have 'with' and 'ast' available – idanshmu Nov 05 '12 at 10:42
  • 2
    @user1798187 Here's a radical idea - what asking a question, state all available information... so that's 1) you're using Python 2.3 only, that 2) you already have something from `execfile` (an example source is...), and you'd like a result of.... – Jon Clements Nov 05 '12 at 10:49
  • @JonClements Hi, Jon. I'm trying to modify this code, but when I add a line `return node.lineno` it won't return anything. What's going on here? – SurpriseDog Mar 23 '21 at 01:37
0

using regex:

try something like this, it will return all the lines wherever it finds x = something :

In [25]: with open("fil.py") as f:
    print [index for index,line in enumerate(f) if re.findall(r'(x\s+=)',line)]
   ....:     
[0, 2, 3]

where fil.py contains:

x = 5
mylist = [ 1, 2, 3]
x = 7
x = 8
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • Thanks but bear in mind that I don't like to read 'file1.py' since this is a python file that is being imported to 'file2.py' using excefile() command (just edited my question) – idanshmu Nov 05 '12 at 10:10
  • @user1798187 and the reason for doing this? – Ashwini Chaudhary Nov 05 '12 at 10:14
  • I don't like to read the 'file1.py' since it is already accessible in 'file2.py'. I can access its members, meaning, see which variables are defined and their values. the only thing I am missing is the line number. I need the line number mainly to give detailed log report. say 'x' must be positive but it is defined as -1 I would like to generate log: "[Line 5] 'x' is invalid. must be positive. Found '-1'" – idanshmu Nov 05 '12 at 10:26
  • 1
    I think then `execfile()` or `import` are not the correct way of doing this as they convert the file into byte code, so you can't fetch the line number from them. You need to read the file with `open()` or may use a parsing module like [configparser](http://docs.python.org/2/library/configparser.html) – Ashwini Chaudhary Nov 05 '12 at 10:46