Import() 공개 정적인 메소드

public static Import ( string name ) : PyObject
name string
리턴 PyObject
예제 #1
0
 public static TextWriter ToTextWriter(TextWriter writer = null)
 {
     using (Py.GIL())
     {
         SysIOStream.TextWriter = writer;
         dynamic sys = Py.Import("sys");
         sys.stdout = sys.stderr = SysIOStream;
     }
     return(SysIOStream.TextWriter);
 }
예제 #2
0
 public static InteropConfiguration MakeDefault()
 {
     return(new InteropConfiguration
     {
         PythonBaseTypeProviders =
         {
             DefaultBaseTypeProvider.Instance,
             new CollectionMixinsProvider(new Lazy <PyObject>(() => Py.Import("clr._extras.collections"))),
         },
     });
 }
예제 #3
0
        public static void Initialize(string backend = null)
        {
            np         = Py.Import("numpy");
            matplotlib = Py.Import("matplotlib");
            if (!String.IsNullOrEmpty(backend))
            {
                // backend must be set before pylab be imported
                // It can not be changed after pylab be imported
                PythonEngine.Exec($"import matplotlib;matplotlib.use('{backend}')");
            }
            plt           = Py.Import("matplotlib.pylab");
            PltFigureType = plt.GetAttr("Figure").Handle;
            var io = Py.Import("io");

            BytesIO = io.GetAttr("BytesIO");
            PythonEngine.Exec("import matplotlib;print(matplotlib.get_backend())");
        }
예제 #4
0
        public static void Initialize()
        {
            np             = Py.Import("numpy");
            NumpyArrayType = np.GetAttr("ndarray").Handle;
            np_dtypes.Clear();
            np_dtypes.Add(typeof(byte), np.GetAttr("uint8"));
            np_dtypes.Add(typeof(short), np.GetAttr("int16"));
            np_dtypes.Add(typeof(int), np.GetAttr("int32"));
            np_dtypes.Add(typeof(long), np.GetAttr("int64"));
            np_dtypes.Add(typeof(ushort), np.GetAttr("uint16"));
            np_dtypes.Add(typeof(uint), np.GetAttr("uint32"));
            np_dtypes.Add(typeof(ulong), np.GetAttr("uint64"));
            np_dtypes.Add(typeof(float), np.GetAttr("float"));
            np_dtypes.Add(typeof(double), np.GetAttr("float64"));
            var copy = Py.Import("copy");

            deepcopy = copy.GetAttr("deepcopy");
        }
예제 #5
0
        internal virtual IntPtr Invoke(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, MethodInfo[] methodinfo)
        {
            Binding binding = Bind(inst, args, kw, info, methodinfo);
            object  result;
            IntPtr  ts = IntPtr.Zero;

            if (binding == null)
            {
                var value = new StringBuilder("No method matches given arguments");
                if (methodinfo != null && methodinfo.Length > 0)
                {
                    value.Append($" for {methodinfo[0].Name}");
                }

                long argCount = Runtime.PyTuple_Size(args);
                value.Append(": (");
                for (long argIndex = 0; argIndex < argCount; argIndex++)
                {
                    var arg = Runtime.PyTuple_GetItem(args, argIndex);
                    if (arg != IntPtr.Zero)
                    {
                        var type = Runtime.PyObject_Type(arg);
                        if (type != IntPtr.Zero)
                        {
                            try {
                                var description = Runtime.PyObject_Unicode(type);
                                if (description != IntPtr.Zero)
                                {
                                    value.Append(Runtime.GetManagedString(description));
                                    Runtime.XDecref(description);
                                }
                            } finally {
                                Runtime.XDecref(type);
                            }
                        }
                    }

                    if (argIndex + 1 < argCount)
                    {
                        value.Append(", ");
                    }
                }
                value.Append(')');
                Exceptions.SetError(Exceptions.TypeError, value.ToString());
                return(IntPtr.Zero);
            }

            //allow_threads = true breaks the code with passing pyobjects to clr functions.
            //I think this happens because python is not thread safe and objects cannot be called
            //different threads
            allow_threads = false;
            if (allow_threads)
            {
                Console.WriteLine("ALLOW THR");
                ts = PythonEngine.BeginAllowThreads();
            }

            try
            {
                result = binding.info.Invoke(binding.inst, BindingFlags.Default, null, binding.args, null);
            }
            catch (Exception e)
            {
                if (e.InnerException != null)
                {
                    e = e.InnerException;
                }
                if (allow_threads)
                {
                    PythonEngine.EndAllowThreads(ts);
                }
                Exceptions.SetError(e);
                return(IntPtr.Zero);
            }

            if (allow_threads)
            {
                PythonEngine.EndAllowThreads(ts);
            }

            // If there are out parameters, we return a tuple containing
            // the result followed by the out parameters. If there is only
            // one out parameter and the return type of the method is void,
            // we return the out parameter as the result to Python (for
            // code compatibility with ironpython).

            var mi = (MethodInfo)binding.info;

            if (binding.outs == 1 && mi.ReturnType == typeof(void))
            {
            }

            if (binding.outs > 0)
            {
                ParameterInfo[] pi = mi.GetParameters();
                int             c  = pi.Length;
                var             n  = 0;

                IntPtr t = Runtime.PyTuple_New(binding.outs + 1);
                IntPtr v = Converter.ToPython(result, mi.ReturnType);
                Runtime.PyTuple_SetItem(t, n, v);
                n++;

                for (var i = 0; i < c; i++)
                {
                    Type pt = pi[i].ParameterType;
                    if (pi[i].IsOut || pt.IsByRef)
                    {
                        v = Converter.ToPython(binding.args[i], pt);
                        Runtime.PyTuple_SetItem(t, n, v);
                        n++;
                    }
                }

                if (binding.outs == 1 && mi.ReturnType == typeof(void))
                {
                    v = Runtime.PyTuple_GetItem(t, 1);
                    Runtime.XIncref(v);
                    Runtime.XDecref(t);
                    return(v);
                }

                return(t);
            }


            if (mi.ReturnType == typeof(QuantApp.Kernel.JVM.JVMObject))
            {
                dynamic pymodule = Py.Import("clr");
                result = pymodule.createJVM(result);
            }

            return(Converter.ToPython(result, mi.ReturnType));
        }