Esempio n. 1
0
  def testCallback(self):

    class Foo(object):
      def __init__(self):
        self.x = 1

    class Bar(object):
      def __init__(self):
        self.foo = Foo()

    def serialize(obj):
      return dict(obj.__dict__, **{"_pytype_": type(obj).__name__})

    def deserialize(obj):
      if obj["_pytype_"] == "Foo":
        result = Foo()
      elif obj["_pytype_"] == "Bar":
        result = Bar()

      obj.pop("_pytype_", None)
      result.__dict__ = obj
      return result

    bar = Bar()
    bar.foo.x = 42

    numbuf.register_callbacks(serialize, deserialize)

    metadata, size, serialized = numbuf.serialize_list([bar])
    self.assertEqual(numbuf.deserialize_list(serialized)[0].foo.x, 42)
Esempio n. 2
0
    def testCallback(self):
        class Foo(object):
            def __init__(self):
                self.x = 1

        class Bar(object):
            def __init__(self):
                self.foo = Foo()

        def serialize(obj):
            return dict(obj.__dict__, **{"_pytype_": type(obj).__name__})

        def deserialize(obj):
            if obj["_pytype_"] == "Foo":
                result = Foo()
            elif obj["_pytype_"] == "Bar":
                result = Bar()

            obj.pop("_pytype_", None)
            result.__dict__ = obj
            return result

        bar = Bar()
        bar.foo.x = 42

        numbuf.register_callbacks(serialize, deserialize)

        metadata, size, serialized = numbuf.serialize_list([bar])
        self.assertEqual(numbuf.deserialize_list(serialized)[0].foo.x, 42)
Esempio n. 3
0
  def testObjectArray(self):
    x = np.array([1, 2, "hello"], dtype=object)
    y = np.array([[1, 2], [3, 4]], dtype=object)

    def myserialize(obj):
      return {"_pytype_": "numpy.array", "data": obj.tolist()}

    def mydeserialize(obj):
      if obj["_pytype_"] == "numpy.array":
        return np.array(obj["data"], dtype=object)

    numbuf.register_callbacks(myserialize, mydeserialize)

    metadata, size, serialized = numbuf.serialize_list([x, y])

    assert_equal(numbuf.deserialize_list(serialized), [x, y])
Esempio n. 4
0
    def testObjectArray(self):
        x = np.array([1, 2, "hello"], dtype=object)
        y = np.array([[1, 2], [3, 4]], dtype=object)

        def myserialize(obj):
            return {"_pytype_": "numpy.array", "data": obj.tolist()}

        def mydeserialize(obj):
            if obj["_pytype_"] == "numpy.array":
                return np.array(obj["data"], dtype=object)

        numbuf.register_callbacks(myserialize, mydeserialize)

        metadata, size, serialized = numbuf.serialize_list([x, y])

        assert_equal(numbuf.deserialize_list(serialized), [x, y])
Esempio n. 5
0
    """This is the callback that will be used by numbuf.

  If numbuf encounters a dictionary that contains the key "_pytype_" during
    deserialization, it will ask this callback to deserialize the object.

  Args:
    serialized_obj (object): A dictionary that contains the key "_pytype_".

  Returns:
    A Python object.
  """
    class_id = serialized_obj["_pytype_"]
    cls = whitelisted_classes[class_id]
    if class_id in classes_to_pickle:
        obj = pickling.loads(serialized_obj["data"])
    elif class_id in custom_deserializers.keys():
        obj = custom_deserializers[class_id](serialized_obj["data"])
    else:
        # In this case, serialized_obj should just be the __dict__ field.
        if "_ray_getnewargs_" in serialized_obj:
            obj = cls.__new__(cls, *serialized_obj["_ray_getnewargs_"])
        else:
            obj = cls.__new__(cls)
            serialized_obj.pop("_pytype_")
            obj.__dict__.update(serialized_obj)
    return obj


# Register the callbacks with numbuf.
numbuf.register_callbacks(serialize, deserialize)