Exemplo n.º 1
0
        public static PySignature GetSignature(PyScope scope, string functionName)
        {
            var s = new PySignature();

            PyObject fn = scope.Get(functionName);

            var inspect    = Py.Import("inspect");
            var signature  = inspect.InvokeMethod("signature", fn);
            var parameters = signature.GetAttr("parameters");

            var keys          = parameters.InvokeMethod("keys");
            var argumentNames = new List <string>();

            foreach (var key in keys.OfType <PyObject>())
            {
                var p    = parameters[key];
                var name = p.GetAttr("name").As <string>();
                argumentNames.Add(name);
            }

            PyDict annotations = new PyDict(fn.GetAttr("__annotations__"));

            foreach (var argumentName in argumentNames)
            {
                Type type;
                if (annotations.HasKey(argumentName))
                {
                    var pyType = annotations.GetItem(argumentName);
                    type = ToClrType(pyType);
                }
                else
                {
                    type = typeof(object);
                }
                s.Arguments.Add(new PyArgument {
                    Name = argumentName, Type = type
                });
            }

            Type returnType;

            if (annotations.HasKey("return"))
            {
                var returnPyType = annotations.GetItem("return");
                returnType = ToClrType(returnPyType);
            }
            else
            {
                returnType = typeof(object);
            }

            s.ReturnType = returnType;

            return(s);
        }
Exemplo n.º 2
0
        public IEnumerable <YouTubeDLInfo> GetInfo(string url)
        {
            EnsureModuleExists();

            var extractor = GetExtractor(url);
            var instance  = GetInstance(extractor);

            var result = new List <YouTubeDLInfo>();

            using (Py.GIL())
            {
                var info = new PyDict(instance.InvokeMethod("extract_info", url.ToPython(), false.ToPython()));
                if (info.HasKey("entries"))
                {
                    foreach (PyObject item in new PyList(info.GetItem("entries")))
                    {
                        result.Add(YouTubeDLInfo.FromResult(new PyDict(item), info));
                    }
                }
                else
                {
                    result.Add(YouTubeDLInfo.FromResult(info, info));
                }
            }
            return(result);
        }
Exemplo n.º 3
0
 private static T GetKey <T>(PyDict obj, string key)
 {
     if (obj.HasKey(key))
     {
         return(obj.GetItem(key).As <T>());
     }
     return(default);
Exemplo n.º 4
0
 public void ClearPlot()
 {
     using (Py.GIL())
     {
         if (Local.HasKey("fig"))
         {
             Python.PushLocal(Local);
             Python.RunString(
                 "fig.clear()\n" +
                 "plt.close(fig)\n" +
                 "del fig\n" +
                 "gc.collect()"
                 );
             Local = Python.PopLocal();
         }
     }
 }