Automatically generate Python Docstring Comment in Emacs

Docstring Comment like PyCharm in Emacs I wanted an automatic generation function, so I prepared a function.

Demo bbb.gif

Introduction method

Just paste the following code into init.el and load it. It is generated when the cursor is placed on the line of the function name definition and C-cd is executed.

;; docstring comment
(defun python-docstring-comment()
  (interactive)
  (let* ((begin-point (point-at-bol))
         (end-point (point-at-eol))
         (function-line (buffer-substring begin-point end-point))
         (space (format "    %s" (replace-regexp-in-string "def.*" "" function-line))))
    (goto-char end-point)
    (insert "\n")
    (insert (format "%s\"\"\"\n" space))
    (when (string-match ".*(\\(.+\\)):" function-line)
      (dolist (arg (split-string (match-string 1 function-line) ","))
        (if (not (equal arg "self"))
            (insert (format "%s:param TYPE %s:\n" space (replace-regexp-in-string "^\\s-+\\|\\s-+$" "" arg))))))
    (insert (format "%s:rtype: TYPE\n" space))
    (insert (format "%s\"\"\"" space))))

(define-key python-mode-map (kbd "C-c d") 'python-docstring-comment)

Recommended Posts

Automatically generate Python Docstring Comment in Emacs
Generate U distribution in Python
Generate QR code in Python
Generate 8 * 8 (64) cubes in Blender Python
Automatically generate frequency distribution table in one shot with Python
[Python] Generate QR code in memory
Automatically format Python code in Vim
Generate Jupyter notebook ".ipynb" in Python
Generate a first class collection in Python
Generate AWS-S3 signed (time-limited) URLs in Python
Write the test in a python docstring
Automatically update Power BI Desktop in Python
Module to generate word N-gram in Python
Generate a class from a string in Python
Generate C language from S-expressions in Python
Generate the Look-and-say Sequence featured in QuizKnock in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Automatically register function arguments to argparse in Python
Try auto to automatically price Enums in Python 3.6
Meta-analysis in Python
Unittest in python
Automatically create word and excel reports in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
Automatically format Python code into PEP8-compliant code with Emacs
N-Gram in Python
python comment out
Programming in python
Try to automatically generate Python documents with Sphinx
Plink in Python
Constant in python
[Python] Automatically generate an example animation list-like image.
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Generate points only in Tokyo using python + shapely
Reflection in Python