GetPythonType() public method

GetPythonType Method
Returns the Python type of the object. This method is equivalent to the Python expression: type(object).
public GetPythonType ( ) : PyObject
return PyObject
Exemplo n.º 1
0
        static bool TryConvertArgument(IntPtr op, Type parameterType, bool needsResolution,
                                       out object arg, out bool isOut)
        {
            arg   = null;
            isOut = false;
            var clrtype = TryComputeClrArgumentType(parameterType, op, needsResolution: needsResolution);

            if (clrtype == null)
            {
                return(false);
            }

            if (!Converter.ToManaged(op, clrtype, out arg, false))
            {
                // Arturo Rodriguez -- Start
                // Needed to allow Python objects to be passed from python code to CLR functions.
                var pyobj = new PyObject(op);
                var ptype = pyobj.GetPythonType();

                arg     = pyobj;
                clrtype = arg.GetType();
                isOut   = clrtype.IsByRef;
                return(true);
                // Arturo Rodriguez -- End

                // Repo code
                // Exceptions.Clear();
                // return false;
            }

            isOut = clrtype.IsByRef;
            return(true);
        }
Exemplo n.º 2
0
        public MethodBinding(MethodObject m, PyObject?target, PyType?targetType = null)
        {
            this.target = target;

            this.targetType = targetType ?? target?.GetPythonType();

            this.info = null;
            this.m    = m;
        }
Exemplo n.º 3
0
        private static IntPtr DoInstanceCheck(IntPtr tp, IntPtr args, bool checkType)
        {
            var cb = GetManagedObject(tp) as ClassBase;

            if (cb == null)
            {
                Runtime.XIncref(Runtime.PyFalse);
                return(Runtime.PyFalse);
            }

            Runtime.XIncref(args);
            using (var argsObj = new PyList(args))
            {
                if (argsObj.Length() != 1)
                {
                    return(Exceptions.RaiseTypeError("Invalid parameter count"));
                }

                PyObject arg = argsObj[0];
                PyObject otherType;
                if (checkType)
                {
                    otherType = arg;
                }
                else
                {
                    otherType = arg.GetPythonType();
                }

                if (Runtime.PyObject_TYPE(otherType.Handle) != PyCLRMetaType)
                {
                    Runtime.XIncref(Runtime.PyFalse);
                    return(Runtime.PyFalse);
                }

                var otherCb = GetManagedObject(otherType.Handle) as ClassBase;
                if (otherCb == null)
                {
                    Runtime.XIncref(Runtime.PyFalse);
                    return(Runtime.PyFalse);
                }

                return(Converter.ToPython(cb.type.IsAssignableFrom(otherCb.type)));
            }
        }
Exemplo n.º 4
0
        public static byte[] SaveFigureToArray(PyObject fig, int dpi = 200, string format = "png")
        {
            if (fig.GetPythonType().Handle != PltFigureType)
            {
                throw new Exception("object is not a matplotlib Figure");
            }

            dynamic _np = np;
            //buf = io.BytesIO()
            dynamic buf = BytesIO.Invoke();

            //fig.savefig(buf, dpi=__dpi__, format='png')
            fig.InvokeMethod("savefig", new PyTuple(new PyObject[] { buf }), Py.kw("dpi", dpi, "format", format));
            var buf_out = _np.array(buf.getbuffer(), Py.kw("dtype", Numpy.GetNumpyDataType(typeof(byte))));
            var arr     = Numpy.ToArray(buf_out);

            return((byte[])arr);
        }
        public object ToClr(PyObject obj, Type t = null)
        {
            if (obj == null)
            {
                return(null);
            }
            PyObject type = obj.GetPythonType();
            Dictionary <Type, PyClrTypeBase> converters;
            var state = PythonConverters.TryGetValue(type.Handle, out converters);

            if (!state)
            {
                throw new Exception($"Type {type.ToString()} not recognized");
            }
            if (t == null || !converters.ContainsKey(t))
            {
                return(converters.Values.First().ToClr(obj));
            }
            else
            {
                return(converters[t].ToClr(obj));
            }
        }
Exemplo n.º 6
0
            public NumpyArrayInterface(PyObject o)
            {
                if (o.GetPythonType().Handle != NumpyArrayType)
                {
                    throw new Exception("object is not a numpy array");
                }
                var meta = o.GetAttr("__array_interface__");

                IsCStyleContiguous = meta["strides"] == null;
                Address            = new System.IntPtr((long)meta["data"][0].As <long>());

                var typestr = meta["typestr"].As <string>();
                var dtype   = typestr.Substring(1);

                switch (dtype)
                {
                case "b1":
                    DataType = typeof(bool);
                    break;

                case "f4":
                    DataType = typeof(float);
                    break;

                case "f8":
                    DataType = typeof(double);
                    break;

                case "i2":
                    DataType = typeof(short);
                    break;

                case "i4":
                    DataType = typeof(int);
                    break;

                case "i8":
                    DataType = typeof(long);
                    break;

                case "u1":
                    DataType = typeof(byte);
                    break;

                case "u2":
                    DataType = typeof(ushort);
                    break;

                case "u4":
                    DataType = typeof(uint);
                    break;

                case "u8":
                    DataType = typeof(ulong);
                    break;

                default:
                    throw new NumpyException($"type '{dtype}' not supported");
                }
                Shape  = o.GetAttr("shape").As <long[]>();
                NBytes = o.GetAttr("nbytes").As <int>();
            }