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
def resolve_methods(connections, methods, resolved_methods):
"""
First iteration will give methods without any arguments and
methods that uses these argumentless methods.
Second iteration will yield A, because B and C are resolved.
Need to pass in resolved methods for other methods to use.
"""
result_length = len(resolved_methods.keys())
mapped_methods = {}
for method in methods:
data = {"method": method, "arguments": {}}
for arg in inspect.getargspec(method).args:
data["arguments"][arg] = None
mapped_methods[method.__name__] = data
for connection in connections:
out_connection = connection[0]
in_connection, argument = connection[1].split(".")
data = mapped_methods[out_connection]
# Methods without any arguments can be passed straight as resolved
if not data["arguments"].keys():
resolved_methods[data["method"].__name__] = data["method"]
# Fill in arguments from resolved_methods
if out_connection in resolved_methods.keys():
arguments = mapped_methods[in_connection]["arguments"]
arguments[argument] = resolved_methods[out_connection]
# Add fully resolved methods to results
for name, data in mapped_methods.iteritems():
resolved = True
for key, value in data["arguments"].iteritems():
if value is None:
resolved = False
if resolved and name not in resolved_methods:
arguments = []
for arg in inspect.getargspec(data["method"]).args:
for key, value in data["arguments"].iteritems():
if key == arg:
arguments.append(value())
resolved_methods[name] = functools.partial(
data["method"], *arguments
)
if result_length == len(resolved_methods.keys()):
return resolved_methods
else:
return resolve_methods(connections, methods, resolved_methods)
registered_methods = [add, multiply, x]
# Need unique ids for components in graph. Ei. instead of "add", its needs to be "unique-id".
# UUIDs might be a good option.
# Or we could describe the connections with classes.
class component(object):
def __init__(self, method):
self.method = method
for arg in inspect.getargspec(method).args:
self.__dict__[arg] = None
x1 = component(x)
add1 = component(add)
add2 = component(add)
connections = [
(x1, add1.x),
(x1, add1.y),
(x1, add2.x),
(add1, add2.y)
]
"""
connections = [
("x", "add.x"), ("x", "add.y"),
]
resolved_methods = {}
resolved_methods = resolve_methods(
connections, registered_methods, resolved_methods
)
print resolved_methods["add"]()
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment