From 85bc9012a01f910a8c431d5aa698847a180e208c Mon Sep 17 00:00:00 2001 From: Pavel Egorov Date: Tue, 17 Mar 2026 19:08:57 +0300 Subject: [python] add pythonrc --- .config/fish/conf.d/python.fish | 4 +++- .config/pythonrc.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 .config/pythonrc.py (limited to '.config') diff --git a/.config/fish/conf.d/python.fish b/.config/fish/conf.d/python.fish index b3b5eec..9ea8f6e 100644 --- a/.config/fish/conf.d/python.fish +++ b/.config/fish/conf.d/python.fish @@ -1,3 +1,5 @@ -if type -q pyenv +if type -q pyenv && status is-interactive pyenv init - | source end + +set -x PYTHONSTARTUP "$XDG_CONFIG_HOME/pythonrc.py" diff --git a/.config/pythonrc.py b/.config/pythonrc.py new file mode 100644 index 0000000..febde4e --- /dev/null +++ b/.config/pythonrc.py @@ -0,0 +1,40 @@ +_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"}, +] + +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() -- cgit v1.3