-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path69.py
More file actions
36 lines (31 loc) · 1.48 KB
/
69.py
File metadata and controls
36 lines (31 loc) · 1.48 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
# Problem 69: Write a program mydoc.py to implement the functionality of pydoc. The program should take
# the module name as argument and print documentation for the module and each of the functions defined in that
# module.
# Hints:
# • The dir function to get all entries of a module
# • The inspect.isfunction function can be used to test if given object is a function
# • x.__doc__ gives the docstring for x.
# • The __import__ function can be used to import a module by name
import sys
import inspect
# try to get the module name from argv, default to 'os' if none provided
try:
MODULE_NAME = sys.argv[1]
except IndexError:
MODULE_NAME = 'os'
module = __import__(MODULE_NAME) # store the module object
# -- FLUFF-------------------------------------
print("Help on module {}".format(MODULE_NAME))
print("---------------------------------------------")
print("DESCRIPTION")
print("---------------------------------------------")
print("\n{}\n".format(module.__doc__))
print("---------------------------------------------")
print("FUNCTIONS")
print("---------------------------------------------")
# -- END FLUFF--------------------------------
# module.__dict__ contains names of attributes as keys and attribute objects themselves as values
for fname, funct in module.__dict__.items(): # get tuple of function name, function object from module
if inspect.isfunction(funct): # check if the current object is a function. Print docs if yes
print(fname)
print(funct.__doc__)