link to a function inside a file (get function's name) when I only know a line number inside that file
See original GitHub issueProblem 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:
- Created 2 years ago
- Comments:14 (6 by maintainers)
Top GitHub Comments
No, I would to straight imports. Assuming your files are
foo.py
andbar.py
, I would recommend to do the following:No imports of variable values. 😃
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).
So now we have 1. For our statistics.
I just knew about issues. I didn’t know you had a ranking of “interesting or relevant questions”. But thanks for letting me know.