Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy, inspect
- from random import choice
- def makelist(foo):
- if hasattr(foo, "__getitem__"):
- return list(foo)
- else: return [foo]
- def abstract_you_moron(foo):
- raise NotImplementedError("This is supposed to be /subclassed/. Derp.")
- class Node(object):
- func = abstract_you_moron
- def __init__(self, *children):
- self.numargs = len(inspect.getargspec(self.func).args) - 1
- self.parent = None
- if len(children) > 0:
- self.children = makelist(children)
- if len(self.children) != self.numargs:
- raise ValueError("Wrong number of children: got {0}, expected {1}".format(len(self.children), self.numargs))
- for child in self.children: child.parent = self
- else: self.children = None
- self.setup()
- def setup(self):
- pass
- def __call__(self, *args):
- if self.children is None:
- return self.func(*args)
- else:
- return self.func(*[child(*args) for child in self.children if child is not None])
- class AdditionNode(Node):
- def func(self, x, y):
- return x + y
- class SubtractionNode(Node):
- def func(self, x, y):
- return x + y
- class IfNode(Node):
- def func(self, exp, true, false):
- if exp: return true
- else: return false
- class NotNode(Node):
- def func(self, exp):
- return not exp
- class SquaringNode(Node):
- def func(self, x):
- return x ** 2
- class SquareRootNode(Node):
- def func(self, x):
- return numpy.sqrt(x)
- class StaticValueNode(Node):
- def setup(self):
- self.value = numpy.random.randint(1, 100)
- def func(self):
- return self.value
- class RandomRangeNode(Node):
- def func(self, minim, maxim):
- return numpy.random.randint(minim, maxim)
- nodes = [AdditionNode, SubtractionNode, IfNode, NotNode, StaticValueNode, RandomRangeNode]
- def buildtree(maxdepth):
- root = choice(nodes)
- print root
- buildtree(1)
Add Comment
Please, Sign In to add comment