Skip to content
/ pygen Public
forked from JackCrawley/pygen

Python code generation in Emacs with Elpy and python-mode

License

Notifications You must be signed in to change notification settings

syohex/pygen

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

Pygen is a package that allows the user to automatically generate Python code.

Installation

  • State “TODO” from [2016-10-16 Sun 17:54]

This section still needs to be completed. Installation should shortly be available on MELPA.

Dependencies

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. However, the variable pygen-navigate-to-definition-command has been added to allow Elpy to be disabled - if you have another command that allows Pygen to navigate to function definitions.

Commands

It provides the following code generation commands. Scroll down for animated examples.

CommandKeychordDescription
pygen-generate-classC-c C-= cGenerate a python class from the reference under point.
pygen-generate-functionC-c C-= fGenerate a python function from the reference under point.
pygen-generate-static-functionC-c C-= sGenerate a static python function from the reference under point.
pygen-extract-variableC-c C-= vExtract the current region into a variable.
pygen-make-keyword-argumentC-c C-= kAdds a keyword argument to the current function.
pygen-make-sequence-argumentC-c C-= aAdds a sequence argument to the current function.
pygen-add-decorator-to-functionC-c C-= @Adds a decorator in front of the current function.
pygen-selfify-symbolN/APuts the word self. in front of the current symbol.
pygen-unselfify-symbolN/ARemoves the word self. from the current symbol (if it exists).
pygen-toggle-selfify-symbolC-c C-= .Toggles the word self in front of the current symbol.
pygen-insert-superC-c C-= uInserts 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. Pymacs is faster and more robust against syntax errors elsewhere in your code. 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.

Examples

Generating Classes & Functions

pygen-generate-class

C-c C-= c - Generate a python class from the reference under point.

./gifs/pygen-generate-class.gif

It also works on member classes.

./gifs/pygen-generate-class-self.gif

pygen-generate-function

C-c C-= f - Generate a python function from the reference under point.

./gifs/pygen-generate-function.gif

It also works on member functions.

./gifs/pygen-generate-function-self.gif

pygen-generate-static-function

C-c C-= s - Generate a static python function from the reference under point.

./gifs/pygen-generate-static-function.gif

Generating Variables

pygen-extract-variable

C-c C-= v - Extract the current region into a variable.

./gifs/pygen-extract-variable.gif

pygen-make-keyword-argument

C-c C-= k - Adds a keyword argument to the current function.

./gifs/pygen-make-keyword-argument.gif

pygen-make-sequence-argument

C-c C-= a - Adds a sequence argument to the current function.

./gifs/pygen-make-sequence-argument.gif

Automatic Decorators

pygen-add-decorator-to-function

C-c C-= @ - Adds a decorator in front of the current function.

./gifs/pygen-add-decorator-to-function.gif

Modifying the “self” keyword:

pygen-selfify-symbol

Puts the word self. in front of the current symbol.

pygen-unselfify-symbol

Removes the word self. from the current symbol (if it exists).

pygen-toggle-selfify-symbol

C-c C-= . - Toggles the self. keyword in front of the current symbol.

./gifs/pygen-toggle-selfify-symbol.gif

Dynamic Boilerplate Code Generation

pygen-insert-super

C-c C-= u - Inserts a proper call to the current method in the superclass.

./gifs/pygen-insert-super.gif

About

Python code generation in Emacs with Elpy and python-mode

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Emacs Lisp 100.0%