|
| 1 | +Nova Style Commandments |
| 2 | +======================= |
| 3 | + |
| 4 | +Step 1: Read http://www.python.org/dev/peps/pep-0008/ |
| 5 | +Step 2: Read http://www.python.org/dev/peps/pep-0008/ again |
| 6 | +Step 3: Read on |
| 7 | + |
| 8 | +Imports |
| 9 | +------- |
| 10 | +- thou shalt not import objects, only modules |
| 11 | +- thou shalt not import more than one module per line |
| 12 | +- thou shalt not make relative imports |
| 13 | +- thou shalt organize your imports according to the following template |
| 14 | + |
| 15 | +:: |
| 16 | + # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 17 | + {{stdlib imports in human alphabetical order}} |
| 18 | + \n |
| 19 | + {{nova imports in human alphabetical order}} |
| 20 | + \n |
| 21 | + \n |
| 22 | + {{begin your code}} |
| 23 | + |
| 24 | + |
| 25 | +General |
| 26 | +------- |
| 27 | +- thou shalt put two newlines twixt toplevel code (funcs, classes, etc) |
| 28 | +- thou shalt put one newline twixt methods in classes and anywhere else |
| 29 | +- thou shalt not write "except:", use "except Exception:" at the very least |
| 30 | +- thou shalt include your name with TODOs as in "TODO(termie)" |
| 31 | +- thou shalt not name anything the same name as a builtin or reserved word |
| 32 | +- thou shalt not violate causality in our time cone, or else |
| 33 | + |
| 34 | + |
| 35 | +Human Alphabetical Order Examples |
| 36 | +--------------------------------- |
| 37 | +:: |
| 38 | + import httplib |
| 39 | + import logging |
| 40 | + import random |
| 41 | + import StringIO |
| 42 | + import time |
| 43 | + import unittest |
| 44 | + |
| 45 | + from nova import flags |
| 46 | + from nova import test |
| 47 | + from nova.auth import users |
| 48 | + from nova.endpoint import api |
| 49 | + from nova.endpoint import cloud |
| 50 | + |
| 51 | +Docstrings |
| 52 | +---------- |
| 53 | + """A one line docstring looks like this and ends in a period.""" |
| 54 | + |
| 55 | + |
| 56 | + """A multiline docstring has a one-line summary, less than 80 characters. |
| 57 | + |
| 58 | + Then a new paragraph after a newline that explains in more detail any |
| 59 | + general information about the function, class or method. Example usages |
| 60 | + are also great to have here if it is a complex class for function. After |
| 61 | + you have finished your descriptions add an extra newline and close the |
| 62 | + quotations. |
| 63 | + |
| 64 | + When writing the docstring for a class, an extra line should be placed |
| 65 | + after the closing quotations. For more in-depth explanations for these |
| 66 | + decisions see http://www.python.org/dev/peps/pep-0257/ |
| 67 | + |
| 68 | + If you are going to describe parameters and return values, use Sphinx, the |
| 69 | + appropriate syntax is as follows. |
| 70 | + |
| 71 | + :param foo: the foo parameter |
| 72 | + :param bar: the bar parameter |
| 73 | + :returns: description of the return value |
| 74 | + |
| 75 | + """ |
| 76 | + |
| 77 | +Text encoding |
| 78 | +---------- |
| 79 | +- All text within python code should be of type 'unicode'. |
| 80 | + |
| 81 | + WRONG: |
| 82 | + |
| 83 | + >>> s = 'foo' |
| 84 | + >>> s |
| 85 | + 'foo' |
| 86 | + >>> type(s) |
| 87 | + <type 'str'> |
| 88 | + |
| 89 | + RIGHT: |
| 90 | + |
| 91 | + >>> u = u'foo' |
| 92 | + >>> u |
| 93 | + u'foo' |
| 94 | + >>> type(u) |
| 95 | + <type 'unicode'> |
| 96 | + |
| 97 | +- Transitions between internal unicode and external strings should always |
| 98 | + be immediately and explicitly encoded or decoded. |
| 99 | + |
| 100 | +- All external text that is not explicitly encoded (database storage, |
| 101 | + commandline arguments, etc.) should be presumed to be encoded as utf-8. |
| 102 | + |
| 103 | + WRONG: |
| 104 | + |
| 105 | + mystring = infile.readline() |
| 106 | + myreturnstring = do_some_magic_with(mystring) |
| 107 | + outfile.write(myreturnstring) |
| 108 | + |
| 109 | + RIGHT: |
| 110 | + |
| 111 | + mystring = infile.readline() |
| 112 | + mytext = s.decode('utf-8') |
| 113 | + returntext = do_some_magic_with(mytext) |
| 114 | + returnstring = returntext.encode('utf-8') |
| 115 | + outfile.write(returnstring) |
0 commit comments