Skip to content

Instantly share code, notes, and snippets.

@EkremDincel
Created January 24, 2020 12:34
Show Gist options
  • Save EkremDincel/b97861cc03af9fbc96f707f9a85528f8 to your computer and use it in GitHub Desktop.
Save EkremDincel/b97861cc03af9fbc96f707f9a85528f8 to your computer and use it in GitHub Desktop.
Python standard library attributes
import collections
import importlib
import inspect
import sys
from stdlib_list import stdlib_list
MODULES = stdlib_list("3.8")
def gettype(x):
if inspect.ismodule(x): return "module"
if inspect.isclass(x): return "class"
if inspect.isroutine(x): return "function"
if isinstance(x, type): return "type"
if x is None or x is ...: return "primitive"
if isinstance(x, (str, bytes)): return "primitive"
if isinstance(x, (float, int, complex)): return "primitive"
if isinstance(x, (list, dict, tuple, set, frozenset)): return "primitive"
return "other"
types = collections.defaultdict(lambda: 0)
processed_modules = []
def process_module(module):
if module in processed_modules:
return
processed_modules.append(module)
for a in dir(module):
attr = getattr(module, a)
types[gettype(attr)] += 1
if inspect.ismodule(attr):
process_module(attr)
for m in MODULES:
try:
module = importlib.import_module(m)
except ImportError:
continue
process_module(module)
for typename, count in sorted(types.items(), key=lambda kv: kv[1], reverse=True):
print(count, typename)
@EkremDincel
Copy link
Author

EkremDincel commented Feb 23, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment