Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.1 kB
1
Indexable
Never
import inspect
import bs4

my_module = bs4

# Print out all class objects inside a package
A: list[tuple[str, type]] = inspect.getmembers(my_module, inspect.isclass)
B: list[str] = [
    m[0] for m in A if m[1].__module__ == my_module.__name__
]  # B prints the main package classes.
C: list[str] = [
    m[0] for m in A
]  # C prints all class objects, including those are located in the subpackages.
D: list[str] = [m[1].__module__ for m in A]  # D prints all module names.



def record_groupby_keys(_data_pairs: list[tuple[str, str]]) -> dict[str, list]:
    db: dict[str, list] = {}
    for package, class_name in _data_pairs:
        if package not in db:
            db[package] = []
            db[package].append(class_name) #KeyError: 'bs4'. This error is raised when you try to access a key that isn't in a dictionary.
        else:
            db[package].append(class_name)
    return db

contain_data: list[tuple[str, str]] = list(zip(D, C))
H = record_groupby_keys(contain_data)
from pprint import pprint 
#pprint.pprint(H)