Skip to content

Instantly share code, notes, and snippets.

@tokejepsen
Last active December 13, 2017 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tokejepsen/acd7d86f268e579883c8da0b1bb7d9ac to your computer and use it in GitHub Desktop.
Save tokejepsen/acd7d86f268e579883c8da0b1bb7d9ac to your computer and use it in GitHub Desktop.
Flow Based Programming Experiments
import inspect
import functools
def multiply(x, y):
return x * y
def add(x, y):
return x + y
def x():
return 1
# Assign "process" method to components values
def resolve_connections(connections):
for connection in connections:
source = connection[0]
target = connection[1]
argument = connection[2]
target.__dict__[argument] = functools.partial(source.process)
class component(object):
def __init__(self, method):
self.method = method
def process(self):
arguments = []
for arg in inspect.getargspec(self.method).args:
arguments.append(self.__dict__[arg]())
return self.method(*arguments)
x1 = component(x)
add1 = component(add)
add2 = component(add)
multipy1 = component(multiply)
connections = [
(x1, add1, "x"),
(x1, add1, "y"),
(x1, add2, "x"),
(add1, add2, "y"),
(add2, multipy1, "x"),
(add1, multipy1, "y"),
]
resolve_connections(connections)
print multipy1.process()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment