summaryrefslogtreecommitdiffstats
path: root/.config/pythonrc.py
blob: cb3716166e7d3de5072ef2693bd19db7b3534bc9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
_auto_imports = [
    {"name": "sys", "module": "sys"},
    {"name": "os", "module": "os"},
    {"name": "Path", "module": "pathlib", "object": "Path"},
    {"name": "dt", "module": "datetime"},
    {"name": "collections", "module": "collections"},
    {"name": "pprint", "module": "pprint", "object": "pprint"},
    {"name": "it", "module": "itertools"},
    {"name": "tempfile", "module": "tempfile"},
    {"name": "subprocess", "module": "subprocess"},
    {"name": "json", "module": "json"},
    {"name": "math", "module": "math"},
]

def _autoimport():
    import importlib

    print('Auto importing libs:')

    for target in sorted(_auto_imports, key=lambda x: x["name"].lower()):
        try:
            module = importlib.import_module(target["module"])
        except Exception:
            continue

        obj = getattr(module, target["object"]) if target.get("object") else module
        globals()[target["name"]] = obj

        if target.get("object"):
            what = f'{target["object"]} from {target["module"]}'
        else:
            what = f'{target["module"]}'

        if target["module"] == target["name"]:
            named = ''
        else:
            named = f' as {target["name"]}'

        print(f'\t{what}{named}')

_autoimport()