LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Looking for any good idea to read python script. (https://www.linuxquestions.org/questions/linux-software-2/looking-for-any-good-idea-to-read-python-script-4175677137/)

kaz2100 06-16-2020 01:11 AM

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

pan64 06-16-2020 01:20 AM

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.

dugan 06-17-2020 11:29 AM

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

teckk 06-17-2020 12:55 PM

Quote:

How do experts read python script, written by somebody else?
You'll have to become familiar with python, and it's modules that the script uses.
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
>>> help(urllib)

Help on package urllib:

NAME
    urllib

MODULE REFERENCE
    https://docs.python.org/3.8/library/urllib
...


kaz2100 06-17-2020 08:25 PM

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

JJJCR 06-18-2020 03:32 AM

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!!!

kaz2100 06-22-2020 08:32 PM

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)
BIB.ARROW_UP = BIB.ARROWS.Add(images.getSmallUpArrowBitmap())

images.py looks like
Code:

def getSmallUpArrowData():
    return \
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\
\x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\
\x00\x00<IDAT8\x8dcddbf\xa0\x040Q\xa4{h\x18\xf0\xff\xdf\xdf\xffd\x1b\x00\xd3\
\x8c\xcf\x10\x9c\x06\xa0k\xc2e\x08m\xc2\x00\x97m\xd8\xc41\x0c \x14h\xe8\xf2\
\x8c\xa3)q\x10\x18\x00\x00R\xd8#\xec\xb2\xcd\xc1Y\x00\x00\x00\x00IEND\xaeB`\
\x82'

def getSmallUpArrowBitmap():
    return BitmapFromImage(getSmallUpArrowImage())  <*>

def getSmallUpArrowImage():
    stream = io.StringIO(getSmallUpArrowData())
    return ImageFromStream(stream)

and python3 chokes at <*>.

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

pan64 06-23-2020 01:00 AM

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.

ondoho 06-23-2020 01:44 AM

Quote:

Originally Posted by kaz2100 (Post 6137103)
My goal is major tune up of bibus and if possible, upgrade it to python3.

That looks empty.
Do you mean this https://sourceforge.net/projects/bibus-biblio/ ???

kaz2100 06-28-2020 06:29 PM

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

kaz2100 07-07-2020 02:24 AM

Hya

My problem at #7, 'b' prefix was missing.
Code:

def getSmallUpArrowData():
    return \
b'\x89PNG\r\n\x1a\n\x00\x0...


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.

Similarly, in Python 2, the type Literal["foo"] is equivalent to Literal[b"foo"] -- unless the file includes a from __future__

I still have not found if there are more prefixes.

Several other problems have taken care of, and now I need to figure out how to do with
Code:

class TitledPage(wx.wizard.WizardPageSimple):
So far, replacing the line with
Code:

TitledPage(wx.adv.WizardPageSimple):
does not work. I guess this has to do with wxpython (or wxwidgets), not python.

cheers

pan64 07-07-2020 03:34 AM

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 ....

kaz2100 12-01-2020 01:46 AM

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

pan64 12-01-2020 02:03 AM

Quote:

Originally Posted by kaz2100 (Post 6190511)
1. when text contains <i>...</i>, the program chokes with exception (probably). I need to figure out where.

Which text is it (looks like some kind of html).
which python script is it.
what is the error message (what's happening)

How can we help you without details?

kaz2100 12-01-2020 11:31 PM

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
The script which chokes (I guess, reason: pyCharm stops with exception) is
Code:

Title = record.getElementsByTagName("ArticleTitle")[0].firstChild.data
There is no error messages.
Entire xml file is processed like
Code:

a1 = parse("ngFile.xml")
record = a1.getElementsByTagName("PubmedArticle")

I am trying to pinpoint where thing goes wrong using interactive mode, but no success yet. Script chokes, while 'step by step manually' does not.

cheers


All times are GMT -5. The time now is 05:04 PM.