Dan_Patterson

Code Formatting... the basics++

Blog Post created by Dan_Patterson Champion on Aug 14, 2016

Code formatting tips

 

Updated - 2017/06/27  Added another reference and some editing.

 

This topic has been covered by others as well...

 

We all agree the Geonet code editor is horrible... but it has been updated.

Here are some other tips.

 

To begin... introduction or review

  • don't try to post code while you are responding to a thread in your inbox
  • access the More button, from the main thread's title... to do this:
    • click on the main thread's title
    • now you should see it... you may proceed

 

Step 1... select ... More ... then ... Syntax highlighter
  • Go to the question and press Reply ...
  • Select the Advanced editor if needed (or ...),  then select

If you can't see it, you didn't select it

 

 More...Syntax highlighter ,

 

Your code can now be pasted in and highlighted with the language of your choice .........

Your code should be highlighted something like this ............

--- Python -----------------------------

import numpy as np
a = np.arange(5)
print("Sample results:... {}".format(a))

--------------------------------------------

Now the above example gives you the language syntax highlighting that you are familiar with..

Alternatives include just using the HTML/XML option

-----HTML/XML ---------------------

# just use the HTML/XML option.. syntax colors will be removed
import numpy as np
a = np.arange(5)
print("simple format style {}".format(a))
simple format style [0 1 2 3 4]

--------------------------------------------

 

NOTE:   you can only edit code within this box and not from outside!

 

 

 

Script editing tipscont'd

HTML editing tips:....

  • You can get into the html editor for fine-tuning, but it can get ugly for the uninitiated.
  • Comments get deleted ... this not a full editor under your control
  • If you have lots of text to enter, do it first then enter and format code
  • If editing refresh is slow, use the HTML editor or you will have retired before it completes.
  • The editor seems to edit each character as you type and it gets painfully slower as the post gets bigger.
  • You can improve comments and code output by using tables like this and in the example below.

 

Here is a simple script with code and results published in columns (2 columns * 1 row).  If the contents are wider than the screen, the scroll-bar will be located at the end of the document rather than attached to each table (except for iThingys, then just use swipe).

 

Sample script using a plain format... 1920x1080px screen sizeResult 2
>>> import numpy as np
>>> a = np.arange(5)
>>> print("Sample results:... {}".format(a))
>>> # really long comment 30 |------40 | -----50 | -----60 | -----70 | ---- 80|
Sample results:... [0 1 2 3 4]
>>> # really long comment 30 |------40 | -----50 | -----60 | -----70 | ---- 80|

 

Leave space after a table so you can continue editing after the initial code insertion.

It is often hard to select the whitespace before or after a table and you may need to go to the html editor < > just above the More toggle

 

Larger script sample...

Before code tip:  try to keep your line length <70 characters

# -*- coding: UTF-8 -*-
"""
:Script:   demo.py
:Author:   Dan.Patterson@carleton.ca
:Modified: 2016-08-14
:Purpose:  none
:Functions:  help(<function name>) for help
:----------------------------
: _demo  -  This function ...
:Notes:
:References
:
"""

#---- imports, formats, constants ----

import sys
import numpy as np
from textwrap import dedent

ft = {'bool':lambda x: repr(x.astype('int32')),
      'float': '{: 0.3f}'.format}
np.set_printoptions(edgeitems=10, linewidth=80, precision=2, suppress=True,
                    threshold=100, formatter=ft)
script = sys.argv[0]

#---- functions ----

def _demo():
    """  
    :Requires:
    :--------
    :
    :Returns:
    :-------
    :
    """

    return None

#----------------------
if __name__ == "__main__":
    """Main section...   """
    #print("Script... {}".format(script))
    _demo()

 

Some space for editing after should be left since positioning the cursor is difficult after the fact.

 

Output options

 

  • You can paste text and graphics with a table column.
  • You can format a column to a maximum pixel size.

 

Sample output with a graph

Option 0: 1000 points
[[ 2. 2.]
[ 3. 3.]] extents
.... snip
Time results: 1.280e-05 s, for 1000 repeats

point_in_polygon.png

point_in_polygon.png

 

So there has been some improvement. 

Again...

You just have to remember that to edit code...

you have to go back to the syntax highlighter.

You can't edit directly on the page.

Outcomes