Skip to the content.

Python

The following readme describes the coding conventions especially for Python.

use Python 3.6+

Formatting

Packages & Modules

Interfaces & Abstract Classes

Classes

Methods & Functions

Instance Variables

Constants

Public Variables (e.g.: flask-http-server model instance)

Function & Class spacing

Unit Tests

We use unittest for unit tests.

Example unit test class:

import unittest


class FooTest(unittest.TestCase):
    def test_bar(self):
        # given
        foo = Foo()
        # when
        res = foo.bar()
        # then
        self.assertEqual(res, True)

PyPi

Documentation Example

# Set the run environment to development.
PYTHON_ENV = "development"


class Foo:
    """
    The awesome example class.

    :param world: This is where you are.
    """

    def __init__(self, world: str = "world"):
        self.hello = world

    def print(self, name: str) -> str:
        """
        Builds a "Hello world `name`" string and returns it.

        :param name: Some content that should stand behind the world.
        :return: String with the formatted result.
        """
        return f"Hello {self.hello} to {name}"

    @staticmethod
    def bar():
        """
        Returns the perfect result.

        :return: True.
        """
        return True


# Main function (this is executed only if the current py-file is directly called).
# If the py-file is imported to another py-file then the content in the statement is not executed.
if __name__ == "__main__":
    foo = Foo("mars")