NOTE: I have migrated to PyCharm, so I won’t be updating this project for the foreseeable future. If anyone would like to extend it, feel free to create a new fork.
Pygen is a package that allows the user to automatically generate Python code.
Download pygen
from MELPA using M-x package-list-packages
.
To load, require the package and add a hook to enable pygen-mode
when python-mode
is loaded:
(require 'pygen)
(add-hook 'python-mode-hook 'pygen-mode)
Pygen leverages Elpy and python-mode. That’s the package called
python-mode
- not the mode itself. As of Emacs 24.5.1,
python-mode
is not the default Python mode but a separate package.
The default package is python.el
.
Pygen won’t work with a Python setup unless python-mode is installed and Elpy is active. If you have a function that allows the user to navigate to the definition of a function/class, you can use this instead of Elpy.
It provides the following code generation commands. Scroll down for animated examples.
Command | Keychord | Description |
---|---|---|
pygen-generate-class | C-c g c | Generate a python class from the reference under point. |
pygen-generate-function | C-c g f | Generate a python function from the reference under point. |
pygen-generate-static-function | C-c g s | Generate a static python function from the reference under point. |
pygen-extract-variable | C-c g v | Extract the current region into a variable. |
pygen-make-keyword-argument | C-c g k | Adds a keyword argument to the current function. |
pygen-make-sequence-argument | C-c g a | Adds a sequence argument to the current function. |
pygen-add-decorator-to-function | C-c g @ | Adds a decorator in front of the current function. |
pygen-selfify-symbol | N/A | Puts the word self. in front of the current symbol. |
pygen-unselfify-symbol | N/A | Removes the word self. from the current symbol (if it exists). |
pygen-toggle-selfify-symbol | C-c g . | Toggles the word self in front of the current symbol. |
pygen-insert-super | C-c g u | Inserts a proper call to the current method in the superclass. This is basically a wrapper for the built-in python-mode function that does the same thing. |
Some of this functionality is implemented by Ropemacs. However, Ropemacs is slow and will not work if there are syntax errors in your code. Pygen is faster and more robust against syntax errors. This comes at the cost of being less precise - in some situations, it won’t know where to generate code. Both packages can be used alongside one another.
C-c g c
- Generate a python class from the reference under point.
It also works on member classes.
C-c g f
- Generate a python function from the reference under point.
It also works on member functions.
C-c g s
- Generate a static python function from the reference under point.
C-c g v
- Extract the current region into a variable.
C-c g k
- Adds a keyword argument to the current function.
C-c g a
- Adds a sequence argument to the current function.
C-c g @
- Adds a decorator in front of the current function.
Puts the word self.
in front of the current symbol.
Removes the word self.
from the current symbol (if it exists).
C-c g .
- Toggles the self.
keyword in front of the current symbol.
C-c g u
- Inserts a proper call to the current method in the superclass.