Skip to content

Instantly share code, notes, and snippets.

@Marius-Juston
Created March 31, 2025 03:28
Show Gist options
  • Save Marius-Juston/08c574901317ee40837ab87ce0855685 to your computer and use it in GitHub Desktop.
Save Marius-Juston/08c574901317ee40837ab87ce0855685 to your computer and use it in GitHub Desktop.
Counts the number of lines that the Python library documentation has
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