Python Forum
getting the source line number - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: getting the source line number (/thread-6928.html)

Pages: 1 2


getting the source line number - Skaperen - Dec-14-2017

is there any easy way for the running code to get it's source line number at run time?  i'd like to include it in running diagnostics without killing the script at every such place.  i'd imagine this would be meaningless for .pyc and .pyo files.


RE: getting the source line number - buran - Dec-14-2017

http://code.activestate.com/recipes/145297-grabbing-the-current-line-number-easily/


RE: getting the source line number - Skaperen - Dec-15-2017

and i really want to get the line number where the code getting the number gets called from.  so, it should be just another .f_back attribute level in the frame.


RE: getting the source line number - Larz60+ - Dec-15-2017

See this post I did some time ago: https://python-forum.io/Thread-Walking-the-python-call-stack-without-leaks?highlight=call+stack


RE: getting the source line number - Skaperen - Dec-15-2017

my code is a function that takes the name of a variable (or variable.attribute) as a string, looks in the caller's local (if not in local, then next in global) space, and prints it out, name, value and which space it was found in, or if not found, name and that it is not assigned.  all arguments are processed like that.  i want to add the line number at the start so it identifies which call to it outputs which variable vales.


RE: getting the source line number - Larz60+ - Dec-15-2017

You can filter the line number out. Here's a simple class that does that:
import inspect

class GetCallStackItem:
    def __init__(self):
        self.full_stack = inspect.stack()
        for stacklevel, item in enumerate(self.full_stack):
            attributes = self.get_method_attributes(item)
            print(f'\nstacklevel: {stacklevel}')
            for attrname, value in attributes:
                if attrname == 'lineno':
                    print(f'line Number: {value}')

    def get_method_attributes(self, method):
        temp = dir(type('whatever', (object,), {}))
        return [item for item in inspect.getmembers(method) if item[0] not in temp]


if __name__ == '__main__':
    gcsi = GetCallStackItem()
Running this on itself returns the following:
Output:
stacklevel: 0 line Number: 5 stacklevel: 1 line Number: 19
If you add a print statement immediately after the second for statement, you'll get an exhaustive of all that's available.
There may be more stuff that would like to extract.


RE: getting the source line number - Skaperen - Dec-16-2017

what is this 'f' string prefix?

Output:
lt1/forums /home/forums 5> cat gcsi.py import inspect   class GetCallStackItem:     def __init__(self):         self.full_stack = inspect.stack()         for stacklevel, item in enumerate(self.full_stack):             attributes = self.get_method_attributes(item)             print(f'\nstacklevel: {stacklevel}')             for attrname, value in attributes:                 if attrname == 'lineno':                     print(f'line Number: {value}')       def get_method_attributes(self, method):         temp = dir(type('whatever', (object,), {}))         return [item for item in inspect.getmembers(method) if item[0] not in temp]     if __name__ == '__main__':     gcsi = GetCallStackItem() lt1/forums /home/forums 6> py3 gcsi.py   File "gcsi.py", line 8     print(f'\nstacklevel: {stacklevel}')                                       ^ SyntaxError: invalid syntax lt1/forums /home/forums 7>

i posted the source code for that function i wrote, over in Completed Scripts/Snippets.


RE: getting the source line number - Larz60+ - Dec-16-2017

Called an f-string, it's new in python 3.6,
prior to that replace:
print(f'\nstacklevel: {stacklevel}')
with:
print('\nstacklevel: {}'.format(stacklevel))
The second version will also work in python 3.6

The new version is great when writing formatted reports.


RE: getting the source line number - Skaperen - Dec-16-2017

looks nice, does it also search globals for that name?  will it support dot attribute names, such as {self.whatever} that classes might need?


RE: getting the source line number - Larz60+ - Dec-16-2017

I would expect globals to work if not overridden.
I avoid globals like the plague.