Esempio n. 1
0
        public void TestCtorSByte()
        {
            const sbyte i = 5;
            var         a = new PyInt(i);

            Assert.AreEqual(i, a.ToInt32());
        }
Esempio n. 2
0
        public PyList getHandFeature(PlayerKeyInfo ownInfo, PlayerKeyInfo enemyInfo)
        {
            int[] featureArray = new int[cardArray.Length * 2];
            foreach (PlayerKeyInfo.CardKeyInfo hc in ownInfo.handcardJsonList)
            {
                int idx = cardIdxDict[CardDB.Instance.cardNamestringToEnum(hc.cardName)];
                featureArray[idx] = featureArray[idx] + 1;
            }

            foreach (PlayerKeyInfo.CardKeyInfo hc in enemyInfo.handcardJsonList)
            {
                int idx = cardIdxDict[CardDB.Instance.cardNamestringToEnum(hc.cardName)] + cardArray.Length;
                featureArray[idx] = featureArray[idx] + 1;
            }

            PyList hand_feature = new PyList();

            foreach (int ft in featureArray)
            {
                PyInt num = new PyInt(ft);
                hand_feature.Append(num);
                num.Dispose();
            }

            return(hand_feature);
        }
Esempio n. 3
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;
                    }
                }
            }
        }
Esempio n. 4
0
        public void TestCtorString()
        {
            const string i = "5";
            var          a = new PyInt(i);

            Assert.AreEqual(5, a.ToInt32());
        }
Esempio n. 5
0
        public void TestCtorPtr()
        {
            var i = new PyInt(5);
            var a = new PyInt(i.Handle);

            Assert.AreEqual(5, a.ToInt32());
        }
Esempio n. 6
0
        public bool TryDecode <T>(PyObject pyObj, out T?value)
        {
            value = default;
            if (!typeof(T).IsEnum)
            {
                return(false);
            }

            Type etype = Enum.GetUnderlyingType(typeof(T));

            if (!PyInt.IsIntType(pyObj))
            {
                return(false);
            }

            object?result;

            try
            {
                result = pyObj.AsManagedObject(etype);
            }
            catch (InvalidCastException)
            {
                return(false);
            }

            if (Enum.IsDefined(typeof(T), result) || typeof(T).IsFlagsEnum())
            {
                value = (T)Enum.ToObject(typeof(T), result);
                return(true);
            }

            return(false);
        }
Esempio n. 7
0
        public void TestCtorUShort()
        {
            const ushort i = 5;
            var          a = new PyInt(i);

            Assert.AreEqual(i, a.ToInt32());
        }
Esempio n. 8
0
        public void PyIntImplicit()
        {
            var i  = new PyInt(1);
            var ni = (PyObject)i.As <object>();

            Assert.AreEqual(i.rawPtr, ni.rawPtr);
        }
Esempio n. 9
0
        public void TestConvertToInt64()
        {
            var a = new PyInt(5);

            Assert.IsInstanceOf(typeof(long), a.ToInt64());
            Assert.AreEqual(5, a.ToInt64());
        }
Esempio n. 10
0
        public void TestCtorULong()
        {
            const ulong i = 5;
            var         a = new PyInt(i);

            Assert.AreEqual(i, a.ToInt32());
        }
Esempio n. 11
0
        public void TestCtorPyObject()
        {
            var i = new PyInt(5);
            var a = new PyInt(i);

            Assert.AreEqual(5, a.ToInt32());
        }
Esempio n. 12
0
        public void TestAsIntGood()
        {
            var i = new PyInt(5);
            var a = PyInt.AsInt(i);

            Assert.AreEqual(5, a.ToInt32());
        }
Esempio n. 13
0
            public static PyObject NewArray(Array content)
            {
                // BlockCopy possibly multidimensional array of arbitrary type to onedimensional byte array
                System.Type ElementType = content.GetType().GetElementType();
                int         nbytes      = content.Length * Marshal.SizeOf(ElementType);

                byte[] data = new byte[nbytes];
                System.Buffer.BlockCopy(content, 0, data, 0, nbytes);

                // Create an python tuple with the dimensions of the input array
                PyObject[] lengths = new PyObject[content.Rank];
                for (int i = 0; i < content.Rank; i++)
                {
                    lengths[i] = new PyInt(content.GetLength(i));
                }
                PyTuple shape = new PyTuple(lengths);

                // Create an empty numpy array in correct shape and datatype
                var dtype = GetNumpyDataType(ElementType);
                var arr   = np.InvokeMethod("empty", shape, dtype);

                var meta    = arr.GetAttr("__array_interface__");
                var address = new System.IntPtr((long)meta["data"][0].As <long>());

                // Copy the data to that array
                Marshal.Copy(data, 0, address, nbytes);
                return(arr);
            }
Esempio n. 14
0
        public void TestVariables()
        {
            (ps.Variables() as dynamic)["ee"] = new PyInt(200);
            var a0 = ps.Get <int>("ee");

            Assert.AreEqual(200, a0);

            ps.Exec("locals()['ee'] = 210");
            var a1 = ps.Get <int>("ee");

            Assert.AreEqual(210, a1);

            ps.Exec("globals()['ee'] = 220");
            var a2 = ps.Get <int>("ee");

            Assert.AreEqual(220, a2);

            using (var item = ps.Variables())
            {
                item["ee"] = new PyInt(230);
            }
            var a3 = ps.Get <int>("ee");

            Assert.AreEqual(230, a3);
        }
Esempio n. 15
0
 public static object ToCLI(this PyObject o)
 {
     if (PySequence.IsSequenceType(o))
     {
         var list = new List <object>();
         foreach (PyObject subo in o)
         {
             list.Add(subo.ToCLI());
         }
         return(list);
     }
     if (PyString.IsStringType(o))
     {
         return(o.As <string>());
     }
     if (PyInt.IsIntType(o))
     {
         return(o.As <long>());
     }
     if (PyLong.IsLongType(o))
     {
         return(o.As <long>());
     }
     if (PyFloat.IsFloatType(o))
     {
         return(o.As <double>());
     }
     return(o);
 }
Esempio n. 16
0
        public void TestConvertToInt64()
        {
            long val = 5 + (long)int.MaxValue;
            var  a   = new PyInt(val);

            Assert.IsInstanceOf(typeof(long), a.ToInt64());
            Assert.AreEqual(val, a.ToInt64());
        }
Esempio n. 17
0
        public void ToNullable()
        {
            const int Const = 42;
            var       i     = new PyInt(Const);
            var       ni    = i.As <int?>();

            Assert.AreEqual(Const, ni);
        }
Esempio n. 18
0
        public void TestCtorPyObject()
        {
            var i = new PyInt(5);

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

            Assert.AreEqual(5, a.ToInt32());
        }
Esempio n. 19
0
        public void ToBigIntegerLarge()
        {
            BigInteger val   = BigInteger.Pow(2, 1024) + 3;
            var        pyInt = new PyInt(val);

            Assert.AreEqual(val, pyInt.ToBigInteger());
            val   = -val;
            pyInt = new PyInt(val);
            Assert.AreEqual(val, pyInt.ToBigInteger());
        }
Esempio n. 20
0
        public void TestBadPyObjectCtor()
        {
            var      t      = new PyInt(5);
            PyString actual = null;

            var ex = Assert.Throws <ArgumentException>(() => actual = new PyString(t));

            StringAssert.StartsWith("object is not a string", ex.Message);
            Assert.IsNull(actual);
        }
Esempio n. 21
0
        public void TestAsIntBad()
        {
            var   s = new PyString("Foo");
            PyInt a = null;

            var ex = Assert.Throws <PythonException>(() => a = PyInt.AsInt(s));

            StringAssert.StartsWith("invalid literal for int", ex.Message);
            Assert.IsNull(a);
        }
Esempio n. 22
0
        public void TestCtorBadPyObject()
        {
            var   i = new PyString("Foo");
            PyInt a = null;

            var ex = Assert.Throws <ArgumentException>(() => a = new PyInt(i));

            StringAssert.StartsWith("object is not an int", ex.Message);
            Assert.IsNull(a);
        }
Esempio n. 23
0
        public void TestCtorBadString()
        {
            const string i = "Foo";
            PyInt        a = null;

            var ex = Assert.Throws <PythonException>(() => a = new PyInt(i));

            StringAssert.StartsWith("invalid literal for int", ex.Message);
            Assert.IsNull(a);
        }
Esempio n. 24
0
        public void TestPyTupleBadCtor()
        {
            var     i = new PyInt(5);
            PyTuple t = null;

            var ex = Assert.Throws <ArgumentException>(() => t = new PyTuple(i));

            Assert.AreEqual("object is not a tuple", ex.Message);
            Assert.IsNull(t);
        }
Esempio n. 25
0
        public void TestInvalidAsTuple()
        {
            var     i = new PyInt(5);
            PyTuple t = null;

            var ex = Assert.Throws <PythonException>(() => t = PyTuple.AsTuple(i));

            Assert.AreEqual("'int' object is not iterable", ex.Message);
            Assert.IsNull(t);
        }
Esempio n. 26
0
        public void TestBadPyObjectCtor()
        {
            var    i = new PyInt(5);
            PyList t = null;

            var ex = Assert.Throws <ArgumentException>(() => t = new PyList(i));

            Assert.AreEqual("object is not a list", ex.Message);
            Assert.IsNull(t);
        }
Esempio n. 27
0
        public void TestStringAsListType()
        {
            var    i = new PyInt(5);
            PyList t = null;

            var ex = Assert.Throws <PythonException>(() => t = PyList.AsList(i));

            Assert.AreEqual("'int' object is not iterable", ex.Message);
            Assert.IsNull(t);
        }
Esempio n. 28
0
        public void BigIntExplicit()
        {
            BigInteger val = 42;
            var        i   = new PyInt(val);
            var        ni  = i.As <BigInteger>();

            Assert.AreEqual(val, ni);
            var nullable = i.As <BigInteger?>();

            Assert.AreEqual(val, nullable);
        }
Esempio n. 29
0
        public static PyObject CreateNdArray(A content)
        {
            // Create an python tuple with the dimensions of the input array
            PyObject[] lengths = new PyObject[content.Rank];
            for (int i = 0; i < content.Rank; i++)
            {
                lengths[i] = new PyInt(content.Dimension[i]);
            }
            PyTuple shape = new PyTuple(lengths);

            // Create an empty numpy array in correct shape and datatype
            var dtype = GetNumpyDataType(content.ElementType);
            var arr   = np.InvokeMethod("empty", shape, dtype);

            var meta    = arr.GetAttr("__array_interface__");
            var address = new IntPtr((long)meta["data"][0].As <long>());

            if (content is A <byte> byteArray)
            {
                Marshal.Copy(byteArray.Buffer, 0, address, content.Count);
            }
            else if (content is A <short> shortArray)
            {
                Marshal.Copy(shortArray.Buffer, 0, address, content.Count);
            }
            else if (content is A <int> intArray)
            {
                Marshal.Copy(intArray.Buffer, 0, address, content.Count);
            }
            else if (content is A <long> longArray)
            {
                Marshal.Copy(longArray.Buffer, 0, address, content.Count);
            }
            else if (content is A <double> doubleArray)
            {
                Marshal.Copy(doubleArray.Buffer, 0, address, content.Count);
            }
            else if (content is A <double> floatArray)
            {
                Marshal.Copy(floatArray.Buffer, 0, address, content.Count);
            }
            else
            {
                int    nbytes = content.SizeInBytes;
                byte[] data   = new byte[nbytes];
                Buffer.BlockCopy(content.Buffer, 0, data, 0, nbytes);
                Marshal.Copy(data, 0, address, nbytes);
            }

            return(arr);
        }
Esempio n. 30
0
 public CrossValidationSet(string estimator, string estimatorname, string datasetname, PyObject dataset, int rowstart, int rowfinish, PyObject attributes, CVObject cvObject, PyObject PyModule)
 {
     Estimator     = new PyString(estimator);
     EstimatorName = estimatorname;
     DatasetName   = datasetname;
     Dataset       = dataset;
     Rowstart      = new PyInt(rowstart);
     Rowfinish     = new PyInt(rowfinish);
     Attributes    = attributes;
     CV            = new PyString(cvObject.Type);
     CVparams      = new PyDict();
     if (cvObject.N_groups != 0)
     {
         CVparams["n_groups"] = new PyInt(cvObject.N_groups);
     }
     if (cvObject.N_splits != 0)
     {
         CVparams["n_splits"] = new PyInt(cvObject.N_splits);
     }
     if (cvObject.P != 0)
     {
         CVparams["p"] = new PyInt(cvObject.P);
     }
     if (cvObject.Shuffle)
     {
         CVparams["shuffle"] = true.ToPython();
     }
     if (cvObject.Train_size != null)
     {
         if (cvObject.Train_size.GetType().Name == "Int32")
         {
             CVparams["train_size"] = new PyInt((int)cvObject.Train_size);
         }
         if (cvObject.Train_size.GetType().Name == "Double")
         {
             CVparams["train_size"] = new PyFloat((double)cvObject.Train_size);
         }
     }
     if (cvObject.Test_size != null)
     {
         if (cvObject.Test_size.GetType().Name == "Int32")
         {
             CVparams["test_size"] = new PyInt((int)cvObject.Test_size);
         }
         if (cvObject.Test_size.GetType().Name == "Double")
         {
             CVparams["test_size"] = new PyFloat((double)cvObject.Test_size);
         }
     }
     _pymodule = PyModule;
 }