def _range2parts(inputs, outputs): dsp = sh.Dispatcher() dsp.add_data(data_id='cr', default_value='1') dsp.add_data(data_id='cc', default_value='1') dsp.add_function('relative2absolute', _sum, ['cr', 'rr1'], ['r1']) dsp.add_function('relative2absolute', _sum, ['cc', 'rc1'], ['n1']) dsp.add_function('relative2absolute', _sum, ['cr', 'rr2'], ['r2']) dsp.add_function('relative2absolute', _sum, ['cc', 'rc2'], ['n2']) dsp.add_function(function=_index2col, inputs=['n1'], outputs=['c1']) dsp.add_function(function=_index2col, inputs=['n2'], outputs=['c2']) dsp.add_function(function=_col2index, inputs=['c1'], outputs=['n1']) dsp.add_function(function=_col2index, inputs=['c2'], outputs=['n2']) dsp.add_function(function=sh.bypass, inputs=['c1'], outputs=['c2']) dsp.add_function(function=sh.bypass, inputs=['r1'], outputs=['r2']) dsp.add_function(function=lambda x, y: x[y], inputs=['external_links', 'excel_id'], outputs=['excel']) dsp.add_function(function=sh.bypass, weight=1, inputs=['excel_id'], outputs=['excel']) dsp.add_data(data_id='excel', filters=(str.upper, )) dsp.add_data(data_id='sheet', default_value='', filters=(str.upper, )) dsp.add_data(data_id='ref', filters=(str.upper, )) dsp.add_data(data_id='name', filters=(str.upper, )) dsp.add_data(data_id='n1', default_value=0, initial_dist=100) dsp.add_data(data_id='r1', default_value='0', initial_dist=100) dsp.add_data(data_id='c2', default_value=_maxcol(), initial_dist=100) dsp.add_data(data_id='r2', default_value=_maxrow(), initial_dist=100) dsp.add_function(None, _build_ref, ['c1', 'r1', 'c2', 'r2'], ['ref']) dsp.add_function(None, _build_id, ['ref', 'sheet', 'excel'], ['name']) func = sh.DispatchPipe(dsp, '', inputs, outputs) func.output_type = 'all' return func
def compile(self, references=None, **inputs): dsp, inp = self.dsp, inputs.copy() for k in set(dsp.data_nodes).intersection(references or {}): inp[k] = Ranges().push(references[k]) res, o = dsp(inp), self.get_node_id(self[-1]) dsp = dsp.get_sub_dsp_from_workflow([o], graph=dsp.dmap, reverse=True, blockers=res, wildcard=False) dsp.nodes.update({k: v.copy() for k, v in dsp.nodes.items()}) i = collections.OrderedDict() for k in sorted(dsp.data_nodes): if not dsp.dmap.pred[k]: if k in res: v = res[k] if k not in inputs and isinstance(v, Ranges) and v.ranges: i[k] = v else: dsp.add_data(data_id=k, default_value=v) else: try: i[k] = Ranges().push(k) except ValueError: i[k] = None dsp.raises = True dsp.nodes[o]['filters'] = wrap_ranges_func(sh.bypass), return sh.DispatchPipe(dsp, '=%s' % o, i, [o], wildcard=False)
def setUp(self): ss_dsp = sh.Dispatcher() fun = lambda a: (a + 1, 5, a - 1) dom = lambda kw: True c = '|!"£$%&/()=?^*+éè[]#¶ù§çò@:;-_.,<>' ss_dsp.add_function(function=fun, inputs=['a'], outputs=['b', sh.SINK, c], input_domain=dom, weight=1) def raise_fun(a): raise ValueError('Error') ss_dsp.add_function(function=raise_fun, inputs=['a'], outputs=['b']) sdspfunc = sh.SubDispatchFunction(ss_dsp, 'SubDispatchFunction', ['a'], ['b', c]) sdsppipe = sh.SubDispatchPipe(ss_dsp, 'SubDispatchPipe', ['a'], ['b', c]) spipe = sh.DispatchPipe(ss_dsp, 'DispatchPipe', ['a'], [c]) sdsp = sh.SubDispatch(ss_dsp, ['b', c], output_type='list') s_dsp = sh.Dispatcher() s_dsp.add_function(None, sdspfunc, ['a'], ['b', 'c'], weight=2) s_dsp.add_function(None, sdsppipe, ['a'], ['b', 'c'], out_weight={'c': 5}) s_dsp.add_function(None, spipe, ['a'], ['b'], weight=2) s_dsp.add_function('SubDispatch', sdsp, ['d'], ['e', 'f']) dsp = sh.Dispatcher() dsp.add_data('A', default_value=[0] * 1000) dsp.add_data('D', default_value={'a': 3}) dsp.add_dispatcher(dsp=s_dsp, inputs={ 'A': 'a', 'D': 'd' }, outputs={ 'b': 'B', 'c': 'C', 'e': 'E', 'f': 'F' }, inp_weight={'A': 3}) self.sol = dsp.dispatch() self.dsp = dsp dsp = sh.Dispatcher() dsp.add_function(function=sh.bypass, inputs=['a'], outputs=[sh.PLOT]) self.dsp_plot = dsp
def hex2dec2bin2oct(function_id): dsp = sh.Dispatcher() for k in ('HEX', 'OCT', 'BIN'): dsp.add_data(k, filters=[_parseX]) dsp.add_function( function_id='HEX2DEC', function=_x2dec, inputs=['HEX'], outputs=['DEC'] ) dsp.add_function( function_id='OCT2DEC', function=functools.partial(_x2dec, base=8), inputs=['OCT'], outputs=['DEC'] ) dsp.add_function( function_id='BIN2DEC', function=functools.partial(_x2dec, base=2), inputs=['BIN'], outputs=['DEC'] ) dsp.add_function( function_id='DEC2HEX', function=_dec2x, inputs=['DEC', 'places'], outputs=['HEX'] ) dsp.add_function( function_id='DEC2OCT', function=functools.partial(_dec2x, base=8), inputs=['DEC', 'places'], outputs=['OCT'] ) dsp.add_function( function_id='DEC2BIN', function=functools.partial(_dec2x, base=2), inputs=['DEC', 'places'], outputs=['BIN'] ) i, o = function_id.split('2') _func = sh.DispatchPipe(dsp, function_id, [i, 'places'], [o]) def func(x, places=None): return _func(x, places) return func
def setUp(self): import functools ss_dsp = sh.Dispatcher() def fun(a, c): """ :param a: Nice a. :type a: float :param c: Nice c. :type c: float :return: Something. :rtype: tuple """ return a + 1, c, a - 1 ss_dsp.add_function('fun', fun, ['a', 'e'], ['b', 'c', 'd']) ss_dsp_func = sh.DispatchPipe(ss_dsp, 'func', ['e', 'a'], ['c', 'd', 'b']) sub_disfun = sh.add_args(functools.partial(ss_dsp_func, 5)) s_dsp = sh.Dispatcher() s_dsp.add_data('a', 1) s_dsp.add_data('d', 4) s_dsp.add_function('sub_dispatch', sub_disfun, ['d', 'a'], ['b', 'c', sh.SINK]) dispatch = sh.SubDispatch(s_dsp, ['b', 'c', 'a'], output_type='list') self.dsp = dsp = sh.Dispatcher() dsp.add_data('input', default_value={'a': 3}) dsp.add_function('dispatch', dispatch, ['input'], [sh.SINK, 'h', 'i'], inp_weight={'input': 4}, out_weight={ 'h': 3, 'i': 6 }) dsp.add_function('fun', lambda: None, None, ['j']) dsp.add_dispatcher(s_dsp, inputs=('a', ), outputs=('b', 'c'), include_defaults=True)
def test_function(self): fun = sh.DispatchPipe(self.dsp_1, 'F', ['a', 'b'], ['a']) self.assertEqual(fun.__name__, 'F') # noinspection PyCallingNonCallable self.assertEqual(fun(2, 1), 1) self.assertRaises(sh.DispatcherError, fun, 3, -1) self.assertRaises(sh.DispatcherError, fun, 3, None) fun = sh.DispatchPipe(self.dsp_2, 'F', ['b', 'a'], ['c', 'd']) # noinspection PyCallingNonCallable self.assertEqual(fun(1, 2), [3, 2]) self.assertRaises(ValueError, sh.DispatchPipe, self.dsp_2, 'F', ['a', 'c'], ['d']) fun = sh.DispatchPipe(self.dsp_3, 'F', ['b', 'a'], ['c', 'd']) # noinspection PyCallingNonCallable self.assertEqual(fun(5, 20), [25, 20]) fun = sh.DispatchPipe(self.dsp_4, 'F', ['b', 'a'], ['c', 'd']) # noinspection PyCallingNonCallable self.assertEqual(fun(5, 20), [25, 20]) fun = sh.DispatchPipe(self.dsp_5, 'F', ['b', 'a', 'e', 'h'], ['c', 'd']) # noinspection PyCallingNonCallable self.assertEqual(fun(1, 2, 0, 0), [3, 2]) self.assertEqual(fun(b=1, a=2, e=0, h=0), [3, 2]) self.assertEqual(fun(1, 2, 0), [4, 3]) self.assertEqual(fun(1, 2, e=0), [4, 3]) self.assertRaises(ValueError, sh.DispatchPipe, self.dsp_5, 'F', ['a', 'c'], ['d']) self.assertRaises(TypeError, fun, 2, 1, 2, 5, 6) self.assertRaises(TypeError, fun, 2, 1, a=2, b=2) self.assertRaises(TypeError, fun, 2, 1, g=0) self.assertRaises(TypeError, fun, 1, 2, 0, c=3) self.assertRaises(TypeError, fun, 1, 2, 0, i=3) self.assertRaises(TypeError, fun) fun = sh.DispatchPipe(self.dsp_6, outputs=['d']) self.assertEqual(fun(), 0) self.assertRaises(TypeError, fun, a=4) self.assertRaises(TypeError, fun, d=5) self.assertRaises(TypeError, fun, a=3, d=5) self.assertRaises(TypeError, fun, 2) self.assertRaises(TypeError, fun, c=2) fun = sh.DispatchPipe(self.dsp_6, outputs=['c']) self.assertEqual(fun(), 0) self.assertRaises(TypeError, fun, 2) self.assertRaises(TypeError, fun, a=2) self.assertRaises(TypeError, fun, c=5) self.assertRaises(TypeError, fun, a=2, c=7) self.assertRaises(TypeError, fun, d=2) fun = sh.DispatchPipe(self.dsp_6, inputs=['y', 'x'], outputs=['z']) self.assertEqual(fun(x=3), 3) self.assertRaises(TypeError, fun, 2) self.assertRaises(TypeError, fun, y=4) self.assertRaises(TypeError, fun, a=2) fun = sh.DispatchPipe(self.dsp_6, inputs=['x'], outputs=['z']) self.assertEqual(fun(x=3), 3) fun.__setstate__(fun.__getstate__()) self.assertEqual(fun(x=3), 3)