Home > front end >  Correct Way of getting all functions of a module?
Correct Way of getting all functions of a module?

Time:12-20

I am trying a get function of a module but when trying with inspect i am getting false value kindy suggest the proper way of doing this, expected is the i should be able to get the fuction name which is directly callable , here i am able to get the function name but why inspection is throwing False

import psutil
from inspect import isfunction, ismethod

c = dir(psutil)

for x in c:
    print(x, isfunction(x), ismethod(x))

Output

ABOVE_NORMAL_PRIORITY_CLASS False False
AF_LINK False False
AIX False False
AccessDenied False False
BELOW_NORMAL_PRIORITY_CLASS False False
BSD False False
CONN_CLOSE False False
CONN_CLOSE_WAIT False False
CONN_CLOSING False False
CONN_DELETE_TCB False False
CONN_ESTABLISHED False False
CONN_FIN_WAIT1 False False
CONN_FIN_WAIT2 False False
CONN_LAST_ACK False False
CONN_LISTEN False False
CONN_NONE False False
CONN_SYN_RECV False False
CONN_SYN_SENT False False
CONN_TIME_WAIT False False
Error False False
FREEBSD False False
HIGH_PRIORITY_CLASS False False
IDLE_PRIORITY_CLASS False False
IOPRIO_HIGH False False
IOPRIO_LOW False False
IOPRIO_NORMAL False False
IOPRIO_VERYLOW False False
LINUX False False
MACOS False False
NETBSD False False
NIC_DUPLEX_FULL False False
NIC_DUPLEX_HALF False False
NIC_DUPLEX_UNKNOWN False False
NORMAL_PRIORITY_CLASS False False
NoSuchProcess False False
OPENBSD False False
OSX False False
POSIX False False
POWER_TIME_UNKNOWN False False
POWER_TIME_UNLIMITED False False
PermissionError False False
Popen False False
Process False False
ProcessLookupError False False
REALTIME_PRIORITY_CLASS False False
STATUS_DEAD False False
STATUS_DISK_SLEEP False False
STATUS_IDLE False False
STATUS_LOCKED False False
STATUS_PARKED False False
STATUS_RUNNING False False
STATUS_SLEEPING False False
STATUS_STOPPED False False
STATUS_TRACING_STOP False False
STATUS_WAITING False False
STATUS_WAKING False False
STATUS_ZOMBIE False False
SUNOS False False
TimeoutExpired False False
WINDOWS False False
ZombieProcess False False
_LOWEST_PID False False
_PY3 False False
_SENTINEL False False
_TOTAL_PHYMEM False False
__all__ False False
__author__ False False
__builtins__ False False
__cached__ False False
__doc__ False False
__file__ False False
__loader__ False False
__name__ False False
__package__ False False
__path__ False False
__spec__ False False
__version__ False False
_as_dict_attrnames False False
_assert_pid_not_reused False False
_common False False
_compat False False
_cpu_busy_time False False
_cpu_times_deltas False False
_cpu_tot_time False False
_last_cpu_times False False
_last_cpu_times_2 False False
_last_per_cpu_times False False
_last_per_cpu_times_2 False False
_lock False False
_pmap False False
_ppid_map False False
_pprint_secs False False
_psplatform False False
_psutil_windows False False
_pswindows False False
_timer False False
_wrap_numbers False False
boot_time False False
collections False False
contextlib False False
cpu_count False False
cpu_freq False False
cpu_percent False False
cpu_stats False False
cpu_times False False
cpu_times_percent False False
datetime False False
disk_io_counters False False
disk_partitions False False
disk_usage False False
functools False False
getloadavg False False
long False False

CodePudding user response:

You could use __dict__, which returns the objectName and reference to the function:

import psutil
import inspect

for funcName, funcReference in psutil.__dict__.items():
    print(funcName, inspect.isfunction(funcReference))

Out:

__name__ False
__doc__ False
__package__ False
__loader__ False
__spec__ False
__path__ False
__file__ False
__cached__ False
__builtins__ False
collections False
contextlib False
datetime False
functools False
os False
...
process_iter True
wait_procs True
cpu_count True
cpu_times True
_last_cpu_times False
_last_per_cpu_times False
_cpu_tot_time True
_cpu_busy_time True
_cpu_times_deltas True
cpu_percent True
_last_cpu_times_2 False
_last_per_cpu_times_2 False
cpu_times_percent True

CodePudding user response:

dir return List[str], It's element is str object. dir(psutil) return the names in the psutil scope do you mean

for obj_name in dir(psutil):
    obj = getattr(psutil, obj_name)
    print("obj: %s, isfunction: %s, ismethod: %s" % (obj_name, isfunction(obj), ismethod(obj)))

CodePudding user response:

why inspection is throwing False

dir with single argument does return list of names as strings, for example

import imghdr  # module from standard library
print(dir(imghdr))

output

['PathLike', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'test', 'test_bmp', 'test_exr', 'test_gif', 'test_jpeg', 'test_pbm', 'test_pgm', 'test_png', 'test_ppm', 'test_rast', 'test_rgb', 'test_tiff', 'test_webp', 'test_xbm', 'testall', 'tests', 'what']

Note enclosing ', whilst inspect.isfunction is awaiting object. Consider following simple example

import inspect
def add(x,y):
    return x y
print(inspect.isfunction('add'))  # False
print(inspect.isfunction(add))  # True
  • Related