# 使用解析Python工具 一次就上手_解開函式類別的奧秘
###### tags: `python`
Copyright 2021, [月下麒麟](https://hackmd.io/@YMont/note-catalog)
---
## Outline
當你應用Python一段時間後,常常會接觸到第三方套件,此時你可能就會將它下載下來做使用。那麼,人總會有好奇心,會想對這個source code(公開的自由軟體原始碼)做一番了解,如它的class, function...,但是程式碼這麼長,它們彼此間的關聯是什麼、又該如何追蹤起?
在探討上述問題前,先來思考一個情境:
`假設它的類別(class)分類有10個,然後每個類別底下又各有10個函式(function),看到這裡,心裡一想可能覺得這個數量還可以trace。假設我們再把數量乘於5倍呢? 試問,你還會想對這個程式進行追蹤嗎? 沒有一定的答案,但你一定會追蹤的很心煩!`
## tool installation
* `pip install pylint`
* download and install [Graphviz](https://graphviz.org/download/)
* git clone open source code [netmiko](https://github.com/ktbyers/netmiko.git)
* Document: [What is the netmiko?](https://pynet.twb-tech.com/blog/netmiko-python-library.html)
## tool guide
**open CMD terminal**
`pyreverse --help`
```
Usage:
pyreverse [options] <packages>
create UML diagrams for classes and modules in <packages>
Options:
-h, --help show this help message and exit
-f <mode>, --filter-mode=<mode>
filter attributes and functions according to
<mode>. Correct modes are :
...
...
```
**command**
`D:\temp>pyreverse -mn -a1 -s1 -f ALL -o pdf D:\netmiko\netmiko\cisco`
```
parsing D:\netmiko\netmiko\cisco\__init__.py...
parsing D:\netmiko\netmiko\cisco\cisco_asa_ssh.py...
parsing D:\netmiko\netmiko\cisco\cisco_ftd_ssh.py...
parsing D:\netmiko\netmiko\cisco\cisco_ios.py...
parsing D:\netmiko\netmiko\cisco\cisco_nxos_ssh.py...
parsing D:\netmiko\netmiko\cisco\cisco_s300.py...
parsing D:\netmiko\netmiko\cisco\cisco_tp_tcce.py...
parsing D:\netmiko\netmiko\cisco\cisco_viptela.py...
parsing D:\netmiko\netmiko\cisco\cisco_wlc_ssh.py...
parsing D:\netmiko\netmiko\cisco\cisco_xr.py...
parsing D:\netmiko\netmiko\cisco\__init__.py...
```
**get result**
It will download two PDF files automatically. There are name =="classes.pdf"== and =="packages.pdf"==
**program flow chart**

classes.pdf

packages.pdf
**check detail**

We reference source code.
```python=
class CiscoIosBase(CiscoBaseConnection):
"""Common Methods for IOS (both SSH and telnet)."""
def __init__(self, *args, **kwargs):
# Cisco-IOS defaults to fast_cli=True and legacy_mode=False
kwargs.setdefault("fast_cli", True)
kwargs.setdefault("_legacy_mode", False)
return super().__init__(*args, **kwargs)
def session_preparation(self):
"""Prepare the session after the connection has been established."""
cmd = "terminal width 511"
self.set_terminal_width(command=cmd, pattern=cmd)
self.disable_paging()
self.set_base_prompt()
def check_config_mode(self, check_string=")#", pattern="#"):
"""
Checks if the device is in configuration mode or not.
Cisco IOS devices abbreviate the prompt at 20 chars in config mode
"""
return super().check_config_mode(check_string=check_string, pattern=pattern)
def save_config(self, cmd="write mem", confirm=False, confirm_response=""):
"""Saves Config Using Copy Run Start"""
return super().save_config(
cmd=cmd, confirm=confirm, confirm_response=confirm_response
)
```
Then, We can find the class name is 'CiscoIosBase' and get four function is '\_\_init\_\_', 'session_preparation', 'check_config_mode', 'save_config'
Thanks for your reading.
reference:[subprocess call unable to find dot although it's installed](https://stackoverflow.com/questions/66040824/subprocess-call-unable-to-find-dot-although-its-installed)
reference:[Converting dot to png in python](https://stackoverflow.com/questions/5316206/converting-dot-to-png-in-python)
reference:[Plotting the Digraph with graphviz in python from DOT file](https://stackoverflow.com/questions/41942109/plotting-the-digraph-with-graphviz-in-python-from-dot-file)
reference:import[How to get pyreverse to work](https://python-forum.io/thread-27300.html)
reference:[Generating UML Diagrams from Python Code](https://bhavaniravi.com/blog/generate-uml-diagrams-from-python-code/)
reference:[command pyreverse help](https://helpmanual.io/help/pyreverse/)
keyword:UML(from C)