Beispiel #1
0
        /// <summary>
        ///     Get a PyObject from a list
        /// </summary>
        /// <typeparam name="TItem"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public PyObject From <TItem>(IEnumerable <TItem> value)
        {
            var result = new PyObject(this, Py.PyList_New(value.Count()), true);

            for (var i = 0; i < value.Count(); i++)
            {
                var pyItem = From(value.ElementAt(i));

                if (pyItem == null)
                {
                    return(PyZero);
                }

                // PyList_SetItem steals a reference, this makes sure we dont free it later
                Py.Py_IncRef(pyItem);

                if (Py.PyList_SetItem(result, i, pyItem) == -1)
                {
                    return(PyZero);
                }
            }

            return(result);
        }
Beispiel #2
0
        /// <summary>
        ///     Clear this PyObject (the PyObject must be a List or Dictionary)
        /// </summary>
        /// <param name="pyType">Force this Python Type</param>
        /// <returns></returns>
        /// <remarks>
        ///     For a list attribute, the list is cleared
        ///     For a Dictionary attribute, the dictionary is cleared
        /// </remarks>
        public bool Clear(PyType pyType)
        {
            try
            {
                switch (pyType)
                {
                case PyType.ListType:
                case PyType.DerivedListType:
                    return(Py.PyList_SetSlice(this, 0, Size() - 1, PySharp.PyZero) == 0);

                case PyType.DictType:
                case PyType.DictProxyType:
                case PyType.DerivedDictType:
                case PyType.DerivedDictProxyType:
                    return(ToDictionary().All(item => Py.PyDict_DelItem(this, item.Key) == 0));
                }

                return(false);
            }
            finally
            {
                HandlePythonError();
            }
        }
Beispiel #3
0
 /// <summary>
 ///     Get a PyObject from a double
 /// </summary>
 /// <param name="value"></param>
 /// <returns></returns>
 public PyObject From(double value)
 {
     // Note: Caching double's has no use due to rounding errors
     return(new PyObject(this, Py.PyFloat_FromDouble(value), true));
 }
Beispiel #4
0
        /// <summary>
        ///     Set an attribute value
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="attribute"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool SetAttribute <T>(string attribute, T value)
        {
            var pyValue = PySharp.PyZero;

            var oValue = (object)value;

            if (value is PyObject)
            {
                pyValue = (PyObject)oValue;
            }

            // Only allow type conversions if we have a PySharp object reference
            if (_pySharp != null)
            {
                if (oValue is bool)
                {
                    pyValue = _pySharp.From((bool)oValue);
                }
                if (oValue is int)
                {
                    pyValue = _pySharp.From((int)oValue);
                }
                if (oValue is long)
                {
                    pyValue = _pySharp.From((long)oValue);
                }
                if (oValue is float)
                {
                    pyValue = _pySharp.From((float)oValue);
                }
                if (oValue is double)
                {
                    pyValue = _pySharp.From((double)oValue);
                }
                if (oValue is string)
                {
                    pyValue = _pySharp.From((string)oValue);
                }
                if (oValue is IEnumerable <PyObject> )
                {
                    pyValue = _pySharp.From((IEnumerable <PyObject>)oValue);
                }
                if (oValue is IEnumerable <int> )
                {
                    pyValue = _pySharp.From((IEnumerable <int>)oValue);
                }
                if (oValue is IEnumerable <long> )
                {
                    pyValue = _pySharp.From((IEnumerable <long>)oValue);
                }
                if (oValue is IEnumerable <float> )
                {
                    pyValue = _pySharp.From((IEnumerable <float>)oValue);
                }
                if (oValue is IEnumerable <double> )
                {
                    pyValue = _pySharp.From((IEnumerable <double>)oValue);
                }
                if (oValue is IEnumerable <string> )
                {
                    pyValue = _pySharp.From((IEnumerable <string>)oValue);
                }
            }

            try
            {
                if (IsValid && !pyValue.IsNull)
                {
                    return(Py.PyObject_SetAttrString(this, attribute, pyValue) != -1);
                }

                return(false);
            }
            finally
            {
                HandlePythonError();
            }
        }
Beispiel #5
0
        /// <summary>
        ///     Call this PyObject as a python function
        /// </summary>
        /// <param name="keywords"></param>
        /// <param name="parms"></param>
        /// <returns></returns>
        public PyObject CallThisWithKeywords(Dictionary <string, object> keywords, params object[] parms)
        {
            if (!IsValid)
            {
                return(PySharp.PyZero);
            }

            if (_pySharp == null)
            {
                throw new NotImplementedException();
            }

            var pyKeywords = PySharp.PyZero;

            if (keywords != null && keywords.Keys.Any())
            {
                pyKeywords = new PyObject(_pySharp, Py.PyDict_New(), true);
                foreach (var item in keywords)
                {
                    var pyValue = _pySharp.From(item.Value);

                    if (pyValue == null || pyValue.IsNull)
                    {
                        throw new NotImplementedException();
                    }

                    Py.PyDict_SetItem(pyKeywords, _pySharp.From(item.Key), pyValue);
                }
            }

            var pyParms = new List <PyObject>();

            foreach (var parm in parms)
            {
                var pyParm = _pySharp.From(parm);

                if (pyParm == null || pyParm.IsNull)
                {
                    throw new NotImplementedException();
                }

                // Fail if any parameter is invalid (PyNone is a valid parameter)
                if (pyParm.IsNull)
                {
                    return(PySharp.PyZero);
                }

                pyParms.Add(pyParm);
            }

            var      format = "(" + string.Join("", pyParms.Select(dummy => "O").ToArray()) + ")";
            PyObject pyArgs = null;

            if (pyParms.Count == 0)
            {
                pyArgs = new PyObject(_pySharp, Py.Py_BuildValue(format), true);
            }
            if (pyParms.Count == 1)
            {
                pyArgs = new PyObject(_pySharp, Py.Py_BuildValue(format, pyParms[0]), true);
            }
            if (pyParms.Count == 2)
            {
                pyArgs = new PyObject(_pySharp, Py.Py_BuildValue(format, pyParms[0], pyParms[1]), true);
            }
            if (pyParms.Count == 3)
            {
                pyArgs = new PyObject(_pySharp, Py.Py_BuildValue(format, pyParms[0], pyParms[1], pyParms[2]), true);
            }
            if (pyParms.Count == 4)
            {
                pyArgs = new PyObject(_pySharp, Py.Py_BuildValue(format, pyParms[0], pyParms[1], pyParms[2], pyParms[3]), true);
            }
            if (pyParms.Count == 5)
            {
                pyArgs = new PyObject(_pySharp, Py.Py_BuildValue(format, pyParms[0], pyParms[1], pyParms[2], pyParms[3], pyParms[4]), true);
            }
            if (pyParms.Count == 6)
            {
                pyArgs = new PyObject(_pySharp, Py.Py_BuildValue(format, pyParms[0], pyParms[1], pyParms[2], pyParms[3], pyParms[4], pyParms[5]), true);
            }
            if (pyParms.Count == 7)
            {
                pyArgs = new PyObject(_pySharp, Py.Py_BuildValue(format, pyParms[0], pyParms[1], pyParms[2], pyParms[3], pyParms[4], pyParms[5], pyParms[6]), true);
            }

            if (pyArgs == null)
            {
                throw new NotImplementedException();
            }

            return(new PyObject(_pySharp, Py.PyEval_CallObjectWithKeywords(this, pyArgs, pyKeywords), true));
        }