Python Programming Engineering Guidelines

Principles:

Install executable package into a directory (example: /apps/my-app) instead of site-packages

Style

Script Python
1. Keep all statements, functions, classes in one module (your script !) . No relative imports is allowed.
2. function main() is not needed.

Package Python (package name maintained)
1. Keep the overall package layout flat. The package name is the only entry point of all module imports, use absolute imports only, sub-packages are generally accepted but not encouraged.
example:
from mypackage.mymodule import myfunction
2. Expand sys.path in the module that contains main()

Package Python (package name not maintained)
1. Keep the overall package layout flat. Use relative imports (import from the same folder) only, sub-packages are generally accepted but not encouraged.
example:
from mymodule import xxx

Recommendations

  1. Coding scripts (temporary):
    Put all definitions, variables, classes into one script; Or split them into several scripts if really needed (using relative import)

  2. Coding non-trivial packages:
    Split definitions, variables, classes into separate scripts; using absolute import, expand sys.path if needed.

Problems:

  • Interfaces cannot have typed parameters (dynamic typing nature), which raises troubles and confusions for the users (partial due to the lack of IDE support).
  • Interfaces with a generics nature in dynamic typing system accepts arguments with different primitive types (root types). This complicates interface calls.

Leave a Reply

Your email address will not be published. Required fields are marked *