Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
cs190c:ps3_p2_lib.py [2008/01/25 11:26]
seh created
cs190c:ps3_p2_lib.py [2008/01/25 11:51] (current)
Line 3: Line 3:
  
 def read_points(filename):​ def read_points(filename):​
-    ​#try to open the file +    ​"""​ 
-    ​try: +    ​Read points from file filename.
-        ​file = open(filename, '​r'​) +
-    #if we can't, print out an error and return an empty list +
-    except IOError: +
-        print >>​stderr,​ "​Couldn'​t open file \"​%s\"​." % filename +
-        return []+
  
-    ​+    ​>>>​ read_points("​testdata0.txt"​) 
 +    [[0.0, 0.0], [1.0, -1.0], [2.0, 3.0]] 
 +    """​ 
 + 
 +    file = open(filename,​ '​r'​)
     points = []                             #​initialize our list of points     points = []                             #​initialize our list of points
  
Line 29: Line 28:
  
 def save_points(points,​ fname): def save_points(points,​ fname):
-    """​Given a list of points, ​saves them to the file named fname. These  +    """​ 
-    points can be read back in with read_points(fname)."""​+    ​Given a list of points, ​save them to the file named fname. These  
 +    points can be read back in with read_points(fname). 
 + 
 +    >>>​ save_points([[0,​0],​[1,​-1],​[2,​3]], ​"testdata0.txt"
 +    ​"""​ 
 +    ​
     file = open(fname, '​w'​)     file = open(fname, '​w'​)
     for point in points:     for point in points:
Line 40: Line 44:
 def show_points(points,​ color=(0,​1,​0)):​ def show_points(points,​ color=(0,​1,​0)):​
     """​     """​
-    Given a list of 2D or 3D points, ​draws them as spheres in the active  +    Given a list of 2D or 3D points, ​draw them as spheres in the active  
-    VPython window. ​ If no color is given, the spheres will be green."""​+    VPython window. ​ If no color is given, the spheres will be green.
  
 +    #>>>​ show_points([[0,​0],​[1,​-1],​[2,​3]])
 +    """​
 +    ​
     for point in points:     for point in points:
         sphere(pos=point,​ radius=.5, color=color)         sphere(pos=point,​ radius=.5, color=color)
Line 48: Line 55:
 def show_box(p1,​ p2, color=(0,​0,​1)):​ def show_box(p1,​ p2, color=(0,​0,​1)):​
     """​     """​
-    Given two points p1 and p2, draws a box in the active VPython window+    Given two points p1 and p2, draw a box in the active VPython window
     indicating the rectangle outlined by p1 and p2. If no color is given, ​     indicating the rectangle outlined by p1 and p2. If no color is given, ​
-    the box will be blue."""​+    the box will be blue. 
 + 
 +    #>>>​ show_box([0,​0],​ [5,5]) 
 +    ​"""​
  
     # convert the points to vectors to make math much more convenient     # convert the points to vectors to make math much more convenient
Line 63: Line 73:
  
 def random_points(n,​ d): def random_points(n,​ d):
-    from random import uniform 
     """​     """​
-    Generate a list of random ​n points in d-dimensional space. ​ For example,+    Generate a list of n random ​points in d-dimensional space. ​ For example,
     if d=2, then the points will be in 2D space (they will have two     if d=2, then the points will be in 2D space (they will have two
     coordinates). ​ The space that the points are placed in scales with n and d.     coordinates). ​ The space that the points are placed in scales with n and d.
 +
 +    >>>​ len(random_points(5,​ 3))
 +    5
     """​     """​
 +
 +    from random import uniform
  
     r = int(n**(1./​d)+1) ​               # we use this so there is roughly     r = int(n**(1./​d)+1) ​               # we use this so there is roughly
Line 83: Line 97:
         points.append(point) ​           #  add our completed point to the list         points.append(point) ​           #  add our completed point to the list
  
-    return points ​+    return points ​                      ​
  
-</​code>​ +# Self-test code if this module is run directly... 
-                      ​+
 +if __name__ == "​__main__":​ 
 +    import doctest 
 +    doctest.master = None 
 +    doctest.testmod() 
 +</​code> ​                  ​
  
 
cs190c/ps3_p2_lib.py.txt · Last modified: 2008/01/25 11:51 (external edit)
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki