Monday 9 June 2014

Testing recursion limits python mode in the processing ide

Python is well known for not dealing with recursion too well, it seems python mode in the processing mode is even worse:-

   1 """
   2 spiral.pyde python mode sketch
   3 features fairly extreme recursion for
   4 python hence we try increase limit
   5 but it doesn't work so we capture error
   6 """
   7 MAX_RECURSION = 1000
   8 
   9 
  10 def setup():
  11     """
  12     processing setup
  13     """
  14     size(400, 400)
  15     translate(100, 330)
  16     rotate(0.3)
  17     fill(255, 0, 0, 0)
  18     background(0)
  19     noStroke()
  20     smooth()
  21     fill(255, 0, 0, 20)  # transparency makes for almost '3d' look
  22     import sys         # increase recursion limit or sketch will fail
  23     sys.setrecursionlimit(MAX_RECURSION)
  24     try:               # handle potential index error
  25         shell(-0.008, 1.5, 25)
  26     except RuntimeError:
  27         print "Sketch exceeds set recursion limit"
  28     else:
  29         pass  # Sketch finishes before recursion limit is reached
  30 
  31 
  32 def shell(rot, disp, sz):
  33     """
  34     Recursive shell shape limited by sz
  35     """
  36     REDUCE = 0.999
  37     MIN_SIZE = 0.8   # about right for processing
  38     if (sz > MIN_SIZE):
  39         sz *= REDUCE
  40         disp *= REDUCE
  41         translate(disp, 0)
  42         rotate(rot)
  43         ellipse(disp, 0, sz, sz)
  44         return shell(rot, disp, sz)  # recursive call
  45     else:
  46         return  # break recursive loop on size
  47 


To get python syntax highlighting in jEdit with the *.pyde file add the following to the mode catalog:-
   1 <?xml version="1.0"?>
   2 <!DOCTYPE MODES SYSTEM "catalog.dtd">
   3 
   4 <MODES>
   5 
   6 <!-- Add lines like the following, one for each edit mode you add: -->
   7 <!-- <MODE NAME="foo" FILE="foo.xml" FILE_NAME_GLOB="*.foo" /> -->
   8 
   9 <MODE NAME="processing.py" FILE="python.xml" FILE_NAME_GLOB="*.pyde" />
  10 </MODES>

The latest way to import a java library processing python mode

What you need to do is place the library folder (from vanilla processing) in a libraries folder (under PythonMode), on linux this is sketchbook/modes/PythonMode/libraries/peasy/library/peasycam.jar. The latest python processing way of calling libary is as for vanilla processing to use the processing menu add library method, which adds the following to your sketch add_library('peasycam')







I have since updated many more of my processing.py examples to run using the latest python mode in the processing ide.