Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting source line number
#1
is there a way to get the source file line number in some code?  for example:

from __future__ import print_function

# print a message with source line number

n = get_source_code_file_line_number()
n += 2
print('now printing at line',str(n))
Output:
now printing at line 6
OR

from __future__ import print_function
for n in range(3)
    print('now printing at line',str(get_source_code_file_line_number()))
    print('now printing at line',str(get_source_code_file_line_number()))
Output:
now printing at line 2 now printing at line 3 now printing at line 2 now printing at line 3 now printing at line 2 now printing at line 3
it's ok if it is based on starting at 1 instead of 0.
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
Many (most?) editors can toggle line numbers off and on.
Reply
#3
i mean at run time

i found this solution that seems to work:

import inspect
print( 'line', inspect.getframeinfo(inspect.currentframe()).lineno, 'of file', inspect.getframeinfo(inspect.currentframe()).filename )
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
Yes can use inspect for this.
Can make a function,then insert that function call will print line number.
import inspect

def line_numb():
   '''Returns the current line number in our program'''
   return inspect.currentframe().f_back.f_lineno

# print a message with source line number
n = 2 ; print('variable {} is at line {}'.format(n, line_numb()))
print('now printing at line {}'.format(line_numb()))
Output:
variable 2 is at line 8 now printing at line 9
Reply
#5
(Feb-27-2017, 03:29 AM)snippsat Wrote: Yes can use inspect for this.
Can make a function,then insert that function call will print line number.
import inspect

def line_numb():
   '''Returns the current line number in our program'''
   return inspect.currentframe().f_back.f_lineno

# print a message with source line number
n = 2 ; print('variable {} is at line {}'.format(n, line_numb()))
print('now printing at line {}'.format(line_numb()))
Output:
variable 2 is at line 8 now printing at line 9

this also gives me the solution to another long time problem: a function to display the name and value of a variable, given only one argument ... a string of the name (so a coding error by the user of this function cannot result in a mismatch) Dance

from inspect import currentframe
from os import environ
def v(n):
    if 'nodebug' in environ:
        return
    x=currentframe().f_back.f_locals
    if n not in x:
        return print(n,'not assigned')
    return print(n,'=',repr(x[n]))
(*) code not tested

edit: correction applied to code: s/[0]//
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
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 583 Aug-22-2023, 10:14 AM
Last Post: GYKR
  line number of first and second occurance of string in a file mdalireza 1 1,847 Nov-18-2019, 09:55 AM
Last Post: perfringo
  Search for the line number corresponding to a value Lali 0 1,657 Oct-22-2019, 08:56 AM
Last Post: Lali
  print number of a list line per line lateublegende 2 2,733 Mar-20-2019, 04:07 PM
Last Post: lateublegende
  Adding a line number to an lxml Element vindy 0 3,366 Mar-08-2019, 08:34 PM
Last Post: vindy
  What are ways of pointing cross-compiled origin source line for python? wyvogew 2 2,831 Feb-02-2019, 03:16 PM
Last Post: wyvogew
  get the number in the line in text file lateublegende 2 2,497 Jan-29-2019, 06:03 PM
Last Post: lateublegende
  Uncanny line of code in Torchat source file pkm 2 2,699 Jan-05-2019, 04:01 PM
Last Post: pkm
  i woule a way to parse a line python source like split Skaperen 2 2,791 Nov-11-2018, 07:35 PM
Last Post: Skaperen
  getting the source line number Skaperen 12 7,909 Dec-17-2017, 04:29 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