Guest User

Untitled

a guest
Nov 23rd, 2017
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. import numpy, inspect
  2. from random import choice
  3. def makelist(foo):
  4. if hasattr(foo, "__getitem__"):
  5. return list(foo)
  6. else: return [foo]
  7.  
  8. def abstract_you_moron(foo):
  9. raise NotImplementedError("This is supposed to be /subclassed/. Derp.")
  10.  
  11. class Node(object):
  12. func = abstract_you_moron
  13.  
  14.  
  15.  
  16. def __init__(self, *children):
  17. self.numargs = len(inspect.getargspec(self.func).args) - 1
  18.  
  19. self.parent = None
  20.  
  21. if len(children) > 0:
  22. self.children = makelist(children)
  23. if len(self.children) != self.numargs:
  24. raise ValueError("Wrong number of children: got {0}, expected {1}".format(len(self.children), self.numargs))
  25. for child in self.children: child.parent = self
  26. else: self.children = None
  27.  
  28. self.setup()
  29.  
  30. def setup(self):
  31. pass
  32.  
  33. def __call__(self, *args):
  34. if self.children is None:
  35. return self.func(*args)
  36. else:
  37. return self.func(*[child(*args) for child in self.children if child is not None])
  38.  
  39.  
  40.  
  41. class AdditionNode(Node):
  42. def func(self, x, y):
  43. return x + y
  44.  
  45. class SubtractionNode(Node):
  46. def func(self, x, y):
  47. return x + y
  48.  
  49. class IfNode(Node):
  50. def func(self, exp, true, false):
  51. if exp: return true
  52. else: return false
  53.  
  54. class NotNode(Node):
  55. def func(self, exp):
  56. return not exp
  57.  
  58. class SquaringNode(Node):
  59. def func(self, x):
  60. return x ** 2
  61.  
  62. class SquareRootNode(Node):
  63. def func(self, x):
  64. return numpy.sqrt(x)
  65.  
  66. class StaticValueNode(Node):
  67. def setup(self):
  68. self.value = numpy.random.randint(1, 100)
  69.  
  70. def func(self):
  71. return self.value
  72.  
  73. class RandomRangeNode(Node):
  74. def func(self, minim, maxim):
  75. return numpy.random.randint(minim, maxim)
  76.  
  77. nodes = [AdditionNode, SubtractionNode, IfNode, NotNode, StaticValueNode, RandomRangeNode]
  78.  
  79. def buildtree(maxdepth):
  80. root = choice(nodes)
  81. print root
  82.  
  83. buildtree(1)
Add Comment
Please, Sign In to add comment