Пример #1
0
 def _update(self, value):
     from silk import Silk
     from silk.mixed.get_form import get_form
     from IPython.display import clear_output
     outdated = False
     tcelltype, tmimetype = self.traitlet.celltype, self.traitlet.mimetype
     if tcelltype is not None and tcelltype != self.celltype or tmimetype != self.mimetype:
         DOC, DOC_name, params = select_DOC(tcelltype, tmimetype)
         if DOC_name != self.DOC_name:
             outdated = True
         elif params is not None:
             outdated = True
         else:
             self.celltype = tcelltype
             self.mimetype = tmimetype
     if outdated:
         value = "<Outdated>"
     if isinstance(value, Silk):
         value = value.unsilk
     try:
         form, _ = get_form(value)
         if form == "pure-plain":
             pass
         elif form == "pure-binary":
             value = value.tolist()
     except:
         value = "<Cannot be displayed>"
     if self.DOC_name == "Pretty":
         value = str(value)
     self.value = value
     self.refresh()
Пример #2
0
 def _inputsetter(self, attr, value):
     if attr in ("value", "data", "buffered", "checksum", "handle",
                 "schema", "example", "status", "exception",
                 "add_validator"):
         raise AttributeError(attr)
     if isinstance(value, Cell):
         raise TypeError(value)
     htf = self._get_htf()
     if not self._has_tf():
         if isinstance(value, Resource):
             value = value.data
         if "TEMP" not in htf or htf["TEMP"] is None:
             htf["TEMP"] = {}
         if "input_auth" not in htf["TEMP"]:
             htf["TEMP"]["input_auth"] = {}
         get_form(value)
         htf["TEMP"]["input_auth"][attr] = value
         self._parent()._translate()
         return
     tf = self._get_tf(force=True)
     inputcell = getattr(tf, htf["INPUT"])
     handle = inputcell.handle_no_inference
     setattr(handle, attr, value)
     self._parent()._translate()
Пример #3
0
            return json.loads(sdata)
        except ValueError:
            return sdata


old_cwd = os.getcwd()
try:
    process = None
    tempdir = tempfile.mkdtemp(prefix="seamless-bash-transformer")
    os.chdir(tempdir)
    signal.signal(signal.SIGTERM, sighandler)
    for pin in pins_:
        v = PINS[pin]
        if isinstance(v, Silk):
            v = v.unsilk
        storage, form = get_form(v)
        if storage.startswith("mixed"):
            raise TypeError("pin '%s' has '%s' data" % (pin, storage))
        if storage == "pure-plain":
            if isinstance(form, str):
                vv = str(v)
                if not vv.endswith("\n"): vv += "\n"
                if len(vv) <= 1000:
                    env[pin] = vv
            else:
                vv = json.dumps(v)
            with open(pin, "w") as pinf:
                pinf.write(vv)
        elif isinstance(v, bytes):
            with open(pin, "bw") as pinf:
                pinf.write(v)
Пример #4
0

def print(*args):
    for a in args:
        _print(json.dumps(a, indent=2, sort_keys=True), end=" ")
    _print()


dt = np.dtype([
    ("a", int),
    ("b", object),
], align=True)
data = np.zeros(2, dtype=dt)
data[0]["b"] = [1, 2, 3]
data[1]["b"] = [4, 5, 6]
storage, form = get_form(data)
print(storage, form)
print()

data[1]["b"] = [4.0, 5.0, 6.0]
storage, form = get_form(data)
print(storage, form)
print()

arr = np.zeros(10)

data = [arr, arr, arr]
storage, form = get_form(data)
print(storage, form)
print()
Пример #5
0
    def _setattr(self, attr, value):
        from .assign import assign_connection
        translate = False
        parent = self._parent()
        node = self._get_node()

        if attr != "code":
            if attr not in node["pins"]:
                node["pins"][attr] = default_pin.copy()
            else:
                if not isinstance(value, Cell):
                    pin = node["pins"][attr]
                    if pin["io"] != "parameter":
                        raise Exception(
                            "Can only assign value to parameter pin")

        if isinstance(value, Resource):
            assert attr == "code"
            self._sub_mount(attr, value.filename, "r", "file", True)

        if not self._has_mctx() and not isinstance(value, Cell):
            if isinstance(value, Resource):
                value = value.data
            if "TEMP" not in node or node["TEMP"] is None:
                node["TEMP"] = {}
            if "param_auth" not in node["TEMP"]:
                node["TEMP"]["param_auth"] = {}
            if attr == "code":
                code = value
                if callable(value):
                    code, _, _ = parse_function_code(value)
                node["TEMP"]["code"] = code
            else:
                get_form(value)
                node["TEMP"]["param_auth"][attr] = value
            self._parent()._translate()
            return

        if attr == "code":
            if isinstance(value, Cell):
                target_path = self._path + (attr, )
                assert value._parent() is parent
                assign_connection(parent, value._path, target_path, False)
                translate = True
            elif isinstance(value, Resource):
                mctx = self._get_mctx(force=True)
                mctx.code.set(value.data)
                translate = True
            elif isinstance(value, Proxy):
                raise AttributeError("".join(value._path))
            else:
                mctx = self._get_mctx(force=True)
                if callable(value):
                    value, _, _ = parse_function_code(value)
                mctx.code.set(value)
        else:
            if attr not in node["pins"]:
                node["pins"][attr] = default_pin.copy()
                translate = True
            pin = node["pins"][attr]
            if pin["io"] == "output":
                raise AttributeError(
                    "Cannot assign to output pin '{}'".format(attr))
            if isinstance(value, Cell):
                target_path = self._path + (attr, )
                assert value._parent() is parent
                assign_connection(parent, value._path, target_path, False)
                translate = True
            else:
                mctx = self._get_mctx(force=True)
                param = getattr(mctx, node["PARAM"])
                removed = parent.remove_connections(self._path + (attr, ))
                if removed:
                    translate = True
                setattr(param.handle_no_inference, attr, value)
        if translate:
            parent._translate()
Пример #6
0
    def _setattr(self, attr, value):
        from .assign import assign_connection
        translate = False
        parent = self._parent()
        htf = self._get_htf()

        if isinstance(value, Resource):
            assert attr == "code"
            self._sub_mount(attr, value.filename, "r", "file", True)

        if attr == "main_module" and htf["compiled"] and attr not in htf[
                "pins"]:
            raise TypeError(
                "Cannot assign directly all module objects; assign individual elements"
            )

        if not self._has_tf() and not isinstance(
                value, (Cell, Module)) and attr != htf["RESULT"]:
            if isinstance(value, Resource):
                value = value.data
            if "TEMP" not in htf or htf["TEMP"] is None:
                htf["TEMP"] = {}
            if "input_auth" not in htf["TEMP"]:
                htf["TEMP"]["input_auth"] = {}
            if attr == "code":
                code = value
                if callable(value):
                    code, _, _ = parse_function_code(value)
                htf["TEMP"]["code"] = code
            else:
                get_form(value)
                htf["TEMP"]["input_auth"][attr] = value
                if attr not in htf["pins"]:
                    htf["pins"][attr] = default_pin.copy()
            parent = self._parent()
            parent.remove_connections(self._path + (attr, ), endpoint="target")
            parent._translate()
            return

        if attr == "code":
            if isinstance(value, Resource):
                value = value.data
            if isinstance(value, Cell):
                target_path = self._path + (attr, )
                assert value._parent() is parent
                assign_connection(parent, value._path, target_path, False)
                translate = True
            elif isinstance(value, Proxy):
                raise AttributeError("".join(value._path))
            else:
                tf = self._get_tf(force=True)
                if callable(value):
                    value, _, _ = parse_function_code(value)
                check_libinstance_subcontext_binding(parent, self._path)
                removed = parent.remove_connections(self._path + (attr, ),
                                                    endpoint="target")
                if removed:
                    htf = self._get_htf()
                    htf["UNTRANSLATED"] = True
                    if "TEMP" not in htf or htf["TEMP"] is None:
                        htf["TEMP"] = {}
                    htf["TEMP"]["code"] = value
                    if "checksum" in htf:
                        htf["checksum"].pop("code", None)
                    translate = True
                else:
                    tf.code.set(value)
        elif attr == htf["INPUT"]:
            target_path = self._path
            if isinstance(value, Cell):
                assert value._parent() is parent
                exempt = self._exempt()
                assign_connection(parent,
                                  value._path,
                                  target_path,
                                  False,
                                  exempt=exempt)
                translate = True
            else:
                tf = self._get_tf(force=True)
                inp = getattr(tf, htf["INPUT"])
                removed = parent.remove_connections(self._path + (attr, ),
                                                    endpoint="target")
                if removed:
                    translate = True
                inp.handle_no_inference.set(value)
        elif attr == htf["RESULT"]:
            tf = self._get_tf(force=True)
            result = getattr(tf, htf["RESULT"])
            # Example-based programming to set the schema
            # TODO: suppress inchannel warning
            result.handle_no_inference.set(value)
        else:
            if attr not in htf["pins"]:
                if isinstance(value, Module):
                    pin = {
                        "celltype": "plain",
                        "subcelltype": "module",
                    }
                else:
                    pin = default_pin.copy()
                htf["pins"][attr] = pin
                translate = True
            if isinstance(value, (Cell, Module)):
                target_path = self._path + (attr, )
                assert value._parent() is parent
                assign_connection(parent, value._path, target_path, False)
                translate = True
            else:
                tf = self._get_tf(force=True)
                inp = getattr(tf, htf["INPUT"])
                removed = parent.remove_connections(self._path + (attr, ),
                                                    endpoint="target")
                if removed:
                    translate = True
                setattr(inp.handle_no_inference, attr, value)
        if translate:
            parent._translate()
Пример #7
0
import sys
import numpy as np
from silk.mixed .get_form import get_form
import json

_print = print
def print(*args):
    for a in args:
        _print(json.dumps(a, indent=2, sort_keys=True), end=" ")
    _print()

storage, form = get_form(1)
print(storage, form); print()

storage, form = get_form("test")
print(storage, form); print()

storage, form = get_form(["test", "test2"])
print(storage, form); print()

storage, form = get_form([1, 10.0])
print(storage, form); print()

storage, form = get_form(["test", 1, True])
print(storage, form); print()

storage, form = get_form({
    "a": None,
    "b": False,
    "c": 10,
    "d": 20.0,
Пример #8
0
 def form(self):
     from silk.mixed.get_form import get_form
     v = super().value
     if v is None:
         return None
     return get_form(v)[1]
Пример #9
0
    ("c", object),
], align=True)

assert dt1.isalignedstruct
assert dt2.isalignedstruct
assert dt1["c"].subdtype

d1 = np.zeros(1, dt1)[0]
d2 = np.zeros(1, dt2)[0]
d2["c"] = np.zeros(3, float)

d1["a"] = d2["a"] = 10
d1["b"] = d2["b"] = 20
d1["c"][:] = d2["c"][:] = range(100, 103)

storage1, form1 = get_form(d1)
storage2, form2 = get_form(d2)

print(storage1, form1)
print(storage2, form2)

stream1 = to_stream(d1, storage1, form1)
#print(stream1)
stream2 = to_stream(d2, storage2, form2)
#print(stream2)

newd1 = from_stream(stream1, storage1, form1)
print(newd1.tobytes() == d1.tobytes())
newd2 = from_stream(stream2, storage2, form2)
print(newd2["a"] == d2["a"])
print(newd2["b"] == d2["b"])