| | 1 | = Python 2/3 Compatibility = |
| | 2 | [[TOC]] |
| | 3 | |
| | 4 | This guideline documents coding conventions to achieve hybrid Python-2.7/Python-3.5 compatibility in Sahana. |
| | 5 | |
| | 6 | It'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 | |
| | 12 | Don't use the print statement anywhere: |
| | 13 | {{{#!python |
| | 14 | print "example" # deprecated |
| | 15 | }}} |
| | 16 | |
| | 17 | You shouldn't use the print-function either, because it can clash with uWSGI: |
| | 18 | {{{#!python |
| | 19 | print("example") # not good |
| | 20 | }}} |
| | 21 | ...but it can be tolerated in CLI scripts which don't run in the WSGI environment. |
| | 22 | |
| | 23 | Best option for debug output is to use sys.stderr.write: |
| | 24 | {{{#!python |
| | 25 | import sys |
| | 26 | sys.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 |
| | 31 | current.log.debug("example") # even better |
| | 32 | }}} |
| | 33 | |
| | 34 | === Use as-syntax for catching exceptions === |
| | 35 | |
| | 36 | When catching exceptions in a variable, don't use the comma-syntax: |
| | 37 | {{{#!python |
| | 38 | try: |
| | 39 | ... |
| | 40 | except Exception, e: # deprecated |
| | 41 | ... |
| | 42 | }}} |
| | 43 | |
| | 44 | Instead, use the as-keyword: |
| | 45 | {{{#!python |
| | 46 | try: |
| | 47 | ... |
| | 48 | except Exception as e: # new standard |
| | 49 | ... |
| | 50 | }}} |
| | 51 | |
| | 52 | === No implicit package-relative imports === |
| | 53 | |
| | 54 | Python-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 |
| | 57 | from s3datetime import s3_format_datetime # inside modules/s3, not working in Python-3 |
| | 58 | }}} |
| | 59 | |
| | 60 | Python-2.7 would search relative to the current module, but on the other hand, its supports the explicit-relative syntax as well. |
| | 61 | |
| | 62 | So we decide that only explicit paths shall be used in imports. |
| | 63 | |
| | 64 | To import a module in the same package (e.g. within s3), either use explicit-relative syntax: |
| | 65 | {{{#!python |
| | 66 | from .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 |
| | 71 | from s3.s3datetime import s3_format_datetime # inside modules/s3, acceptable alternative |
| | 72 | }}} |
| | 73 | |
| | 74 | Outside 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 |
| | 76 | from s3 import s3_format_datetime # outside modules/s3 |
| | 77 | }}} |