Пример #1
0
        public static object Decode(PyObject py)
        {
            // Let's aasume that when python use big endian, dotnet also and respectively with little endian
            var dType    = py.GetAttr("dtype");
            var kind     = dType.GetAttr("kind").As <char>();
            var itemSize = dType.GetAttr("itemsize").As <int>();

            switch (kind)
            {
            case 'i':     // integer
                switch (itemSize)
                {
                case 1:         // sbyte
                    return(py.DecodeFast <sbyte>(Decode));

                case 2:         // short
                    return(py.DecodeFast <short>(Decode));

                case 4:         // int
                    return(py.DecodeFast <int>(Decode));

                case 8:         // long
                    return(py.DecodeFast <long>(Decode));
                }
                break;

            case 'b':     // boolean
                return(py.DecodeFast <bool>(Decode));

            case 'u':     // unsigned integer
                switch (itemSize)
                {
                case 1:         // byte
                    return(py.DecodeFast <byte>(Decode));

                case 2:         // ushort
                    return(py.DecodeFast <ushort>(Decode));

                case 4:         // uint
                    return(py.DecodeFast <uint>(Decode));

                case 8:         // ulong
                    return(py.DecodeFast <ulong>(Decode));
                }
                break;

            case 'f':     // floating number
                switch (itemSize)
                {
                case 4:         // float
                    return(py.DecodeFast <float>(Decode));

                case 8:         // double
                    return(py.DecodeFast <double>(Decode));
                }
                break;

            case 'c':     // complex number
                break;

            case 'm':     // timedelta
                return(py.DecodeFast <TimeSpan>((p, a) => Decode(p, dType.GetTimeFactor(), a)));

            case 'M':     // datetime
                return(py.DecodeFast <DateTime>((p, a) => Decode(p, dType.GetTimeFactor(), a)));

            case 'O':                             // object
                return(py.DecodeSafe <string>()); // Todo: do better here if it's not necessary a string

            case 'S':                             // string
                return(py.DecodeSafe <string>()); // Obsolete since python 3

            case 'U':                             // string as unicode
                return(py.DecodeSafe <string>());

            case 'V':     // fixed chunk of memory (void)
                break;
            }

            throw new NotSupportedException($"numpy dtypr: {dType}, Kind: {kind}, ItemSize: {itemSize} is not supported");
        }