Giter Club home page Giter Club logo

pygen's Introduction

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.

Introduction

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

Installation

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)

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. 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.

Commands

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

CommandKeychordDescription
pygen-generate-classC-c g cGenerate a python class from the reference under point.
pygen-generate-functionC-c g fGenerate a python function from the reference under point.
pygen-generate-static-functionC-c g sGenerate a static python function from the reference under point.
pygen-extract-variableC-c g vExtract the current region into a variable.
pygen-make-keyword-argumentC-c g kAdds a keyword argument to the current function.
pygen-make-sequence-argumentC-c g aAdds a sequence argument to the current function.
pygen-add-decorator-to-functionC-c g @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 g .Toggles the word self in front of the current symbol.
pygen-insert-superC-c g 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. 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.

Examples

Generating Classes & Functions

pygen-generate-class

C-c g 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 g 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 g s - Generate a static python function from the reference under point.

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

Generating Variables

pygen-extract-variable

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

./gifs/pygen-extract-variable.gif

pygen-make-keyword-argument

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

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

pygen-make-sequence-argument

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

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

Automatic Decorators

pygen-add-decorator-to-function

C-c g @ - 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 g . - 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 g u - Inserts a proper call to the current method in the superclass.

./gifs/pygen-insert-super.gif

pygen's People

Contributors

andreas-roehler avatar jackcrawley avatar syohex avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

pygen's Issues

My related project (change method signatures, add/rm decorators, etc)

Hi !

I'm just discovering your project while browsing the melpa PR, so congrats, that looks really useful, come tell on reddit as soon as this is accepted in melpa ! and I figured I should share some utils of mine: https://gitlab.com/vindarel/redbaron4emacs Our projects are complementary but do things in common, like adding a decorator (I suggest a list of decorators already used in the file, it's a bit quicker, and also if it is "classmethod" I remove an eventual "self".). My initial goal was to change method signatures, so I have functions to add, rename, remove a method argument, rename the current def, etc, while trying (not very well) to refactor inside a def or in the project when needed/asked.

Also, I wanted to have a solid foundation to manipulate python code, so I use the redbaron lib (a bit, not always, but I believe it has great potential).

Lastly, I rely a lot on an Hydra to expose all my commands, I suggest you think about it. That way, I may very well add a key to call pygen: it's complementary.

Thanks for your attention !

`pygen-insert-super` uses wrong arg name for `@classmethod` / non `self` args

Awesome package, I've been looking for something like this!

One thing I've found is that pygen-insert-super assumes that the 1st arg is always self. Convention is to call the first arg self and for static methods cls or _class, but that's just convention, it could be called anything.

It'd be great if pygen-insert-super grabbed the name of the first argument from the method definition. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.