Python Forum

Full Version: getting the source line number
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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.
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.
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.
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.
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.
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.
looks nice, does it also search globals for that name?  will it support dot attribute names, such as {self.whatever} that classes might need?
I would expect globals to work if not overridden.
I avoid globals like the plague.
Pages: 1 2