Changes between Initial Version and Version 1 of DeveloperGuidelines/Py_2_3


Ignore:
Timestamp:
03/16/19 00:54:47 (6 years ago)
Author:
Dominic König
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DeveloperGuidelines/Py_2_3

    v1 v1  
     1= Python 2/3 Compatibility =
     2[[TOC]]
     3
     4This guideline documents coding conventions to achieve hybrid Python-2.7/Python-3.5 compatibility in Sahana.
     5
     6It's a working document that will be added to as we move towards full Python-3 compatibility.
     7
     8== Syntax ==
     9
     10=== No print statements ===
     11
     12Don't use the print statement anywhere:
     13{{{#!python
     14print "example" # deprecated
     15}}}
     16
     17You shouldn't use the print-function either, because it can clash with uWSGI:
     18{{{#!python
     19print("example") # not good
     20}}}
     21...but it can be tolerated in CLI scripts which don't run in the WSGI environment.
     22
     23Best option for debug output is to use sys.stderr.write:
     24{{{#!python
     25import sys
     26sys.stderr.write("example\n") # better
     27}}}
     28
     29...or the logger (as it can be configured globally to write to a log file instead of the system console):
     30{{{#!python
     31current.log.debug("example") # even better
     32}}}
     33
     34=== Use as-syntax for catching exceptions ===
     35
     36When catching exceptions in a variable, don't use the comma-syntax:
     37{{{#!python
     38try:
     39    ...
     40except Exception, e: # deprecated
     41    ...
     42}}}
     43
     44Instead, use the as-keyword:
     45{{{#!python
     46try:
     47    ...
     48except Exception as e: # new standard
     49    ...
     50}}}
     51
     52=== No implicit package-relative imports ===
     53
     54Python-3 does not search for modules relative to the current module in the same package - unless explicitly indicated by leading {{{.}}} or {{{..}}} in the module path.
     55
     56{{{#!python
     57from s3datetime import s3_format_datetime # inside modules/s3, not working in Python-3
     58}}}
     59
     60Python-2.7 would search relative to the current module, but on the other hand, its supports the explicit-relative syntax as well.
     61
     62So we decide that only explicit paths shall be used in imports.
     63
     64To import a module in the same package (e.g. within s3), either use explicit-relative syntax:
     65{{{#!python
     66from .s3datetime import s3_format_datetime # inside modules/s3, preferred variant
     67}}}
     68
     69...or an absolute path relative to modules (or the global python path):
     70{{{#!python
     71from s3.s3datetime import s3_format_datetime # inside modules/s3, acceptable alternative
     72}}}
     73
     74Outside of modules/s3, you should always import from the top-level of the s3 package (because the package structure may change over time):
     75{{{#!python
     76from s3 import s3_format_datetime # outside modules/s3
     77}}}