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 turtle 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.
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 TurtleInterpreter classes to have certain methods with particular names.
class LSystem(object):
Just like with functions and methods, classes should have their own docstring. Each LSystem object will have two fields: base
, representing the base string, and rules
, representing the way replacements are made. Add a docstring for your class that describes what objects of this class represent and what their fields are.
class LSystem(object): '''An LSystem represents ... fields: base, the ... rules, a list of ... '''
Then we define the __init__ method, 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
LSystem's init method 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 = ''
Here is the template for the init function
def __init__( self, filename = None ): '''docstring''' # assign to the field base, the empty string # assign to the field rules, an empty list # if the filename variable is not equal to None # call the read method of self with filename as the argument
setBase(self,
newbase)
, getBase(self)
. The setBase method should assign the new
base string to the base field of self. The getBase method should
return the base field of self. 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 newrule to
the rules field of self (reminder: newrule is a list with two strings in it). Look at the version 1 lsystem if you need to remember how to write the method. read(self, filename)
method that opens the file, reads in the
L-system information, resets the base and rules fields of self, and
then store the information from the file in the appropriate fields
(you should use the mutators 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 example, you don't
need to create a new LSystem (self already exists) and you'll need to
use the new accessor methods.
You can use the following template
def read( self, filename ): '''docstring''' # assign to a variable (e.g. sourceFile) the file object created with filename in read mode # assign to a variable (e.g. lines) the list of lines in the file # call the close method of the file # for each element in the lines list # assign to a variable (e.g. words) the loop variable split on spaces # if the first item in words is equal to the string 'base' # call the setBase method of self with the new base string # else if the first item in words is equal to the string 'rule' # call the addRule method of self with the new rule
Once you are done with all of the above steps, download and run this test function. It should nicely print out a single rule L-system read from a file and then print out a second L-system created in the test program.
def applyRules(self, inputString): '''docstring''' # assign to a local variable (e.g. resultString) the empty string. # When finished, that variable will be the result of applying the rules. # for each character, char, in the input string # set a local boolean variable (e.g. foundReplacement) to False # for each rule in the rules field of self # if the symbol in the rule is equal to the character # add to the string you're building the replacement from the rule # set foundReplacement to True # break (we don't want any more rules to be activated on this same character) # if a replacement wasn't found # add char to the string you're building # return the result
def buildString(self, iterations): '''docstrings are your best friend''' # assign to a local variable (e.g. string) the base field of self # for the number of iterations # assign to nextString the result of calling self's applyRules method on string # reassign string to the value in nextString. (What will go wrong if you don't do this step?) # return nextString
$ python lsystem.py systemA1.txt 3 strA.txt
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] lSystem = LSystem() lSystem.read( filename ) print lSystem print lSystem.getBase() print lSystem.getRule(0) resultString = lSystem.buildString( iterations ) outputFile = file( outfile, 'w' ) outputFile.write(resultString) outputFile.close() return if __name__ == "__main__": main(sys.argv)
You can download and run the file on any of the following examples. Systems C through G require multiple rules.
class TurtleInterpreter:
def __init__(self, dx = 800, dy = 800): # call turtle.setup # set the turtle tracer to false (optional)
def drawString(self, dstring, distance, angle):
Add the following methods to your turtle interpreter class.