What's the best way to obtain code from a post?

07-26-2016 12:30 PM
Occasional Contributor III

There are all sorts of posts on how to properly format code in messages, like https://community.esri.com/people/curtvprice/blog/2014/09/25/posting-code-blocks-in-the-new-geonet

But, how do I copy the code to use on my system?  The code contains unwanted characters when I highlight/copy and paste it into a text editor.  For example, see the useful code at https://community.esri.com/thread/180215#comment-623464 which also contains long wrap-around lines.


0 Kudos
9 Replies
MVP Honored Contributor


That's a great question. I wonder if you can copy and paste the code into a code editor and have code editor parse out the unwanted things (especially if it's all the same unwanted thing). Or maybe paste it into something MS Word and do a find and replace for "..." or so. Either way, I bet some people here have a good answer.

MVP Legendary Contributor

This example

import numpy as np
from numpy.lib.stride_tricks import as_strided
from functools import wraps
from textwrap import dedent

looks like his

<pre class="jive_text_macro jive_macro_code " jivemacro="code" ___default_attr="c#" data-renderedposition="34_8_916_96" >

<p>import numpy as np</p>

<p>from numpy.lib.stride_tricks import as_strided</p>

<p>from functools import wraps</p>

<p>from textwrap import dedent</p>


<p>                    suppress=True,threshold=100)</p>



So I just use Notepad++ or whatever and replace the <p>, </p> etc stuff

Actually I have a script somewhere that does similar stuff.

As for wrap around text and multilines, you may have to do some text editing, but be careful of trying to strip too much away

MVP Esteemed Contributor

Or, if you had a python IDE like Wing 101 Python IDE for Python Developers - Wingware Python IDE  or any number of others (and NotePad++ probably has this), you can cut and paste and after replacing things like the ... and >>> ,  they usually have a conversion for the spaces to tabs if you like.  Since python is very sensitive to spacing, getting this correct and set right away will hopefully cause less headaches in the end.

BTW, I copied Darren Wiens 's copy you mentioned and it took less then a minute to get the formatting cleaned up.

edit: just ran you script Darren...pretty nice - may use that at some point

MVP Legendary Contributor

I am a little confused here. When I copy from a Jive/GeoNet code block I do not get any extra html formatting tag or unwanted stuff.

0 Kudos
MVP Honored Contributor


Take a look at Darren's reply here:


There are ... before each line. These would get picked up in a copy/paste

0 Kudos
MVP Legendary Contributor

It sometimes depends where you past it Robert, some destinations hide and interpret the paste as valid html code and hence it isn't visible.  Notepad++ has a whole slew of conversion options that does the automagic stripping, and if that doesn't work, then it has some pretty powerful text editing capabilities.  And you can even run code snippets in several languages, if one gets lazy

0 Kudos
MVP Honored Contributor

I just happened to be working on this very code block and noticed the exact same thing. If you copy it into the Python window in ArcMap (where it was made), it goes in smoothly, but if you try to edit it in an IDE, the ... are problematic. As Adrian suggested, I replaced all '... ' with '' (blank) to remove them.

Here's the updated code, but I'm not sure what the best code use advice would be.

lines = 'a_line' # line feature class
sr = arcpy.Describe(lines).spatialReference # spatial reference
new_polys = []
new_scratch_polys = []
new_lines = []
buffers = []
max_buff = 50 # buffer at line start
min_buff = 10 # buffer at line end
with arcpy.da.SearchCursor(lines,'SHAPE@',spatial_reference=sr) as cursor: # loop through lines
  for row in cursor:
      prev_buff = None
      for part in row[0]:
          for pnt in part:
              cur_rad = ((max_buff-min_buff) * (1-row[0].measureOnLine(pnt,True))) + min_buff # calc current proportional buffer size
              cur_buff = arcpy.PointGeometry(pnt,sr).buffer(cur_rad) # create buffer geometry
              new_poly_array = arcpy.Array()
              if prev_buff: # if at least second vertex
                  cur_poly = cur_poly.union(cur_buff) # add buffer to cumulative geometry
                  c1 = prev_pnt # previous point center
                  c2 = pnt
                  r1 = prev_rad
                  r2 = cur_rad
                  dx = c2.X - c1.X
                  dy = c2.Y - c1.Y
                  dr = r2 - r1
                  d = math.sqrt(math.pow(dx,2)+math.pow(dy,2))
                  X = dx/d
                  Y = dy/d
                  R = dr/d
                  ks = [1,-1]
                  for k in ks:
                      a = (R*X)-((k*Y)*math.sqrt(1-math.pow(R,2)))
                      b = -((R*Y)+((k*X)*math.sqrt(1-math.pow(R,2))))
                      c = r1 - (a*c1.X) + (b*c1.Y)
                      tan_x1 = c2.X-10000
                      tan_x2 = c2.X+10000
                      tan_y1 = -((((-a)*(tan_x1))-c)/(b))
                      tan_y2 = -((((-a)*(tan_x2))-c)/(b))
                      new_line = arcpy.Polyline(arcpy.Array([[arcpy.Point(tan_x1,tan_y1),arcpy.Point(tan_x2,tan_y2)]]),sr) # create line
                      if k > 0:
                          new_poly_array.add(new_line.intersect(prev_buff,1).centroid) # find intersections
                          new_poly_array.add(new_line.intersect(cur_buff,1).centroid) # find intersections
                          new_poly_array.add(new_line.intersect(cur_buff,1).centroid) # find intersections
                          new_poly_array.add(new_line.intersect(prev_buff,1).centroid) # find intersections
                  new_rect = arcpy.Polygon(new_poly_array,sr) # make polygon from intersections
                  new_scratch_polys.append(new_rect) # add rectangle to scratch geometry
                  cur_poly = cur_poly.union(new_rect) # add rectangle to cumulative geometry
                  cur_poly = cur_buff # add buffer to cumulative geometry
              prev_rad = cur_rad #remember values
              prev_pnt = pnt
              prev_buff = cur_buff
          new_polys.append(cur_poly) # add cumulative geometry to list
arcpy.CopyFeatures_management(buffers,r'in_memory\buffers') # write outputs
Occasional Contributor III

Thanks Darren, this code copied and pasted correctly formatted without unwanted characters.

As others have pointed out a search and replace will work, but this seemed an unnecessary and potentially problematic step.

So, the ease of copying posted code depends on how it was posted more than the message system.  I'll assume my question was answered.

Thanks everybody.

MVP Honored Contributor

Code like this takes a few hours to put together. I hope I can count on the end user to spend a couple minutes making it fit their system.