Page tree
Skip to end of metadata
Go to start of metadata

Zubax coding conventions for Python inherit the PEP8 almost verbatim, with the following list of minor deviations and a small set of additional requirements.

Deviations from PEP8

The maximum length of line is set to 120 characters. This is unlike PEP8, where the limit is set to 79 characters.

Trailing whitespaces are not allowed. PEP8 provides a weak recommendation to avoid whitespaces, too.

Usage of tabs is not allowed. If you're porting an existing codebase, replace all tabs with 4 spaces. PEP8 recommends to retain tabs when working with an existing codebase.

Formatting

Always provide a blank line after the end of every indented block, unless the following code is related to the indented block. For example, there should be an empty space after the end of an if block, unless it is followed by its else block.

def foo():
    try:
        if a == b:
            bar()
        else:
            baz()
        
        if a == c:
            booz()
    except ValueError as ex:
        print(ex)
        return False
    else:
        return True

Lines

Every text file should contain exactly one empty line at the end.

Allowed end-of-line character sequence is Unix-style (\n, LF). Windows or Mac line endings are not allowed.

Language standards

All new code should be written for Python 3.5 or newer.

Support for older versions of Python is optional. Support for Python 2.x is discouraged.

Recommended IDE

The recommended IDE for Python development is JetBrains PyCharm IDE.

Documentation

All public functions, classes, and variables should be documented, unless they are simple enough for their purpose to be evident.

All public functions should be type-annotated.

An example that shows the adopted documentation convention is provided below.

def write(self, data, timeout=None) -> int:
    """
    This method can be invoked concurrently from multiple threads, even if there are other threads blocked on
    any of the reading methods.
    :param data:        data, bytes or str
    :param timeout:     timeout in seconds, None for infinity
    :return:            number of bytes written - always either len(data) or 0 on timeout
    """
    if isinstance(data, str):
        data = data.encode('utf8')

    if not isinstance(data, bytes):
        raise ValueError('Invalid data type: %r' % type(data))

    try:
        if len(data) > 0:
            self._txq.put(data, timeout=timeout)
    except queue.Full:
        return 0
    else:
        return len(data)