CS 151: Lab #8

Lab Exercise 8: Classes

Main course page

The purpose of this lab is to give you practice in creating your own classes. In particular, we will convert both the lsystem and the interpreter modules into classes. Ultimately, this will make it easier to build a more complex system.

If you have not already done so, take a look at the book 'The Algorithmic Beauty of Plants', which is the basis for this series of lab exercises. You can download the entire book from the algorithmic botany site

The other piece we'll be implementing this week is handling multi-rule L-systems. Adding this capability will enable you to draw more complex L-system structures.


Tasks

Most of the exercises in lab will involve building up an L-system class. This week it will be important for you to use the method names provided. The next several labs will expect the L-system and Interpreter classes to have certain methods with particular names.

  1. Create a new working folder and a new lsystem.py file. Label this one as version 2. The lsystem.py file will hold the Lsystem class and a test function for it.
  2. Begin a class called Lsystem. A class has a simple structure. It begins with the class declaration, and all methods are indented relative to the class definition, similar to a function.

    class Lsystem:

    Then we define the __init__() method (function), which is executed when a Lsystem object is created. The init method should set up the fields of a class and give them reasonable initial values. Init methods often take optional arguments. You can make an argument optional by assigning a value to it in the argument list of the function declaration. For example, the following function has two arguments, the second of which is optional with a default value of 5.

    def boo( a, b = 5 ):
        print a, " ", b
    

    The Lsystem init function should have two arguments: self, and an optional filename. If the function receives a filename, it should read Lsystem information from the file by calling the read method of self (which you'll create below), passing the filename as the argument.

    def __init__(self, filename=None):

    The Lsystem init should create two fields: base and rules. The field base should be initialized to the empty string. The field rules should be initialized to an empty list. To create a field of a class, put the prefix self. in front of the name of the field and assign something to it. The line of code below creates the field base and assigns it the empty string.

    self.base = ''

  3. Create mutator and accessor methods for the base string: setBase(self, bstr), getBase(self). The setBase function should assign bstr to the base field of self. The getBase function should return the base field of self.
  4. Create an accessor method getRule(self, index) that returns the specified single rule from the rules field of self. Then create the method addRule(self, newrule), which should add a copy of newrule to the rules field of self. Look at the version 1 lsystem if you need to remember how to write the method.
  5. Create a read(self, filename) method that opens the file, reads in the Lsystem information, resets the base and rules fields of self, and then store the information from the file in the appropriate fields (you can use the accessors self.setBase and self.addRule to do that). You can copy and paste the function code from the version 1 lsystem.py file, but it will require some modification. For examle, you don't need to create a new Lsystem (self already exists) and you'll need to use the new accessor methods.
  6. In order to handle multiple rules, we need to write our own replace method for an L-system. The indented algorithm is below. We scan through the string, and for each character we test if there is a rule. If so, we add the replacement to a new string, otherwise we add the character itself to the new string.
        def replace(self, istring):
            # assign to a local variable (e.g. tstring) the empty string
            # for each character c in the input string (istring)
                # set a local variable (e.g. found) to False
                # for each rule in the rules field of self
                    # if the symbol in the rule is equal to the character
                        # add to tstring the replacement from the rule
                        # set found to True
                # if not found
                    # add to tstring the character c
            # return tstring
    
  7. Create a buildString(self, iterations) function. It will be almost identical to the buildString function from version 1. The code outline is below.
        def buildString(self, iterations):
            # assign to a local variable (e.g. nstring) the base field of self
            # for the number of iterations
                # assign to nstring the result of calling the replace method of self
            # return nstring
    
  8. Copy the following main function and put it at the bottom of your Lsystem class file. Call it like we did last week.

    python lsystem.py systemA 3 stra

    Be sure to put an import sys at the top of your file.

    def main(argv):
    
        if len(argv) < 4:
            print 'Usage: lsystem.py <filename> <iterations> <output file>'
            exit()
    
        filename = argv[1]
        iterations = int(argv[2])
        outfile = argv[3]
    
        lsys = Lsystem()
    
        lsys.read( filename )
    
        print lsys
    
        lstr = lsys.buildString( iterations )
    
        fp = file( outfile, 'w' )
        fp.write(lstr)
        fp.close()
    
        return
    
    if __name__ == "__main__":
        main(sys.argv)
    

    You can download and run the file on any of the following examples.

    SystemE is a little more complex than the others as it uses the characters f, L, and !. Let f be forward by distance*1.7; let L be a leaf; and let ! reduce the width by 1. In order for the shape to draw properly, you'll need to start the turtle with a width greater than 1 (e.g. 5), and you'll need to save and restore the turtle width along with the position and heading for [ and ]. When you run it, use an angle of 15. Getting systemE to run properly is a good extension.

  9. Create a new file called interpeter.py. Label it as version 2. You'll want to import the turtle package, and probably the random and sys packages as well. Begin the class definition for an Interpreter class.

    class Interpreter:

  10. Create an __init__ method with the definition below. The init should call turtle.setup(width = dx, height = dy ) and then set the tracer to False (if you wish).

    def __init__(self, dx = 800, dy = 800):

  11. Create a drawString method for the Interpreter class. Except for the actual function definition, you can copy and paste it from the version 1 interpreter.py. The new method definition just needs self as the first argument.

    def drawString(self, dstring, distance, angle):

  12. Copy over the hold and saveCanvas functions. Again, you just need to add self as the first argument to each function definition.
  13. Add the test function below, which is almost identical to last week, and test your interpreter.py file just like we did last week.
    def main(argv):
    
        if len(argv) < 4:
            print 'Usage: interpreter.py <string file> <distance> <angle>'
            exit()
    
        filename = argv[1]
        distance = int(argv[2])
        angle = float(argv[3])
    
        dev = Interpreter( 800, 800 )
    
        fp = file( filename, 'r' )
        lstring = fp.readline()
        fp.close()
    
        dev.drawString( lstring, distance, angle )
        dev.hold()
    
    if __name__ == "__main__":
        main(sys.argv)
    

    If you run the file as below, it should draw the rectangular shape corresponding to SystemA.

    python interpreter.py stra 10 90

Once you have finished the lab, go ahead and get started on project 8.