Guest User

Untitled

a guest
May 23rd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. import re
  2.  
  3. class System(object):
  4. matcher = re.compile("^rule_")
  5.  
  6. def __init__(self):
  7. self.base = self.base_rule()
  8. self.__introspect()
  9.  
  10. def __introspect(self):
  11. funcs = {re.sub(System.matcher, '', x).upper(): self.__getattribute__(x) for x
  12. in dir(self) if System.matcher.match(x)}
  13. self.funcs = funcs
  14.  
  15. """Returns the L-system string after _x_ generations"""
  16. def representation(self, generation=1, rep=None):
  17. if rep is None:
  18. rep = self.base
  19. if generation is 1:
  20. return rep
  21. else:
  22. rep = self.process(rep)
  23. return self.representation(generation-1, rep)
  24.  
  25. def process(self, rep):
  26. return "".join([self.process_char(x) for x in rep])
  27.  
  28. def process_char(self, char):
  29. return self.funcs[char]() if self.funcs.has_key(char) else char
  30.  
  31. """Returns a string that describes the base rule for the L-system"""
  32. def base_rule(self):
  33. raise NotImplementedError
  34.  
  35. # """The amount that a right or left turn instruction should turn"""
  36. # def turn_radius(self):
  37. # raise NotImplementedError
  38. #
  39. # """'+' instruction means to turn right"""
  40. # def plus(self):
  41. # return lambda x: x.right(self.turn_radius)
  42. # pass
  43. #
  44. # def minus(self):
  45. # return lambda x: x.left(self.turn_radius)
  46. # pass
  47.  
  48.  
  49.  
  50. """Example L-system derived from System that highlights basic behaviour"""
  51. class KochSnowflakeSystem(System):
  52. def base_rule(self):
  53. return "F++F++F"
  54.  
  55. def turn_radius(self):
  56. return 60
  57.  
  58. def rule_f(self):
  59. return "F-F++F-F"
Add Comment
Please, Sign In to add comment