This page is for those of you who enjoy diving into guidelines, coding conventions, and project philosophies. If you’re looking to get started more quickly, instead check out the Contributing and Writing Drivers pages.
A major goal of Instrumental is to try to unify and simplify a lot of common, useful operations. Essential to that is a consistent and coherent interface.
- Simple, common tasks should be simple to perform
- Options should be provided to enable more complex tasks
- Documentation is essential
- Use of physical units should be standard and widespread
Simple, common tasks should be simple to perform¶
Tasks that are conceptually simple or commonly performed should be made easy. This means having sane defaults.
Options should be provided to enable more complex tasks¶
Along with sane defaults, provide options. Typically, this means providing optional parameters in functions and methods.
Documentation is essential¶
Providing Documentation can be tiring or boring, but, without it, your carefully crafted interfaces can be opaque to others (including future-you). In particular, all functions and methods should have brief summary sentences, detailed explanations, and descriptions of their parameters and return values.
This also includes providing useful error messages and warnings that the average user can actually understand and do something with.
Use of physical units should be standard and widespread¶
Units in scientific code can be a big issue. Instrumental incorporates unitful quantities using the very nice Pint package. While units are great, it can seem like extra work to start using them. Instrumental strives to use units everywhere to encourage their widespread use.
As with most Python projects, you should be keeping in mind the style suggestions in PEP8. In particular:
- Use 4 spaces per indent (not tabs!)
- Classes should be named using
- Functions and methods should be named using
- As an exception, python wrapper (e.g. cffi/ctypes) code used as a _thin_ wrapper to an underlying library may stick with its naming convention for functions/methods. (See the docs for Attocube stages for an example of this)
- Modules and packages should have short, all-lowercase names, e.g.
- Use a
_leading_underscorefor non-public functions, methods, variables, etc.
- Module-level constants are written in
Strongly consider using a plugin for your text editor (e.g. vim-flake8) to check your PEP8 compliance.
It is OK to have lines over 80 characters, though they should almost always be 100 characters or less.
- No spaces after the opening triple-quote
- One-line docstrings should be on a single line, e.g.
"""Does good stuff."""
- Multi-liners have a summary line, followed by a blank line, followed by the rest of the doc. The closing quote should be on its own line
Currently Instrumental is developed and tested using Python 2.7, with an eye towards Python 3 compatibility. The ultimate goal is to to have a code base that runs unmodified on Python 2 and 3. Moving straight to 3 would be nice, but there is still much code in the universe that has not yet been ported.
There are a number of backwards-incompatible changes that occurred, but perhaps the biggest and peskiest for Instrumental was the switchover to using unicode strings by default. This is probably the largest source of Python 3 incompatibility in the existing code.
Other notable changes include:
- All division now uses “true” division (e.g.
3/4 == 0.75). Use
//to denote integer division (e.g.
3//4 == 0)
To help alleviate these transition pains, you should use python’s built-in
__future__ module, as well as the third-party
future package. E.g.:
>>> from __future__ import division, print_function, unicode_literals >>> from past.builtins import basestring