Created
September 9, 2021 14:14
-
-
Save CLTanuki/dfda4878d85ec57a619b6361028c34da to your computer and use it in GitHub Desktop.
HERE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from inspect import getframeinfo, currentframe | |
def HERE(do_print=True): | |
''' Get the current file and line number in Python script. The line | |
number is taken from the caller, i.e. where this function is called. | |
Parameters | |
---------- | |
do_print : boolean | |
If True, print the file name and line number to stdout. | |
Returns | |
------- | |
String with file name and line number if do_print is False. | |
Examples | |
-------- | |
>>> HERE() # Prints to stdout | |
>>> print(HERE(do_print=False)) | |
''' | |
frameinfo = getframeinfo(currentframe().f_back) | |
filename = frameinfo.filename.split('/')[-1] | |
linenumber = frameinfo.lineno | |
loc_str = 'File: %s, line: %d' % (filename, linenumber) | |
if do_print: | |
print('HERE AT %s' % (loc_str)) | |
else: | |
return loc_str | |
if __name__ == '__main__': | |
HERE() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment