-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_methods.py
More file actions
88 lines (67 loc) · 3.1 KB
/
object_methods.py
File metadata and controls
88 lines (67 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def get_methods_help(*args):
"""
Displays help information for methods of given data types or objects.
This function accepts one or more data types (e.g., str, list, dict) or
instances, converts any passed-in variables to their corresponding types,
and then lists all public methods of each type along with their help text
from the built-in `help()` function in a compact and readable format.
Use cases:
- Quickly explore which methods a type or object provides.
- Get detailed descriptions of methods directly in the terminal.
- Learn about Python objects and their functionalities.
Examples:
get_methods_help(str, list) # Shows methods for str and list
get_methods_help("hello", [1, 2, 3]) # Same as above; variables are converted to str and list
Note:
The function clears the terminal screen and exits the program when done.
Remove the sys.exit() call if you want to use the output in another context.
"""
import os
import sys
from io import StringIO
def print_help(method):
""" Helper function to print the help-text """
tmp_stdout = sys.stdout
# Redirect standard output to a StringIO object
# This captures what should be written to the console
sys.stdout = StringIO()
# Produce the help text for the specific method
help(method)
# Fetch the text as a string
method_help = sys.stdout.getvalue()
# Restore the standard output back to its original state
sys.stdout = tmp_stdout
# Formats the printout
method_help = method_help.splitlines()[2:-1]
print('\n'.join([line.strip() for line in method_help if line.strip()]))
# If the argument passed into the function isn't an object or is a
# function or method itself this iterator will handle the printouts of that
builtin_functions_help = []
for arg in args:
if not isinstance(arg, type):
builtin_functions_help.append(arg)
builtin_functions_help = iter(builtin_functions_help)
# Convert non-type arguments (variables) to their respective types
args = [type(arg) if not isinstance(arg, type) else arg for arg in args]
# Dictionary to store methods for each data type
data_type_methods = {}
os.system('cls')
for data_type in args:
# Get a list of non-dunder (non-magic) methods for the data type
methods = [method for method in dir(data_type) if not method.startswith('__')]
data_type_name = f'{data_type.__name__} methods'.upper()
# Store the list of methods in the dictionary
data_type_methods[data_type_name] = methods
print(f'\033[1;31m{data_type_name}:\033[0m')
if len(methods) == 0:
if not isinstance(data_type, type):
print_help(next(builtin_functions_help))
else:
print_help(data_type)
print()
continue
for method in methods:
print(f'\033[1;35m{method}: \033[0m')
print_help(getattr(data_type, method))
print()
sys.exit()