-
-
Save Marius-Juston/08c574901317ee40837ab87ce0855685 to your computer and use it in GitHub Desktop.
Counts the number of lines that the Python library documentation has
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pkgutil | |
import importlib | |
import inspect | |
import statistics | |
def get_package_docs_and_source(): | |
doc_lengths = [] | |
# List all installed packages | |
for module_info in pkgutil.iter_modules(["Lib"]): | |
package_name = module_info.name | |
print(package_name) | |
try: | |
# Import package dynamically | |
package = importlib.import_module(package_name) | |
# Get all members of the package (classes, functions, etc.) | |
for name, obj in inspect.getmembers(package): | |
# Check if it's a function or class | |
if inspect.isfunction(obj) or inspect.isclass(obj): | |
# Get the docstring | |
doc = inspect.getdoc(obj) | |
if doc: | |
print(doc) | |
s = len(doc.split('\n')) | |
if s > 1: | |
doc_lengths.append(s) | |
print("=" * 80) | |
except Exception as e: | |
# Handle errors while loading modules | |
print(f"Could not import {package_name}: {e}") | |
print(doc_lengths) | |
print(min(doc_lengths), max(doc_lengths), statistics.mean(doc_lengths), statistics.median(doc_lengths)) | |
# Run the function | |
get_package_docs_and_source() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment