Пример #1
0
def constructor(ctx, libctx, context_graph, inp, result, elision,
                elision_chunksize):
    m = ctx.m = Macro()
    m.elision = elision
    m.graph = context_graph
    m.pins.graph.celltype = "plain"
    m.pins.result = {
        "io": "output",
        "celltype": "mixed",
        "hash_pattern": {
            "!": "#"
        }
    }

    ctx.inp = Context()
    ctx.cs_inp = Context()
    inp_prefix = "INPUT_"
    m.inp_prefix = inp_prefix
    m.pins.inp_prefix.celltype = "str"
    m.elision_chunksize = elision_chunksize
    m.pins.elision_chunksize.celltype = "int"
    for key in inp:
        c = Cell()
        ctx.inp[key] = c
        c.hash_pattern = {"!": "#"}
        inp[key].connect(c)
        ctx.cs_inp[key] = Cell("checksum")
        ctx.cs_inp[key] = ctx.inp[key]
        setattr(m, inp_prefix + key, ctx.cs_inp[key])

    macro_code_lib_code = libctx.map_list_N.value + "\n\n" + libctx.map_list_N_nested.value
    macro_code_lib = {
        "type": "interpreted",
        "language": "python",
        "code": macro_code_lib_code
    }
    ctx.macro_code_lib = Cell("plain").set(macro_code_lib)
    ctx.macro_code_lib_code = Cell("code").set(macro_code_lib_code)
    m.macro_code_lib = ctx.macro_code_lib
    m.pins.macro_code_lib.celltype = "plain"
    m.pins.macro_code_lib.subcelltype = "module"
    m.macro_code_lib_code = ctx.macro_code_lib_code
    m.pins.macro_code_lib_code.celltype = "plain"
    m.map_list_N_nested_code = libctx.map_list_N_nested.value
    ###m.pins.map_list_N_nested_code.celltype = "python"

    m.code = libctx.main.value
    ctx.result = Cell()
    ctx.result.hash_pattern = {"!": "#"}
    ctx.result = m.result
    result.connect_from(ctx.result)
Пример #2
0
def constructor(ctx, libctx, context_graph, inp, result):
    m = ctx.m = Macro()
    m.graph = context_graph
    m.pins.result = {
        "io": "output",
        "celltype": "mixed",
        "hash_pattern": {
            "!": "#"
        }
    }

    ctx.inp = Context()
    ctx.cs_inp = Context()
    inp_prefix = "INPUT_"
    m.inp_prefix = inp_prefix
    for key in inp:
        c = Cell()
        ctx.inp[key] = c
        c.hash_pattern = {"!": "#"}
        inp[key].connect(c)
        ctx.cs_inp[key] = Cell("checksum")
        ctx.cs_inp[key] = ctx.inp[key]
        setattr(m, inp_prefix + key, ctx.cs_inp[key])

    def map_list_N(ctx, inp_prefix, graph, **inp):
        first_k = list(inp.keys())[0]
        length = len(inp[first_k])
        first_k = first_k[len(inp_prefix):]
        for k0 in inp:
            k = k0[len(inp_prefix):]
            if len(inp[k0]) != length:
                err = "all cells in inp must have the same length, but '{}' has length {} while '{}' has length {}"
                raise ValueError(err.format(k, len(inp[k0]), first_k, length))

        from seamless.core import Cell as CoreCell
        from seamless.core.unbound_context import UnboundContext
        pseudo_connections = []
        ctx.result = cell("mixed", hash_pattern={"!": "#"})

        ctx.sc_data = cell("mixed", hash_pattern={"!": "#"})
        ctx.sc_buffer = cell("mixed", hash_pattern={"!": "#"})
        ctx.sc = StructuredCell(data=ctx.sc_data,
                                buffer=ctx.sc_buffer,
                                inchannels=[(n, ) for n in range(length)],
                                outchannels=[()],
                                hash_pattern={"!": "#"})

        for n in range(length):
            hc = HighLevelContext(graph)

            subctx = "subctx%d" % (n + 1)
            setattr(ctx, subctx, hc)

            if not hasattr(hc, "inp"):
                raise TypeError(
                    "map_list_N context must have a subcontext called 'inp'")
            hci = hc.inp
            if not isinstance(hci, UnboundContext):
                raise TypeError(
                    "map_list_N context must have an attribute 'inp' that is a context, not a {}"
                    .format(type(hci)))

            for k0 in inp:
                k = k0[len(inp_prefix):]
                if not hasattr(hci, k):
                    raise TypeError(
                        "map_list_N context must have a cell called inp.'{}'".
                        format(k))
                if isinstance(hci[k], StructuredCell):
                    raise TypeError(
                        "map_list_N context has a cell called inp.'{}', but its celltype must be mixed, not structured"
                        .format(k))
                if not isinstance(hci[k], CoreCell):
                    raise TypeError(
                        "map_list_N context must have an attribute inp.'{}' that is a cell, not a {}"
                        .format(k, type(hci[k])))
                if hci[k].celltype != "mixed":
                    raise TypeError(
                        "map_list_N context has a cell called inp.'{}', but its celltype must be mixed, not {}"
                        .format(k, hci[k].celltype))

                con = [".." + k], ["ctx", subctx, "inp", k]
                pseudo_connections.append(con)
                cs = inp[k0][n]
                hci[k].set_checksum(cs)

            resultname = "result%d" % (n + 1)
            setattr(ctx, resultname, cell("int"))
            c = getattr(ctx, resultname)
            hc.result.connect(c)
            c.connect(ctx.sc.inchannels[(n, )])
            con = ["ctx", subctx, "result"], ["..result"]
            pseudo_connections.append(con)

        ctx.sc.outchannels[()].connect(ctx.result)
        ctx._pseudo_connections = pseudo_connections

    m.code = map_list_N
    ctx.result = Cell()
    ctx.result.hash_pattern = {"!": "#"}
    ctx.result = m.result
    result.connect_from(ctx.result)