User:ArielGlenn/Sandbox

From mediawiki.org

Figuring out some standard formatting for code examples for dumps (and other) knowledge sharing sessions.

Here's a first sample...

 

This is an example of how not to exit from a python script.

You can see that the author has carefully kept to python style guidelines as far as use of indentation and blank lines, even going so far as to avoid extraneous code in __main__ to avoid pylint errors.

Nonetheless, blah blah blah.

 








import os
import sys


# This is an inline comment
# This is a very long inline comment intended to overrun the container, so that we can see the code block bleeding a little bit into the left column, which should not happen. Why does it though?

def do_main():
    # entry point
    print("This is only a test.")
    # because we are really C programmers:
    sys.exit(0)


# um wtf is that above, why is it moved to a separate line for highlighting??

 

This trick avoids pylint whines about how the constant you were going to define in __main__ doesn't have an approved uppercase name etc. Plus it's polite behavior to have all of your functionality in methods and invoke the ones you want out of __main__ if the script is executed as standalone.

 



# 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123
# the above is max line length in my python code
if __name__ == '__main__':
    do_main()