Python Forum
getting the source line number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting the source line number
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
http://code.activestate.com/recipes/1452...er-easily/
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
See this post I did some time ago: https://python-forum.io/Thread-Walking-t...call+stack
Reply
#5
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
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.
Reply
#7
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
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.
Reply
#9
looks nice, does it also search globals for that name?  will it support dot attribute names, such as {self.whatever} that classes might need?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
I would expect globals to work if not overridden.
I avoid globals like the plague.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sequential number for rows retrieved and storing the Primary UKey to the line number GYKR 2 579 Aug-22-2023, 10:14 AM
Last Post: GYKR
  line number of first and second occurance of string in a file mdalireza 1 1,828 Nov-18-2019, 09:55 AM
Last Post: perfringo
  Search for the line number corresponding to a value Lali 0 1,642 Oct-22-2019, 08:56 AM
Last Post: Lali
  print number of a list line per line lateublegende 2 2,717 Mar-20-2019, 04:07 PM
Last Post: lateublegende
  Adding a line number to an lxml Element vindy 0 3,358 Mar-08-2019, 08:34 PM
Last Post: vindy
  What are ways of pointing cross-compiled origin source line for python? wyvogew 2 2,810 Feb-02-2019, 03:16 PM
Last Post: wyvogew
  get the number in the line in text file lateublegende 2 2,482 Jan-29-2019, 06:03 PM
Last Post: lateublegende
  Uncanny line of code in Torchat source file pkm 2 2,684 Jan-05-2019, 04:01 PM
Last Post: pkm
  i woule a way to parse a line python source like split Skaperen 2 2,782 Nov-11-2018, 07:35 PM
Last Post: Skaperen
  getting source line number Skaperen 4 14,272 Feb-27-2017, 06:56 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020