Shortcuts

Code Style Guide

Basic Code Style Rules

In DI-engine, we follow the following basic code specifications:

  • The file name is always named with lowercase letters, numbers and underscores, such as my_policy.py.

  • For class, the Camelcase Naming Convention which is started with capital letters shall be adopted, such as MyClass. In addition, for internal classes, you can use additional underscores at the beginning, for example _InnerClass.

  • All functions and methods are named with lowercase letters, numbers and underscores, such as my_function and my_method.

  • For variables, they are all named with lowercase letters, numbers and underscores, such as my_var.

  • For methods and variables belonging to a class, use a single underscore to express the protected inheritance relationship, such as _protected_val. Use two underscores to express a private inheritance relationship, for example __private_val.

  • For the naming of method parameters, if it is an instance method, the first parameter should be named self. If it is a class rule, the first parameter should be named cls. Please use *args for the variable length parameter of the list, and **kwargs for the key value parameter.

  • When naming variables, if the name conflicts with reserved keywords, native classes, etc., please underline at the end to avoid unexpected effects, such as type_.

yapf

For yapf, we can use existing Makefile for one-click fix

make format

Considering the large scale of the whole project and the large number of files, you can use the following commands to check the code design of the source code files in a specific path

make format RANGE_DIR=./ding/xxx

In this project, we use the yapf config code specification configuration based on PEP8. For details about the configuration, you can refer to the description on the Github homepage. PEP8 is the code style configuration officially recommended by Python. Paying attention to code style can improve the readability of the code and minimize unintended behavior.

In addition, yapf can also integrate with pycharm through the plug-in yapf pycharm:

flake8

For flake8, we can use the existing Makefile to check the code design

make flake_check

Considering the large scale of the whole project and the large number of files, you can use the following commands to check the code design of the source code files in a specific path

make flake_check RANGE_DIR=./ding/xxx

In this project, we use flake8 code design specification configuration based on pep8. For details of configuration, please refer to the description of flake8 official documents. PEP8 is the code style configuration officially recommended by python. Paying attention to the code style can improve the readability of the code and minimize the behavior that does not meet the expectations.

Reasonable Import Order

Considering the PEP8 standard and usage habits, we recommend that users use the following import order:

  • Standard library imports

  • Related third party imports

  • Local application/library specific imports

  • Relative imports

e.g.

from xx import xx
import xx
import ding.xx
from .xx
Read the Docs v: latest
Versions
latest
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.