Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have automated headers for python files

Tags:

python

vim

A proper header format in python is described here .

Using either VIM or a shell script, I would like to have the usual metadata (like __author__, __authors__, __contact__, __copyright__, __license__, __deprecated__, __date__ and __version__) added to the file header. SVN keywords would also be nice. Adding it to new files is the most relevant. Adding it to existing files is a bonus.

Ruslan's Blog has a solution for Emacs. But I was unable to find a solution for Python.

Where has this been done for python without Emacs? VIM can copy text from one file to another like so , but maybe there is a nicer way.

like image 555
Hauke Avatar asked Jun 01 '11 12:06

Hauke


People also ask

How do you program a header in Python?

You can create headings by starting and ending a line with up to five equal signs. The heading text is between those markers, separated by a single space.

How do I include a header file in Python?

The header files are typically installed with Python. On Unix, these are located in the directories prefix/include/pythonversion/ and exec_prefix/include/pythonversion/ , where prefix and exec_prefix are defined by the corresponding parameters to Python's configure script and version is '%d. %d' % sys.

Are there headers in Python?

Practical Data Science using PythonHeaders contain protocol specific information that appear at the beginning of the raw message that is sent over TCP connection. The body of the message is separated from headers using a blank line.

What is the common header format of Python files?

The first line of each file shoud be #!/usr/bin/env python. This makes it possible to run the file as a script invoking the interpreter implicitly. Next should be the docstring with a description. All code, including import statements, should follow the docstring.


1 Answers

I highly recommend the snipMate plugin. You can easily add new snips per file type, and they're triggered by just typing a keyword and hitting tab, so for example you could just hit

header<TAB>

and all the fields would be added, and you can easily tab through any that require being filled out on a per-file basis. There is even already a snippet called docs in the built-in python snippets (vimfiles/snippets/python.snippets) that looks like it fills out similar metadata for docstrings which you could easily modify for your purposes, here it is as an example of the snip format:

# Module Docstring
snippet docs
    '''
    File: ${1:`Filename('$1.py', 'foo.py')`}
    Author: ${2:`g:snips_author`}
    Description: ${3}
    '''

Dynamic entries are supported using backticks in your snip (Filename and g:snips_author as the default for tabbable entries 1 and 2 above). The date could be added with:

`system("date +%Y-%m-%d")`

(from the snipMate help doc).

like image 138
actionshrimp Avatar answered Sep 21 '22 08:09

actionshrimp