-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
78 lines (61 loc) · 2.28 KB
/
code.py
File metadata and controls
78 lines (61 loc) · 2.28 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
'''
Filename: code.py
Author: Tyler Phillips
This is an inspect-based module with resources for working with one's own code
and accessing object metadata.
'''
# Import modules
import DBC
import inspect
import types
###############################################################################
# Global variables
CALLABLE = (types.FunctionType, types.BuiltinFunctionType, types.MethodType,
types.BuiltinMethodType)
###############################################################################
# Exceptions
###############################################################################
# Classes
###############################################################################
# Functions
def get_methods(target_class):
'''
Purpose: This function finds all of the methods of a class and
organizes them into a dictionary with method names as
keys and the methods themselves as values.
Parameters:
target_class: Any class
Returns: A dictionary.
'''
# Test input
DBC.check_arg_type(target_class, 'target_class', type)
# Define variables
method_list = inspect.getmembers(target_class)
dictionary = {}
# Construct dictionary
for item in method_list:
name = item[0]
function = item[1]
dictionary[name] = function
# Test output
DBC.check_output_type(dictionary, dict)
# Return
return dictionary
def get_function(level: int = 1) -> CALLABLE:
'''
Purpose: Returns the callable function object from the specified level in
the stack. If no level is specified, returns the function that
called this one.
Parameters:
level: Indicates the level (index in the stack) to check. 0 will check
the check_all_argument_types function itself. 1 (the default
value) will check the function that called the
check_all_argument_types function. 2 will check the function
that called that one, and so on.
Returns: A callable object.
'''
DBC.check_all_argument_types()
if not level >= 0:
raise ValueError('level must be >= 0.')
function = inspect.stack()[level][3]
DBC.check_output_type()