List method argument information for classes and modules in Python

At work, I needed to list all the method arguments that belong to a Python module, so I looked it up. I think it's a very niche task, but instead of a memo (^ _ ^;)

policy

Explore from top-level class and module meta information with the inspect module.

Details of the inspect module-> https://docs.python.org/2/library/inspect.html

inspect.getmembers(object)

All members belonging to the object given as an argument are returned in a list of the form tuple ('name','value').

inspect.getargspec(func)

The argument information of the function given to the argument is returned in the format of tuple (args, varargs, keywords, defaults). args contains a list of arguments.

Below is an example.

An example is Trove's python client, which is openstack's DBaaS. (I wanted to check all the arguments of the I / F that issues the API to trove.)

troveclient.v1.client.Client Structure that has instances of various classes in the instance variable of the class of Below is the code to check the method arguments that each instance has

import inspect
import troveclient.v1.client as dbcli


cli = dbcli.Client('', '', '')
cli_info = inspect.getmembers(cli)
for rds_info in cli_info:
    if rds_info[0][0] != "_":
        # Above if statement is for removing private class
        for info in inspect.getmembers(rds_info[1]):
            if inspect.ismethod(info[1]):
                if info[0][0] != "_":
                    # Above "if" statement is for removing private method
                    for arg in inspect.getargspec(info[1]).args:
                        print rds_info[0], info[0], arg

Below is the code to investigate the CLI I / F getmembers really fetches all the attributes I also got the attributes declared in the decorator.

 import inspect
 import troveclient.v1.shell as rdscli

 for i in inspect.getmembers(rdscli):
     if inspect.isfunction(i[1]):
         for j in inspect.getmembers(i[1]):
             if j[0] == 'arguments':
                 # Above if statement is for retrieving decorators attribute. 
                 for k in j[1]:
                     required = 'y'
                     default = 'None'
                     helpstr = k[1].get('help')
                     if 'default' in k[1].keys():
                         required = 'n'
                         default = k[1].get('default')
                     if default is None:
                         default = 'None'
                     if helpstr is None:
                         helpstr = 'None'
                     if isinstance(default, list):
                         default = "[" + ",".join(default) + "]"
                     print(i[0] + "|" + k[0][0] + "|" + required +
                           "|" + default + "|" + helpstr)

Recommended Posts

List method argument information for classes and modules in Python
Difference between list () and [] in Python
List concatenation method in python, difference between list.extend () and “+” operator
Useful tricks related to list and for statements in Python
Modules and packages in Python are "namespaces"
Difference between append and + = in Python list
Summary of modules and classes in Python-TensorFlow2-
Instant method grammar for Python and Ruby (studying)
[Python for Hikari] Chapter 09-02 Classes (Creating and instantiating classes)
Sorted list in Python
List of python modules
Sorted list in Python
Filter List in Python
Python packages and modules
Simplex method (simplex method) in Python
List find in Python
Private method in python
Rock-paper-scissors poi in Python for beginners (answers and explanations)
Implemented List and Bool in Python and SQLite3 (personal note)
List of Python libraries for data scientists and data engineers
[Python] No value for argument'self' in unbound method call
[python] Get the list of classes defined in the module
Preferences for playing Wave in Python PyAudio and PortAudio
Problems and countermeasures for Otsu's binarization overflow in Python
List method for nested resources in Django REST framework
[Python] How to sort dict in list and instance in list
Recursively search for files and directories in Python and output
Python code for k-means method in super simple case
Python list, for statement, dictionary
Understand Python packages and modules
[Python] Create a date and time list for a specified period
Search for strings in Python
Sort and output the elements in the list as elements and multiples in Python.
[Python] How to delete rows and columns in a table (list of drop method options)
Techniques for sorting in Python
Tips for coding short and easy to read in Python
Python classes learned in chemoinformatics
Convenient writing method when appending to list continuously in Python
Stack and Queue in Python
Parse the Researchmap API in Python and automatically create a Word file for the achievement list
Implement method chain in Python
Python list and tuples and commas
Problems and solutions when asked for MySQL db in Python 3
Python list comprehensions and generators
Newton's method in C ++, Python, Go (for understanding function objects)
[Python / PyQ] 4. list, for statement
Unittest and CI in Python
Python #list for super beginners
Suppressing method overrides in Python
Getting list elements in Python
Plot geographic information in Python
About "for _ in range ():" in python
Solving simultaneous linear equations in Python (sweeping method and fractional representation)
Create a CGH for branching a laser in Python (laser and SLM required)
List of Linear Programming (LP) solvers and modelers available in Python
Specify your own class in class argument and return type annotation in Python
Extract multiple list duplicates in Python
Check for memory leaks in Python
MIDI packages in Python midi and pretty_midi
[python] Manage functions in a list
Output 2017 Premium Friday list in Python