4 This document describes the main coding rules in use in the plat/al codebase.
9 Plat/al mostly follows the :pep:8 rules and `Django's coding style`_.
11 .. _Django's coding style: https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/coding-style/
15 - Lines are limited to 120 characters
16 - Indent with 4 spaces
21 - Use ``TODO`` and ``FIXME`` annotations with the name of the assignee::
23 # TODO(x2006barrois): make the spam more explicit
24 # FIXME(x2008gelineau): this may fry the eggs
26 - Use ``XXX`` for things we can't improve, such as limitations in Python::
28 # XXX: OrderedDict does not exist before Python 2.7
34 **Do not use** ``import *``.
36 When importing from the same package, use relative imports.
37 Otherwise, use absolute imports:
39 .. code-block:: python
43 # Absolute import of xnet.blih
46 # Relative import of xnet.foo.baz
50 Imports should be ordered from most general to most local:
52 #. Standard lib imports
54 #. Third party lib imports
56 #. Current app imports
62 Exceptions are quite powerful, but should be used properly.
64 Each module defining custom exceptions should define a base class for its
65 exceptions; other exceptions from the module must then inherit from that base
66 exception. This makes it much easier to catch module-specific exceptions:
69 .. code-block:: python
73 class FooError(Exception):
74 """Base class for exceptions from blah.foo."""
76 class SpecificException(FooError):
77 """A specific exception."""
80 """Demo function for exception catching."""
83 except SpecificException as e:
84 handle_specific_exception(e)
91 except foo.FooError as e:
92 handle_generic_foo_exception(e)
95 Only catch the errors that you expect. Let the other errors propagate. Do not catch an exception then raise another one, as this hides the true cause of the error.
97 Avoid generic excepts, or worse, naked excepts:
99 - ``except Exception:`` will catch import or syntax exceptions
100 - ``except:`` and ``except BaseException:`` will catch all exceptions, including ``KeyboardInterrupt``, thus preventing ``^C``.