wiki:ChimeraPythonStyle

Version 6 (modified by Darren Weber, 15 years ago) ( diff )

--

Chimera Python Styles

There is specific convention for python coding style in Chimera. Developers are free to use a familiar coding style, so long as it is consistent. To follow a familar and consistent style, I've adopted the python style guidelines by Guido (see PEP8).

The style is 4 spaces for each indentation level (no tabs). It can be coded into a .vimrc like so:

" python settings
autocmd BufRead,BufNewFile *.py set ft=python ai sw=4 ts=4 sta et fo=croql foldmethod=indent

It's also possible to insert a mode line into the first line of a python file to override any editor settings. The first line of the file might contain something like this (which should work for both emacs and vim):

# -*- coding: utf-8; mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=python:et:sw=4:ts=4:sts=4:fdm=indent:fo+=croql

Note that the encoding (coding) is utf-8 above, which is recommended for python 3.x, while python 2.x recommends ISO-8859-1, e.g.:

# -*- coding: ISO-8859-1; mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=ISO-8859-1:ft=python:et:sw=4:ts=4:sts=4

For emacs, here's an interesting blog on useful python tools: http://jasonmbaker.com/7-tools-for-working-with-python-in-emacs-and.

PEP8 Snippets

Indentation

    Use 4 spaces per indentation level.

    For really old code that you don't want to mess up, you can continue to
    use 8-space tabs.

  Tabs or Spaces?

    Never mix tabs and spaces.

    The most popular way of indenting Python is with spaces only.  The
    second-most popular way is with tabs only.  Code indented with a mixture
    of tabs and spaces should be converted to using spaces exclusively.  When
    invoking the Python command line interpreter with the -t option, it issues
    warnings about code that illegally mixes tabs and spaces.  When using -tt
    these warnings become errors.  These options are highly recommended!

    For new projects, spaces-only are strongly recommended over tabs.  Most
    editors have features that make this easy to do.

...

Encodings (PEP 263)

    Code in the core Python distribution should aways use the ASCII or
    Latin-1 encoding (a.k.a. ISO-8859-1).  For Python 3.0 and beyond,
    UTF-8 is preferred over Latin-1, see PEP 3120.

Note: See TracWiki for help on using the wiki.