Ejemplo n.º 1
0
        public void TestConvertToInt32()
        {
            var a = new PyInt(5);

            Assert.IsInstanceOf(typeof(int), a.ToInt32());
            Assert.AreEqual(5, a.ToInt32());
        }
Ejemplo n.º 2
0
        public void TestCtorPyObject()
        {
            var i = new PyInt(5);
            var a = new PyInt(i);

            Assert.AreEqual(5, a.ToInt32());
        }
Ejemplo n.º 3
0
        public void TestCtorSByte()
        {
            const sbyte i = 5;
            var         a = new PyInt(i);

            Assert.AreEqual(i, a.ToInt32());
        }
Ejemplo n.º 4
0
        public void TestCtorUShort()
        {
            const ushort i = 5;
            var          a = new PyInt(i);

            Assert.AreEqual(i, a.ToInt32());
        }
Ejemplo n.º 5
0
        public void TestCtorULong()
        {
            const ulong i = 5;
            var         a = new PyInt(i);

            Assert.AreEqual(i, a.ToInt32());
        }
Ejemplo n.º 6
0
        public void TestCtorString()
        {
            const string i = "5";
            var          a = new PyInt(i);

            Assert.AreEqual(5, a.ToInt32());
        }
Ejemplo n.º 7
0
        public static List <T> GetListFromCollection <T>(dynamic pyCollection, ref List <T> lstMember) where T : new()
        {
            if (lstMember != null)
            {
                return(lstMember);
            }

            using (Py.GIL())
            {
                lstMember = new List <T>();

                dynamic builtins = Py.Import("builtins");
                var     pyCount  = new PyInt(builtins.len(pyCollection));
                var     count    = pyCount.ToInt32();

                for (var i = 0; i < count; i++)
                {
                    var element = pyCollection[i];

                    Binder       binder     = null;
                    BindingFlags flags      = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
                    CultureInfo  culture    = null;
                    var          parameters = new object[] { element };

                    lstMember.Add((T)Activator.CreateInstance(typeof(T), flags, binder, parameters, culture));
                }

                return(lstMember);
            }
        }
Ejemplo n.º 8
0
        public void TestCtorPtr()
        {
            var i = new PyInt(5);
            var a = new PyInt(i.Handle);

            Assert.AreEqual(5, a.ToInt32());
        }
Ejemplo n.º 9
0
        private void Copy(DocBin docBin)
        {
            _docs = docBin._docs;

            // I'd rather copy Python object no matter the serialization mode
            // If set to DotNet, the variable will be initialized to null
            // disregarding its current value which might be a default object
            _pyDocBin = docBin._pyDocBin;

            if (Serialization.Selected == Serialization.Mode.SpacyAndDotNet)
            {
                using (Py.GIL())
                {
                    dynamic spacy = Py.Import("spacy");

                    dynamic pyVocab = spacy.vocab.Vocab.__call__();
                    dynamic pyDocs  = _pyDocBin.get_docs(pyVocab);

                    dynamic builtins = Py.Import("builtins");
                    dynamic listDocs = builtins.list(pyDocs);

                    var pyCount = new PyInt(builtins.len(listDocs));
                    var count   = pyCount.ToInt32();

                    for (var i = 0; i < count; i++)
                    {
                        dynamic pyDoc = listDocs[i];
                        _docs[i].PyDoc         = pyDoc;
                        _docs[i].Vocab.PyVocab = pyDoc.vocab;
                    }
                }
            }
        }
Ejemplo n.º 10
0
        public void TestCtorPyObject()
        {
            var i = new PyInt(5);

            Runtime.Runtime.XIncref(i.Handle);
            var a = new PyInt(i);

            Assert.AreEqual(5, a.ToInt32());
        }
Ejemplo n.º 11
0
        public static int GetInt(dynamic dynIntPyObj, ref int?intMember)
        {
            if (intMember != null)
            {
                return((int)intMember);
            }

            using (Py.GIL())
            {
                var intPy = new PyInt(dynIntPyObj);
                intMember = intPy.ToInt32();
                return((int)intMember);
            }
        }
Ejemplo n.º 12
0
        public static bool GetBool(dynamic dynBoolPyObj, ref bool?boolMember)
        {
            if (boolMember != null)
            {
                return((bool)boolMember);
            }

            using (Py.GIL())
            {
                var boolPyInt = new PyInt(dynBoolPyObj);
                boolMember = boolPyInt.ToInt32() != 0;
                return((bool)boolMember);
            }
        }
Ejemplo n.º 13
0
        public static List <T> GetListFromList <T>(dynamic pyList, ref List <T> lstMember)
        {
            if (lstMember != null)
            {
                return(lstMember);
            }

            using (Py.GIL())
            {
                lstMember = new List <T>();

                dynamic builtins = Py.Import("builtins");
                var     pyCount  = new PyInt(builtins.len(pyList));
                var     count    = pyCount.ToInt32();

                for (var i = 0; i < count; i++)
                {
                    var element = pyList[i];

                    object created = null;
                    if (typeof(T) == typeof(string))
                    {
                        var pyObj = new PyString(element);
                        created = pyObj.ToString();
                    }
                    else
                    {
                        Debug.Assert(false);
                        return(null);
                    }

                    lstMember.Add((T)created);
                }

                return(lstMember);
            }
        }