need refactor

This commit is contained in:
Joshua E. Jodesty 2018-09-24 16:10:28 -04:00
parent d93dcf4fcc
commit 8f3f44d676
1 changed files with 526 additions and 0 deletions

526
CAD_Engine.ipynb Normal file
View File

@ -0,0 +1,526 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from fn import op, _\n",
"from itertools import repeat\n",
"from functools import reduce\n",
"# from objproxies import LazyProxy\n",
"import json\n",
"from copy import deepcopy, copy\n",
"from pipetools import pipe\n",
"from functools import partial"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"state_dict = {\n",
" 's1': 2,\n",
" 's2': 4,\n",
" 's3': 300\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# def pipeline(*steps):\n",
"# return reduce(lambda x, y: y(x), list(steps))\n",
"# def compose(*funcs):\n",
"# return lambda x: reduce(lambda f, g: g(f), list(funcs), x)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# ToDo:\n",
"# Handle case where Mechanisms have no input. Perhaps the sentinel value of 0"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# UI Behavior Mechanisms per Mechanism\n",
"def b1m1(s):\n",
" return s['s1']\n",
"def b2m1(s):\n",
" return s['s1'] * s['s2']\n",
"\n",
"def b1m2(s):\n",
" return s['s1']\n",
"def b2m2(s):\n",
" return s['s1'] / s['s2']\n",
"\n",
"# UI State Mechanisms per Mechanism\n",
"def s1m1(s, _input):\n",
" s['s1'] = s['s1']**2 + _input\n",
"def s2m1(s, _input):\n",
" s['s2'] = s['s2'] + 1 + _input \n",
"def s3m1(s, _input):\n",
" s['s3'] = s['s3']\n",
"\n",
"def s1m2(s, _input):\n",
" s['s1'] = s['s1'] + _input\n",
"def s2m2(s, _input):\n",
" s['s2'] = s['s2']\n",
"def s3m2(s, _input):\n",
" s['s3'] = s['s3'] + 1\n",
" \n",
"j = {\n",
" \"m1\": {\n",
" \"behaviors\": {\n",
" \"b1\": b1m1,\n",
" \"b2\": b2m1\n",
" },\n",
" \"states\": {\n",
" \"s1\": s1m1,\n",
" \"s2\": s2m1,\n",
" \"s3\": s3m1\n",
" }\n",
" },\n",
" \"m2\": {\n",
" \"behaviors\": {\n",
" \"b1\": b1m2,\n",
" \"b2\": b2m2\n",
" },\n",
" \"states\": {\n",
" \"s1\": s1m2,\n",
" \"s2\": s2m2,\n",
" \"s3\": s3m2\n",
" }\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"def generate_configs(j):\n",
" return list(\n",
" map(\n",
" lambda x: (\n",
" list(j[x][\"states\"].values()),\n",
" list(j[x][\"behaviors\"].values())\n",
" ), \n",
" j.keys()\n",
" )\n",
" )\n",
"\n",
"def getColResults(s, funcs):\n",
" return list(map(lambda f: f(s), funcs))\n",
"\n",
"def getBehaviorInput(s, funcs): \n",
" return op.foldr(_ + _)(getColResults(s, funcs))\n",
"\n",
"def mech_step(sL, state_funcs, behavior_funcs):\n",
" in_copy, out_copy, mutatable_copy = deepcopy(sL), deepcopy(sL), deepcopy(sL)\n",
" last_in_obj, last_mut_obj = in_copy[-1], mutatable_copy[-1]\n",
" \n",
" _input = getBehaviorInput(last_in_obj, behavior_funcs)\n",
"\n",
" for f in state_funcs:\n",
" f(last_mut_obj, _input)\n",
" \n",
" out_copy.append(last_mut_obj)\n",
" return out_copy\n",
"\n",
"def pipeline(states_list, configs):\n",
" for config in configs:\n",
" s_conf, b_conf = config[0], config[1]\n",
" states_list = mech_step(states_list, s_conf, b_conf)\n",
" return states_list"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'s1': 2, 's2': 4, 's3': 300},\n",
" {'s1': 14, 's2': 15, 's3': 300},\n",
" {'s1': 28.933333333333334, 's2': 15, 's3': 301}]"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"states_list = [state_dict]\n",
"configs = generate_configs(j)\n",
"pipeline(states_list, configs)"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'s1': 2, 's2': 4, 's3': 300}]"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"states_list"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[([<function __main__.s1m1(s, _input)>,\n",
" <function __main__.s2m1(s, _input)>,\n",
" <function __main__.s3m1(s, _input)>],\n",
" [<function __main__.b1m1(s)>, <function __main__.b2m1(s)>]),\n",
" ([<function __main__.s1m2(s, _input)>,\n",
" <function __main__.s2m2(s, _input)>,\n",
" <function __main__.s3m2(s, _input)>],\n",
" [<function __main__.b1m2(s)>, <function __main__.b2m2(s)>])]"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sm1 = [s1m1, s2m1, s3m1]\n",
"bm1 = [b1m1, b2m1]\n",
"\n",
"sm2 = [s1m2, s2m2, s3m2]\n",
"bm2 = [b1m2, b2m2]\n",
"\n",
"configs = [(sm1, bm1), (sm2, bm2)]\n",
"configs"
]
},
{
"cell_type": "code",
"execution_count": 139,
"metadata": {},
"outputs": [],
"source": [
"# UI State Mechanisms\n",
"\n",
"# s['s1']^2 + (s['s1'] + (s['s1'] * s['s2']))\n",
"# 14 = 4 + (2 + (2 * 4))\n",
"def m(sL, s_funcs, b_funcs):\n",
" dSL = deepcopy(sL)\n",
" in_copy = deepcopy(sL)\n",
" out_copy = deepcopy(sL)\n",
" s = dSL[-1]\n",
" _input = getColInput(in_copy[-1], b_funcs)\n",
" \n",
"# s['s1'] = s['s1']**2 + _input\n",
"# s['s2'] = s['s2'] + 1 + _input\n",
" for f in s_funcs:\n",
" f(s, _input)\n",
" \n",
" out_copy.append(s)\n",
" return out_copy\n",
"\n",
"# s['s1'] + (s['s1'] + (s['s1'] / s['s2']))\n",
"# 28.9 = 14 + (14 + (14/15))\n",
"def m2(sL):\n",
" dSL = deepcopy(sL)\n",
" s = dSL[-1]\n",
" _input = getColInput(deepcopy(sL)[-1], [b1m2, b2m2])\n",
" \n",
" s['s1'] = s['s1'] + _input\n",
" s['s2'] = s['s2']\n",
" \n",
" sL.append(s)\n",
" return sL\n",
"\n",
"# def s2m1(sL):\n",
"# s = deepcopy(sL)[-1]\n",
"# s['s2'] = s['s2'] + 1 + getColInput(s, [b1m2, b2m2])\n",
"# sL.append(s)\n",
"# return sL\n",
"\n",
"# def s2m2(sL):\n",
"# s = deepcopy(sL)[-1]\n",
"# s['s2'] = s['s2']\n",
"# sL.append(s)\n",
"# return sL"
]
},
{
"cell_type": "code",
"execution_count": 152,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'s1': 2, 's2': 4, 's3': 300},\n",
" {'s1': 14, 's2': 15, 's3': 300},\n",
" {'s1': 28.933333333333334, 's2': 15, 's3': 300}]"
]
},
"execution_count": 152,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pipeline3(states_list, configs)"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {
"collapsed": true
},
"outputs": [
{
"ename": "TypeError",
"evalue": "s1m1() missing 1 required positional argument: '_input'",
"output_type": "error",
"traceback": [
"\u001b[0;31m-----------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0mTraceback (most recent call last)",
"\u001b[0;32m<ipython-input-96-5f9e087e72ee>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mdSL\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mpipeline2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstates_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0ms1m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms1m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-96-5f9e087e72ee>\u001b[0m in \u001b[0;36mpipeline2\u001b[0;34m(sL, funcs)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mdSL\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdeepcopy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mf\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mfuncs\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mdSL\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdSL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mdSL\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mTypeError\u001b[0m: s1m1() missing 1 required positional argument: '_input'"
]
}
],
"source": [
"def pipeline2(sL, funcs):\n",
" dSL = deepcopy(sL)\n",
" for f in funcs:\n",
" dSL = f(dSL)\n",
" return dSL\n",
"\n",
"pipeline2(states_list, [s1m1, s1m2])"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [],
"source": [
"state_dict = {\n",
" 's1': 2,\n",
" 's2': 4,\n",
" 's3': 300\n",
"}\n",
"# l = [state_dict]\n",
"# l.append(state_dict)\n",
"# l\n",
"# print(s1m1([state_dict]))"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'s1': 2, 's2': 4, 's3': 300}]"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"state_dict\n",
"states_list = [state_dict]\n",
"states_list"
]
},
{
"cell_type": "code",
"execution_count": 140,
"metadata": {
"collapsed": true
},
"outputs": [
{
"ename": "TypeError",
"evalue": "'function' object is not subscriptable",
"output_type": "error",
"traceback": [
"\u001b[0;31m-----------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0mTraceback (most recent call last)",
"\u001b[0;32m<ipython-input-140-e6d630186e0f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mpipeline\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms1m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms1m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-104-17d92fdcc430>\u001b[0m in \u001b[0;36mpipeline\u001b[0;34m(*steps)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mpipeline\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0msteps\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mreduce\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msteps\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcompose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mfuncs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mreduce\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mlambda\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfuncs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-104-17d92fdcc430>\u001b[0m in \u001b[0;36m<lambda>\u001b[0;34m(x, y)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mpipeline\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0msteps\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mreduce\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msteps\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcompose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mfuncs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mreduce\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mlambda\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfuncs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-137-dac038f7b2cf>\u001b[0m in \u001b[0;36ms1m2\u001b[0;34m(sL)\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0;31m# dSL = deepcopy(sL)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;31m# s = dSL[-1]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 19\u001b[0;31m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msL\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 20\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m's1'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m's1'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mgetColInput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mb1m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb2m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0msL\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mTypeError\u001b[0m: 'function' object is not subscriptable"
]
}
],
"source": [
"pipeline(s1m1, s1m2)"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'s1': 2, 's2': 4, 's3': 300}]"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pipeline2(states_list, [s1m1, s1m2])"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"# def a(x):\n",
"# return x + 1\n",
"# func = F() >> s1m1 >> s1m1\n",
"# func(state_dict)\n",
"def s1_pipe(x): return pipe | s1m1 | s1m2 "
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"s1m1 | s1m2"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s1_pipe(state_dict)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'s1': 2, 's2': 4, 's3': 300}"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"state_dict"
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n"
]
}
],
"source": [
"from functools import partial\n",
"\n",
"def multiply(x,y):\n",
" return x * y\n",
"\n",
"# create a new function that multiplies by 2\n",
"dbl = partial(multiply,2)\n",
"print(dbl(4))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}