question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

link to a function inside a file (get function's name) when I only know a line number inside that file

See original GitHub issue

Problem Description

As a Python newbie, my functions contain not only documentation strings below function definition. I also use to insert some tips (# _TIP marks) about how-to-do something in Python, the first time I use that kind of code.

# file myscript_A.py
def myfunction1():
    """ my documentation """
    mylist = ["Jane", "John", "Jennifer"]
    surname = "Johanssen"
    # _TIP: list comprehension
    fullnames = [name+" "+surname for name in mylist]
...
def myfunction5():
    """ my documentation """
    import pandas as pd
    # _TIP: pandas dataframe merging
    pd.read_csv(...)

# file myscript_B.py
def myfunction22():
    """ my documentation """
    # my code
    # my code
    # _TIP: whatever
    # my code
    # my code
...

Proposal

I have created a tips_collector.py script, which reads a list of given python scripts content, locates all lines containing “# _TIP” marks, and generates a my_tips.html file like this:

  • myscript_A.py [line 6]: list comprehension
  • myscript_A.py [line 165]: pandas dataframe merging
  • myscript_B.py [line 87]: whatever

As I have the line numbers and I also have pdoc-generated html documentation for each of these scripts, I would like to improve my_tips.html file including links to open documentation of container function for each of those tips. I would take profit of anchor marks generated by pdoc (i.e. <a href=./myscript_A.html#myfunction1>list comprehension</a>), like this:

  • myscript_A.py [line 6]: list comprehension
  • myscript_A.py [line 165]: pandas dataframe merging
  • myscript_B.py [line 87]: whatever

Obviously, to do this I need a way to get the function name which contains a given line of code inside a given script file. So get_function_name("myscript_A.py", 165) should return "myfunction5":

def get_function_name(script_name, line_number):
    # what should I do here?
    return(function_name)

Alternatives

I think these issues and concepts are somehow related, but I would need some examples so I can connect them to my specific problem: #268 Get line number of classes and functions Doc.source_lines

Additional context

I am generating my pdoc html documents using the approach described in #325

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mhilscommented, Jan 6, 2022

No, I would to straight imports. Assuming your files are foo.py and bar.py, I would recommend to do the following:

import foo
import bar
import pdoc

for mod in [foo, bar]:
    x = pdoc.doc.Module(mod)

No imports of variable values. 😃

0reactions
abubelinhacommented, Feb 20, 2022

I don’t “pick” non-UTF8 files on purpose. I am just documenting a bunch of files I have.

Some are newly created, most are old, some are mine, most are not. Actually I don’t know where my problematic ANSI file came from, but most probably I downloaded it from github or I re-used a .py file to create a new one … so I bet that’s not so odd to find a non-UTF8 file.

Maybe you have an editor which warns you about non-utf8 encoding in a different color or something alike? As for me, I had really no idea about this file encoding until I hit this problem (I was assuming it was utf8 because that’s the default encoding of my Notepad++ editor).

If you know having an ANSI file occurs to less than 0.1% of users, and you expect all pdoc-submitted files to be utf8 encoded, then I think “don’t use ANSI files” should have been your 1st answer to stop spending your time -and mine- (that was my question, and nothing else).

I haven’t seen a single non-UTF8 Python source code file since switching to Python 3, and I also can’t think of a good reason to pick something else.

So now we have 1. For our statistics.

I also don’t consider it to be a particularly interesting or relevant question

I just knew about issues. I didn’t know you had a ranking of “interesting or relevant questions”. But thanks for letting me know.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Print the file name, line number and function name of a calling ...
I'd pass the data to the function through parameters (maybe get the help of a macro) int info(const char *fname, int lineno, const...
Read more >
Function name has to be same as file name? - MATLAB Central
Hi, I have a .m file called randomStep, and within the file I want to have a function ... aren't the functions supposed...
Read more >
Defining Main Functions in Python
In this step-by-step tutorial, you'll learn how Python main functions are used and some best practices to organize your code so it can...
Read more >
HYPERLINK function - Microsoft Support
The HYPERLINK function creates a shortcut that jumps to another location in the current workbook, or opens a document stored on a network...
Read more >
3. Data model — Python 3.11.1 documentation
A reference to the dictionary that holds the function's global variables — the global namespace of the module in which the function was...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found