Looking for any good idea to read python script.
Hya,
I have a question for python experts. How do experts read python script, written by somebody else? I have some sort of trouble how variables are defined, variable/module confusion... I do not have much, but some experience with basic, Fortran, Pascal and C. Thanks to definition (header in C), it is easy to decipher Pascal and C, whereas it is easy to be entangled in spaghetti in basic. Fortran can be easy and hard. As somebody else asked similar question, I am sure that I am NOT the only one to have similar question. With pycharm, too many variables show up... (I guess there are dir(), vars() and some other potentially useful weapons.) Any comments will be appreciated. cheers |
There are two different kind of codes available around here: well written (documented), readable good code and the others.
So it depends on the code quality, not on the language. And obviously we need some practice and knowledge too. |
I'm honestly not sure what answer you're expecting.
First, I know Python well. I have that foundation. Second, I look at what external libraries the Python script uses. That can be seen in its import statements and its requirements.txt file (if it has one). I go skim their documentation. Then, I read the Python script. If you want tooling: a good REPL like PtPython, bpython or iPython sometimes helps, as can knowing how to use PDB. There's a PDB tutorial here: https://github.com/spiside/pdb-tutorial |
Quote:
https://docs.python.org/3/ https://docs.python.org/3/library/tkinter.html https://docs.python.org/3/library/urllib.html https://www.riverbankcomputing.com/static/Docs/PyQt5/ Also: Code:
>>> import urllib |
Experts
Thanks for your replies. It's true, that I do not know what I need to know. The program uses wx, dom, urllib... Documentation of wxpython is good, wxwidgets looks good, but overwhelming... The program itself does not have doc files. I will check one by one. cheers |
I know it's not easy to read someone's code, it's like reading someone else's mind but with some readable hints.
First: Program does what? Second: Just try to view the whole picture, if you are the one to do it. How will you do it? Third: What will you need? (Like libraries, etc.) Fourth: The hardest part, understand the code. I think do it like block by block so it won't be overwhelming. Or clone the source code, put it on a virtual environment run it and check if working perfectly. Then, start deleting some lines on the code and run the program. If an error occurs, then at least you will be able to get some idea and the importance of the line that was deleted. Summary: It's not easy, it's a nightmare. Of course, depends on your level of expertise. Lastly, once you get a hang of it. Give yourself a favor and be a nice fellow, make some documentation. :) Good luck!!! |
Experts.
Thanks for suggestions. I am learning how to navigate with help, still not quite (difference between help(###) and help('###')). My goal is major tune up of bibus and if possible, upgrade it to python3. So far, I took care of 1, file() -> open() 2, gettext.install -> t = gettext.translation(...) (\n) _ = gettext (python 3.8.3 doc, under gettext.install and install()) without confidence. Next one does not look hard, but not solved yet. I assume notation of hex numbers need modification. Code:
BIB.ARROWS = wx.ImageList(16, 16) Code:
def getSmallUpArrowData(): Another point I do not understand is (not my project related) that when initialize wx application, several scripts use a = wx.App(), while wxpython document reads wx.App(PyApp). What is PyApp? What is it for? I will update. cheers |
About wx.App(PyApp): This is an initialization of an object wx.App and actually it is inherited from PyApp. PyApp is another object, see https://pypi.org/project/pyApp/.
I'm afraid you need to learn about OOP (Objectum Oritented Programming) and especially how does it work in python3. |
Quote:
Do you mean this https://sourceforge.net/projects/bibus-biblio/ ??? |
Experts
Thank you for replies. Yes, I do need to learn OOP using wx (or whatever). I once used OOP quite a while ago, Macintosh (pre i-Mac age), following Inside Macintosh. Definition of this kind of parameters was easily found. Here, I have some trouble of finding this kind of information, --header file, Inside Macintosh or equivalent. Another point I need to know is: how python behave when some (probably important) parameter is omitted, "a = wx.App()"? Yes, another apology. My link was empty. https://sourceforge.net/projects/bibus-biblio/ is correct. cheers |
Hya
My problem at #7, 'b' prefix was missing. Code:
def getSmallUpArrowData(): This site says Code:
For example, in Python 3, the type Literal["foo"] is equivalent to Literal[u"foo"], since "foo" is equivalent to u"foo" in Python 3. Several other problems have taken care of, and now I need to figure out how to do with Code:
class TitledPage(wx.wizard.WizardPageSimple): Code:
TitledPage(wx.adv.WizardPageSimple): cheers |
there is a tool: https://docs.python.org/3/library/2to3.html to convert python2 code to python3. It will perfectly handle all the general issues, but there can be special cases when it fails.
would be nice to post your problem exactly, like: what did you try, what did you expect, what's happened .... |
Hya,
Apology for long silence. My life was not straightforward... I have learned a little bit. Currently, I am having trouble with dom library to parse xml file. Two problems: 1. when text contains <i>...</i>, the program chokes with exception (probably). I need to figure out where. 2. python3 dom behaves differently from python2 counterpart. An exception where python2 is happy. With PyCharm, I have not been able to point where problematic code is. cheers |
Quote:
which python script is it. what is the error message (what's happening) How can we help you without details? |
Hya,
Thank you for your reply. Detail of problem 1 at #13 follows. Text (xml) can be downloaded with Code:
wget https://www.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi\?db=PubMed\&id=31819487\&rettype=null\&retmode=xml Code:
Title = record.getElementsByTagName("ArticleTitle")[0].firstChild.data Entire xml file is processed like Code:
a1 = parse("ngFile.xml") cheers |
All times are GMT -5. The time now is 05:04 PM. |