Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- class System(object):
- matcher = re.compile("^rule_")
- def __init__(self):
- self.base = self.base_rule()
- self.__introspect()
- def __introspect(self):
- funcs = {re.sub(System.matcher, '', x).upper(): self.__getattribute__(x) for x
- in dir(self) if System.matcher.match(x)}
- self.funcs = funcs
- """Returns the L-system string after _x_ generations"""
- def representation(self, generation=1, rep=None):
- if rep is None:
- rep = self.base
- if generation is 1:
- return rep
- else:
- rep = self.process(rep)
- return self.representation(generation-1, rep)
- def process(self, rep):
- return "".join([self.process_char(x) for x in rep])
- def process_char(self, char):
- return self.funcs[char]() if self.funcs.has_key(char) else char
- """Returns a string that describes the base rule for the L-system"""
- def base_rule(self):
- raise NotImplementedError
- # """The amount that a right or left turn instruction should turn"""
- # def turn_radius(self):
- # raise NotImplementedError
- #
- # """'+' instruction means to turn right"""
- # def plus(self):
- # return lambda x: x.right(self.turn_radius)
- # pass
- #
- # def minus(self):
- # return lambda x: x.left(self.turn_radius)
- # pass
- """Example L-system derived from System that highlights basic behaviour"""
- class KochSnowflakeSystem(System):
- def base_rule(self):
- return "F++F++F"
- def turn_radius(self):
- return 60
- def rule_f(self):
- return "F-F++F-F"
Add Comment
Please, Sign In to add comment