Files
82d80e529916788c6f9354146e8842d2adb9bd29
nova /HACKING

166 lines
4.6 KiB
Plaintext
Raw Normal View History

2010年05月27日 23:05:26 -07:00
Nova Style Commandments
=======================
Step 1: Read http://www.python.org/dev/peps/pep-0008/
Step 2: Read http://www.python.org/dev/peps/pep-0008/ again
Step 3: Read on
2011年07月29日 12:49:48 -04:00
General
-------
2011年07月29日 12:54:55 -04:00
- Put two newlines between top-level code (funcs, classes, etc)
- Put one newline between methods in classes and anywhere else
2011年07月29日 12:49:48 -04:00
- Do not write "except:", use "except Exception:" at the very least
2011年07月29日 12:54:55 -04:00
- Include your name with TODOs as in "#TODO(termie)"
- Do not name anything the same name as a built-in or reserved word
2011年07月29日 12:49:48 -04:00
2010年05月27日 23:05:26 -07:00
Imports
-------
2011年07月29日 12:49:48 -04:00
- Do not import objects, only modules
- Do not import more than one module per line
- Do not make relative imports
- Order your imports by the full module path
- Organize your imports according to the following template
2010年05月27日 23:05:26 -07:00
::
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2011年07月29日 15:42:10 -04:00
{{stdlib imports in human alphabetical order}}
2010年05月27日 23:05:26 -07:00
\n
{{third-party lib imports in human alphabetical order}}
\n
2011年07月29日 15:42:10 -04:00
{{nova imports in human alphabetical order}}
2010年05月27日 23:05:26 -07:00
\n
\n
{{begin your code}}
Human Alphabetical Order Examples
---------------------------------
::
import httplib
import logging
import random
import StringIO
import time
import unittest
import webob.exc
2011年07月29日 12:21:46 -04:00
import nova.api.ec2
from nova.api import openstack
2010年05月27日 23:05:26 -07:00
from nova.auth import users
2011年07月29日 12:21:46 -04:00
import nova.flags
2010年05月27日 23:05:26 -07:00
from nova.endpoint import cloud
2011年07月29日 12:21:46 -04:00
from nova import test
2011年07月29日 12:44:11 -04:00
Docstrings
----------
2011年03月28日 10:46:02 -07:00
"""A one line docstring looks like this and ends in a period."""
2011年03月28日 10:46:02 -07:00
"""A multiline docstring has a one-line summary, less than 80 characters.
Then a new paragraph after a newline that explains in more detail any
general information about the function, class or method. Example usages
2011年03月29日 11:15:16 -07:00
are also great to have here if it is a complex class for function. After
you have finished your descriptions add an extra newline and close the
quotations.
2011年02月12日 13:30:31 -08:00
When writing the docstring for a class, an extra line should be placed
after the closing quotations. For more in-depth explanations for these
decisions see http://www.python.org/dev/peps/pep-0257/
2011年03月28日 10:46:02 -07:00
If you are going to describe parameters and return values, use Sphinx, the
appropriate syntax is as follows.
:param foo: the foo parameter
:param bar: the bar parameter
2011年07月29日 12:21:46 -04:00
:returns: return_type -- description of the return value
2011年07月29日 15:26:14 -04:00
:returns: description of the return value
2011年07月29日 12:21:46 -04:00
:raises: AttributeError, KeyError
"""
2011年07月29日 12:21:46 -04:00
2011年07月29日 12:44:11 -04:00
2011年07月29日 12:21:46 -04:00
Dictionaries/Lists
------------------
If a dictionary (dict) or list object is longer than 80 characters, its
items should be split with newlines. Embedded iterables should have their
items indented. Additionally, the last item in the dictionary should have
a trailing comma. This increases readability and simplifies future diffs.
Example:
my_dictionary = {
"image": {
"name": "Just a Snapshot",
"size": 2749573,
"properties": {
"user_id": 12,
"arch": "x86_64",
},
"things": [
"thing_one",
"thing_two",
],
"status": "ACTIVE",
},
}
2011年07月29日 12:44:11 -04:00
Calling Methods
---------------
2011年07月29日 12:21:46 -04:00
Calls to methods 80 characters or longer should format each argument with
2011年07月29日 13:00:09 -04:00
newlines. This is not a requirement, but a guideline.
2011年07月29日 12:21:46 -04:00
unnecessarily_long_function_name('string one',
'string two',
kwarg1=constants.ACTIVE,
kwarg2=['a', 'b', 'c'])
Rather than constructing parameters inline, it is better to break things up:
list_of_strings = [
'what_a_long_string',
'not as long',
]
dict_of_numbers = {
'one': 1,
'two': 2,
'twenty four': 24,
}
object_one.call_a_method('string three',
'string four',
kwarg1=list_of_strings,
kwarg2=dict_of_numbers)
2011年07月29日 12:44:11 -04:00
2011年07月29日 13:51:49 -04:00
2011年07月29日 12:44:11 -04:00
Internationalization (i18n) Strings
2011年07月29日 15:43:23 -04:00
-----------------------------------
2011年07月29日 12:44:11 -04:00
In order to support multiple languages, we have a mechanism to support
automatic translations of exception and log strings.
Example:
msg = _("An error occurred")
raise HTTPBadRequest(explanation=msg)
If you have a variable to place within the string, first internationalize
the template string then do the replacement.
Example:
msg = _("Missing parameter: %s") % ("flavor",)
LOG.error(msg)
If you have multiple variables to place in the string, use keyword
parameters. This helps our translators reorder parameters when needed.
Example:
msg = _("The server with id %(s_id)s has no key %(m_key)s")
2011年07月29日 15:58:41 -04:00
LOG.error(msg % {"s_id": "1234", "m_key": "imageId"})