Simplify the Python programming section

This commit is contained in:
Gerard Vermeulen 2023-12-11 17:13:55 +01:00
parent 83511c6b4e
commit 26a713a4eb

View File

@ -1155,10 +1155,7 @@ defined in this Org file.
(org-inactive-current-time-stamp :nomanual t)
(org-syntax-convert-keyword-case-to-lower :no-manual t)
(smart-latex-engrave-org-source-blocks :no-manual t)
(zero-all-org-src-blocks-indentation :no-manual t)
"Python"
(choose-common-python-interpreter :no-manual t)
(choose-common-python-linter :no-manual t))))
(zero-all-org-src-blocks-indentation :no-manual t))))
#+end_src
** [[info:info#Top][Info (info)]]
@ -5640,25 +5637,17 @@ Here are links to Python programming background videos:
:CUSTOM_ID: sec:python-mode
:END:
Listing [[lst:setup-python-mode][setup Python mode]] and [[lst:choose-common-python-interpreter][choose common Python interpreter]] select a common
Python interpreter in a virtual environment for use in =python-mode= and
=ob-python= by means of src_emacs-lisp{(choose-common-python-interpreter)}. The
[[https://github.com/pythonic-emacs/pythonic#readme][pythonic]], [[https://github.com/jorgenschaefer/pyvenv#readme][pyvenv]], and [[https://github.com/jorgenschaefer/pyvenv#readme][pyvenv]] packages allow to handle Python virtual
environments within Emacs. The [[https://github.com/pyenv/pyenv][pyenv]] package provides support to work with
[[https://github.com/pyenv/pyenv#readme][pyenv]] (eventually with [[https://github.com/pyenv/pyenv-virtualenv#readme][pyenv-virtualenv]]) to select between different python
versions (eventually each with different environments). In the end, all those
packages do is to set =python-shell-virtualenv-root= (in case of [[https://github.com/pyenv/pyenv#readme][pyenv]] and
[[https://github.com/pythonic-emacs/pythonic#readme][pythonic]]) or tweak the environment variables and restart the relevant Python
child processes (in case of [[https://github.com/jorgenschaefer/pyvenv#readme][pyvenv]]). Therefore, I replace those packages with
listing [[lst:access-pyenv][access pyenv]] and [[lst:select-python-virtual-environment][select a Python interpreter in a virtual environment]] to
set =python-shell-virtualenv-root= from within Emacs.
Listing [[lst:setup-python-mode][setup Python mode]] and [[lst:choose-common-python-linter][choose common Python linter]] select a common Python
linter for the =python-check-command= and the =python-flymake-command= by means
of src_emacs-lisp{(choose-common-python-linter)} from:
1. [[https://github.com/PyCQA/pyflakes][Pyflakes - simple tool which checks Python source files for errors]].
2. [[https://flake8.pycqa.org/en/latest/][Flake8 - your tool for style guide enforcement]].
3. [[https://github.com/charliermarsh/ruff][Ruff - an extremely fast Python linter written in Rust]].
Listing [[lst:setup-python-mode][setup Python mode]] selects a common Python interpreter in a virtual
environment for use in =python-mode= and =ob-python=. The [[https://github.com/pythonic-emacs/pythonic#readme][pythonic]], [[https://github.com/jorgenschaefer/pyvenv#readme][pyvenv]], and
[[https://github.com/jorgenschaefer/pyvenv#readme][pyvenv]] packages allow to handle Python virtual environments within Emacs. The
[[https://github.com/pyenv/pyenv][pyenv]] package provides support to work with [[https://github.com/pyenv/pyenv#readme][pyenv]] (eventually with
[[https://github.com/pyenv/pyenv-virtualenv#readme][pyenv-virtualenv]]) to select between different python versions (eventually each
with different environments). In the end, all those packages do is to set
=python-shell-virtualenv-root= (in case of [[https://github.com/pyenv/pyenv#readme][pyenv]] and [[https://github.com/pythonic-emacs/pythonic#readme][pythonic]]) or tweak the
environment variables and restart the relevant Python child processes (in case
of [[https://github.com/jorgenschaefer/pyvenv#readme][pyvenv]]). Therefore, I replace those packages with listing [[lst:access-pyenv][access pyenv]] and
[[lst:select-python-virtual-environment][select a Python interpreter in a virtual environment]] to set
=python-shell-virtualenv-root= from within Emacs.
Listing [[lst:pyproject-toml-kickoff][kickoff pyproject.toml proposal]] facilitates dropping a [[https://pip.pypa.io/en/stable/reference/build-system/pyproject-toml][pyproject.toml]]
file into Python projects which anyhow should switch to using a [[https://pip.pypa.io/en/stable/reference/build-system/pyproject-toml][pyproject.toml]]
@ -5673,88 +5662,11 @@ rules which make [[https://flake8.pycqa.org/en/latest/][flake8]] or [[https://py
Finally, listing [[lst:flake8-nocolor][flake8-nocolor]] and [[lst:ruff-nocolor][ruff-nocolor]] pipe the =stdout= output of the
[[https://pypi.org/project/flake8/][flake8]] and [[https://pypi.org/project/ruff/][ruff]] executables through =cat= to remove escape sequences.
#+caption[Choose a common Python interpreter]:
#+caption: Choose a common Python interpreter for =ob-python= and =python-mode=.
#+name: lst:choose-common-python-interpreter
#+begin_src emacs-lisp -n
(defun choose-common-python-interpreter (&optional interpreter)
"Let `ob-python' and `python-mode' use the same Python INTERPRETER."
(interactive)
(unless (featurep 'ob-python)
(set-org-babel-language-activity 'python (fboundp 'python-mode)))
(let* ((prompt (format "Choose Python (%s): "
(bound-and-true-p python-shell-interpreter)))
(choices '(ipython python))
(choice (if (member interpreter choices)
(symbol-name interpreter)
(completing-read prompt choices nil t))))
(when (boundp 'org-babel-python-command)
(pcase choice
("python"
(setopt org-babel-python-command
(concat (or (executable-find "python3")
(executable-find "python"))
" -E")))
("ipython"
(setopt org-babel-python-command
(concat (or (executable-find "ipython3")
(executable-find "ipython"))
" --simple-prompt --HistoryAccessor.enabled=False"))))
(message "Now `org-babel-python-command' equals %S"
org-babel-python-command))
(when (boundp 'python-shell-interpreter)
(pcase choice
("python"
(setopt python-shell-interpreter (or (executable-find "python3")
(executable-find "python"))
python-shell-interpreter-args "-E -i"))
("ipython"
(setopt python-shell-interpreter (or (executable-find "ipython3")
(executable-find "ipython"))
python-shell-interpreter-args
"--simple-prompt --HistoryAccessor.enabled=False")))
(message "Now `python-shell-interpreter' equals %S"
python-shell-interpreter))))
#+end_src
#+caption[Choose a common Python linter]:
#+caption: Choose a common Python linter for =python-check-command= and
#+caption: =python-flymake-command=.
#+name: lst:choose-common-python-linter
#+begin_src emacs-lisp -n
(defun choose-common-python-linter (&optional linter)
"Let `python-check-command' and `python-flymake-command' use the same LINTER."
(interactive)
(let* ((prompt (format "Choose Python checker (%s): "
(bound-and-true-p python-check-command)))
(choices '(flake8-nocolor pyflakes ruff-nocolor))
(choice (if (member linter choices)
(symbol-name linter)
(completing-read prompt choices nil t))))
(when (and (boundp 'python-check-command) (boundp 'python-flymake-command))
(pcase choice
("flake8-nocolor"
(setopt python-check-command (executable-find choice)
python-flymake-command (list (executable-find choice) "-")))
("pyflakes"
(setopt python-check-command (executable-find choice)
python-flymake-command `(,(executable-find choice))))
("ruff-nocolor"
(setopt python-check-command (executable-find choice)
python-flymake-command (list (executable-find choice)
"--stdin-filename" "stdin" "-"))))
(when (bound-and-true-p python-check-custom-command)
(setq python-check-custom-command nil))
(message "Python checker commands are %S and %S"
python-check-command python-flymake-command))))
#+end_src
#+caption[Setup Python mode with =ob-python=]:
#+caption: Setup Python mode with =ob-python=.
#+name: lst:setup-python-mode
#+begin_src emacs-lisp -n
(with-eval-after-load 'ob-python
;; (choose-common-python-interpreter 'python)
(setopt org-babel-python-command (concat (or (executable-find "python3")
(executable-find "python"))
" -E")))
@ -5763,11 +5675,9 @@ Finally, listing [[lst:flake8-nocolor][flake8-nocolor]] and [[lst:ruff-nocolor][
(setopt python-indent-guess-indent-offset nil
python-shell-completion-native-disabled-interpreters '("ipython3"
"pypy")
;; (choose-common-python-interpreter 'python)
python-shell-interpreter (or (executable-find "python3")
(executable-find "python"))
python-shell-interpreter-args "-E -i"
;; (choose-common-python-linter 'ruff-nocolor)
python-check-command (executable-find "ruff-nocolor")
python-flymake-command (list (executable-find "ruff-nocolor")
"--stdin-filename" "stdin" "-")))