Created
January 24, 2020 12:34
-
-
Save EkremDincel/b97861cc03af9fbc96f707f9a85528f8 to your computer and use it in GitHub Desktop.
Python standard library attributes
This file contains 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 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks to @aib for most of the code :)
https://gist.github.com/aib/ce5bb91ee6e006bcaaa4f438879bfd7b