def new(self, classname: str, *args): """ create a new CLR object :param classname: fully qualified or partially qualified class name (i.e. 'com.stg.models.MyClass' or 'MyClass') :param *args: optional arguments to the constructor """ ## create object request req = CLRCreateObject(classname, args) CLRMessage.write(self.cout, req) ## process result rep = CLRMessage.read(self.cin) return rep.toValue()
def getIndexed(self, objectId: int, idx: int): """ get indexed on CLR-side object (this is only used internally) :param objectId: object ID of previously created object :param idx: index to retrieve """ ## create object request req = CLRGetIndexed(objectId, idx) CLRMessage.write(self.cout, req) ## process result rep = CLRMessage.read(self.cin) return rep.toValue()
def getProperty(self, objectId: int, property: str): """ get property on CLR-side object (this is only used internally) :param objectId: object ID of previously created object :param property: name of property to get """ ## create object request req = CLRGetProperty(objectId, property) CLRMessage.write(self.cout, req) ## process result rep = CLRMessage.read(self.cin) return rep.toValue()
def call(self, objectId: int, methodname: str, *args): """ call method on CLR-side object (this is only used internally) :param objectId: object ID of previously created object :param methodname: name of method / function to call :param *args: arguments to method / function """ ## create object request req = CLRCallMethod(objectId, methodname, args) CLRMessage.write(self.cout, req) ## process result rep = CLRMessage.read(self.cin) return rep.toValue()
def callstatic(self, classname: str, methodname: str, *args): """ call class method on CLR-side object :param classname: fully qualified or partially qualified class name (i.e. 'com.stg.models.MyClass' or 'MyClass') :param methodname: name of method / function to call :param *args: arguments to method / function """ ## create object request req = CLRCallStaticMethod(classname, methodname, args) CLRMessage.write(self.cout, req) ## process result rep = CLRMessage.read(self.cin) return rep.toValue()
def deserialize(self, sock: BinarySocketReader): """ Deserialize message (called after magic & type read) """ super().deserialize(sock) self.classname = sock.readString() argc = sock.readInt16() self.argv = [CLRMessage.read(sock).value for i in range(0, argc)]
def deserialize(self, sock: BinarySocketReader): """ Deserialize message (called after magic & type read) """ super().deserialize(sock) self.objectId = sock.readInt32() self.property = sock.readString() self.setval = CLRMessage.read(sock)
def deserialize (self, sock: BinarySocketReader): """ Deserialize message (called after magic & type read) """ super().deserialize(sock) vlen = sock.readInt32() msgs = [CLRMessage.read(sock) for i in range(0,vlen)] self.value = [msg.toValue() for msg in msgs]
def serialize (self, sock: BinarySocketWriter): """ Serialize message """ super().serialize(sock) sock.writeInt32(len(self.value)) for v in self.value: msg = CLRMessage.createByValue(v) msg.serialize(sock)
def classFor(self, classname: str): """ get class template for named type (this is only used internally) """ klass = CLRApi.Classes.get(classname, None) if klass is not None: return klass else: ## create request req = CLRTemplateReq(classname) CLRMessage.write(self.cout, req) ## process result rep = CLRMessage.read(self.cin) info = rep.toValue() klass = CLRObject.proxyClassFor(classname, info) CLRApi.Classes[classname] = klass return klass
def test_create(self): import pydotnet.clr.CLRInitialization ## ints o1 = CLRMessage.createByObj(2) self.assertTrue(isinstance(o1, pydotnet.clr.data.CLRInt32)) ## floats o2 = CLRMessage.createByObj(3.14) self.assertTrue(isinstance(o2, pydotnet.clr.data.CLRFloat64)) ## boolean b1 = CLRMessage.createByObj(True) self.assertTrue(isinstance(b1, pydotnet.clr.data.CLRBool)) ## string s1 = CLRMessage.createByObj("this is a test") self.assertTrue(isinstance(s1, pydotnet.clr.data.CLRString)) ## vectors o2l = CLRMessage.createByObj([3.14, 1.34]) self.assertTrue(isinstance(o2l, pydotnet.clr.data.CLRVector)) o3l = CLRMessage.createByObj(np.array([3.14, 1.34])) self.assertTrue(isinstance(o3l, pydotnet.clr.data.CLRVector)) o4l = CLRMessage.createByObj(np.array([[3.14], [1.34]])) self.assertTrue(isinstance(o4l, pydotnet.clr.data.CLRVector)) o5l = CLRMessage.createByObj( pd.DataFrame([3.14, 1.34], index=['a', 'b'])) self.assertTrue(isinstance(o5l, pydotnet.clr.data.CLRVector)) o6l = CLRMessage.createByObj(pd.DataFrame([3.14, 1.34]).as_matrix()) self.assertTrue(isinstance(o6l, pydotnet.clr.data.CLRVector)) ## matrices m1 = CLRMessage.createByObj( pd.DataFrame([[2.0, 0, 0], [0, 3.0, 0], [0, 0, 23.0]], index=['a', 'b', 'c'], columns=['a', 'b', 'c'])) self.assertTrue(isinstance(m1, pydotnet.clr.data.CLRMatrix)) ## arrays a1 = CLRMessage.createByObj(np.array([1, 3, 5, 7], dtype=np.int32)) self.assertTrue(isinstance(a1, pydotnet.clr.data.CLRInt32Array)) a2 = CLRMessage.createByObj(np.array([1, 3, 5, 7], dtype=np.int64)) self.assertTrue(isinstance(a2, pydotnet.clr.data.CLRInt64Array)) a3 = CLRMessage.createByObj(["this", "that"]) self.assertTrue(isinstance(a3, pydotnet.clr.data.CLRStringArray)) a4 = CLRMessage.createByObj( pd.DataFrame([3.14, 1.34], index=['a', 'b']).index.values) self.assertTrue(isinstance(a4, pydotnet.clr.data.CLRStringArray))
def release(self, objectId: int): """ release an object for GC (this is only used internally) """ req = CLRRelease(objectId) CLRMessage.write(self.cout, req)