From 3f588a5f8e2f9fbaf2c2329eb23a34c362d7a345 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Mon, 21 Mar 2011 22:25:27 +0000 Subject: added comments to Python samples (#2709) --- (limited to 'pysamples') diff --git a/pysamples/copy_from_heap.py b/pysamples/copy_from_heap.py index ffa8ebd..5eb7bab 100644 --- a/pysamples/copy_from_heap.py +++ b/pysamples/copy_from_heap.py @@ -21,6 +21,9 @@ # This procedure is invoked when the user-definable block on the "extras" # palette is selected. +# Usage: Import this code into a Python (user-definable) block; when +# this code is run, the FILO heap will be copied to the clipboard. + def myblock(tw, x): # second argument is ignored diff --git a/pysamples/dotted_line.py b/pysamples/dotted_line.py index b59e38f..e9c1325 100644 --- a/pysamples/dotted_line.py +++ b/pysamples/dotted_line.py @@ -124,6 +124,10 @@ # Note: See http://docs.python.org/tutorial/datastructures.html # +# Usage: Import this code into a Python (user-definable) block; when +# this code is run, the turtle will draw a dotted line of the length +# of the numeric argument block docked to the Python block. + def myblock(tw, line_length): diff --git a/pysamples/load_block.py b/pysamples/load_block.py index b42df4f..8924605 100644 --- a/pysamples/load_block.py +++ b/pysamples/load_block.py @@ -21,6 +21,15 @@ # This procedure is invoked when the user-definable block on the "extras" # palette is selected. +# Usage: Import this code into a Python (user-definable) block; when +# this code is run, the turtle will create a block at the current +# location of the turtle. The first argument to the Python block +# should be a string containing the name of the desired +# block. Arguments to the block can be passed by expanding the Python +# block to include up to two additional arguments. Note that the +# height of the block is pushed to the FILO heap and can be used to +# advance the position of the turtle when creating stacks. + def myblock(tw, blkname): diff --git a/pysamples/load_journal_entry_to_heap.py b/pysamples/load_journal_entry_to_heap.py index f587822..512de76 100644 --- a/pysamples/load_journal_entry_to_heap.py +++ b/pysamples/load_journal_entry_to_heap.py @@ -21,6 +21,11 @@ # This procedure is invoked when the user-definable block on the "extras" # palette is selected. +# Usage: Import this code into a Python (user-definable) block; when +# this code is run, the chooser will be opened for selecting a file +# from the Journal. The contents of that file will be loaded onto the +# FILO heap. + def myblock(tw, x): # ignore second argument diff --git a/pysamples/paste_to_heap.py b/pysamples/paste_to_heap.py index eccd973..cd36732 100644 --- a/pysamples/paste_to_heap.py +++ b/pysamples/paste_to_heap.py @@ -21,6 +21,10 @@ # This procedure is invoked when the user-definable block on the "extras" # palette is selected. +# Usage: Import this code into a Python (user-definable) block; when +# this code is run, the contents of the clipboard will be appended to +# the FILO heap. + def myblock(tw, x): # ignore second argument diff --git a/pysamples/push_mouse_event.py b/pysamples/push_mouse_event.py index 26301ac..154e395 100644 --- a/pysamples/push_mouse_event.py +++ b/pysamples/push_mouse_event.py @@ -22,6 +22,17 @@ # This procedure is invoked when the user-definable block on the "extras" # palette is selected. +# Usage: Import this code into a Python (user-definable) block; when +# this code is run, the current mouse status will be pushed to the +# FILO heap. If a mouse button event occurs, a y, x, and 1 are pushed +# to the heap. If no button is pressed, a y, x, 0 is pushed to the +# heap. + +# To use these data, pop the heap in a compare block to determine if a +# button has been pushed. The conditional could be use to lower or +# raise the pen, for example. After checking the button status, pop +# the x and y coordinates. + def myblock(tw, x): # ignore second argument @@ -38,4 +49,6 @@ def myblock(tw, x): # ignore second argument tw.lc.heap.append(1) # mouse event tw.mouse_flag = 0 else: + tw.lc.heap.append((tw.canvas.height / 2) - tw.mouse_y) + tw.lc.heap.append(tw.mouse_x - (tw.canvas.width / 2)) tw.lc.heap.append(0) # no mouse event diff --git a/pysamples/push_time.py b/pysamples/push_time.py index ed823ee..1945ca4 100644 --- a/pysamples/push_time.py +++ b/pysamples/push_time.py @@ -21,6 +21,11 @@ # This procedure is invoked when the user-definable block on the "extras" # palette is selected. +# Usage: Import this code into a Python (user-definable) block; when +# this code is run, the current hour, minute, and second are pushed to +# the FILO heap. To use these values, pop second, then minute, then +# hour from the FILO. + def myblock(tw, x): # ignore second argument diff --git a/pysamples/save_heap_to_journal_entry.py b/pysamples/save_heap_to_journal_entry.py index 90c46ec..e9121d4 100644 --- a/pysamples/save_heap_to_journal_entry.py +++ b/pysamples/save_heap_to_journal_entry.py @@ -21,6 +21,11 @@ # This procedure is invoked when the user-definable block on the "extras" # palette is selected. +# Usage: Import this code into a Python (user-definable) block; when +# this code is run, the contents of the FILO heap are saved to a +# Journal entry named by the value of the argument to the Python +# block. + def myblock(tw, title): diff --git a/pysamples/set_rgb.py b/pysamples/set_rgb.py index f0f70ee..c9c0f0c 100644 --- a/pysamples/set_rgb.py +++ b/pysamples/set_rgb.py @@ -22,6 +22,12 @@ # This procedure is invoked when the user-definable block on the "extras" # palette is selected and expanded to 3 arguments. +# Usage: Import this code into a Python (user-definable) block. +# First, expand the Python block to reveal three numerics arguments. +# Set these values to the desired red, green, and blue. When the code +# is run, the red, green, and blue values are used to set the pen +# color. + def myblock(tw, rgb_array): @@ -31,15 +37,8 @@ def myblock(tw, rgb_array): # ########################################################################### - def mod(x): - while x < 0: - x += 256 - while b > 255: - x -= 256 - return x - - b = mod(int(rgb_array[2])) - g = mod(int(rgb_array[1])) - r = mod(int(rgb_array[0])) - rgb = "#%02x%02x%02x" % (r, g, b) + + rgb = "#%02x%02x%02x" % ((int(rgb_array[0]) % 256), + (int(rgb_array[1]) % 256), + (int(rgb_array[2]) % 256)) tw.canvas.fgcolor = tw.canvas.cm.alloc_color(rgb) diff --git a/pysamples/sinewave.py b/pysamples/sinewave.py index f825317..54a0700 100644 --- a/pysamples/sinewave.py +++ b/pysamples/sinewave.py @@ -21,6 +21,10 @@ # This procedure is invoked when the user-definable block on the "extras" # palette is selected. +# Usage: Import this code into a Python (user-definable) block and +# pass a frequency in Hertz to the Python block. A tone will play over +# the speaker at the specified frequency. + def myblock(tw, frequency): diff --git a/pysamples/speak.py b/pysamples/speak.py index f7a1cf7..03f47bc 100644 --- a/pysamples/speak.py +++ b/pysamples/speak.py @@ -22,6 +22,11 @@ # This procedure is invoked when the user-definable block on the "extras" # palette is selected. +# Usage: Import this code into a Python (user-definable) block and +# pass a string to be read by the voice synthesizer. If a second +# argument is passed, by expanding the Python block, it is used to specify +# the pitch level of the speaker. Valid range is 0 to 99. + def myblock(tw, arg): diff --git a/pysamples/uturn.py b/pysamples/uturn.py index bba165a..3f7f623 100644 --- a/pysamples/uturn.py +++ b/pysamples/uturn.py @@ -22,6 +22,11 @@ # This procedure is invoked when the user-definable block on the "extras" # palette is selected. +# Usage: Import this code into a Python (user-definable) block; when +# it is run, a u-turn block will be added to the Turtle Palette. You +# can use the u-turn block as you would any other block. + + def myblock(tw, arg): ''' Add a uturn block to the 'turtle' palette ''' -- cgit v0.9.1