github.com/hoffmann Peter Hoffmann on Stackoverflow @peterhoffmann on twitter Peter Hoffmann on Facebook Contact me per email Subscribe to Atom Feed

Peter Hoffmann

Software Engineer
prev page next page

Python live templates for PyCharm

Posted on November 26, 2010
#python

PyCharm lets you define live templates that expand a word into a snippet of code with some input fields. They work similar to textmate snippets and snipmate for vim.

You can either add your on snippets via File/Settings/Live Templates or select a region and add the text with Tools/Save as Live Template.

Live templates are stored in the following location:

Windows: <your home directory>\.<productname><versionnumber>\config\templates
Linux: ~\.<product name><version number>\config\templates
MacOS: ~/Library/Preferences/<product name><version number>/templates

If you have not defined any user templates yet, you can copy the user.xml to the templates location, otherwise you have to merge the files or add the templates by hand. You can get the templates from github/hoffmann/PyCharm-Python-Templates. Feel free to fork and add some useful templates.

class
Python Class Template
class $class$($object$):
    """$cls_doc$"""
    
    def __init__(self, $args$):
        """Constructor for $class$"""
        $END$
    

init
init function
def __init__(self, $args$):
    """$doc$"""
    $END$
    
def
function definition
def $fnname$($args$):
    """$docstring$
    
    Args:
        $args$
       
    Returns:
        $return_value$
    """
    $END$
se
set instance variable
self.$var$ = $var$
$END$
from
from module import name
from $module$ import $name$
$END$
pf
print formatted string
print "$string$".format($args$)
$END$
prop
property
$property$ = property(get_$property$, set_$property$)
$END$
main
if __name__ == "__main__":
def main(argv):
    if argv is None:
        argv = sys.argv
    $END$

if __name__ == "__main__":
    main()