Exemplo n.º 1
0
 public void SimpleFloatMath()
 {
     runBasicTest("a = 10.0 * (2.0 + 4.0) / 3.0\n", new VariableMultimap(new TupleList <string, object>
     {
         { "a", PyFloat.Create(20.0) }
     }), 1);
 }
Exemplo n.º 2
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);
 }
Exemplo n.º 3
0
 public void ComprehensiveArithmeticOperators()
 {
     runBasicTest(
         "x = 10\n" +
         "a = x + 2\n" +
         "b = x - 2\n" +
         "c = x * 2\n" +
         "d = x / 2\n" +
         "e = x % 9\n" +
         "f = x // 3\n" +
         "g = x ** 2\n" +
         "h = x & 2\n" +
         "i = x | 14\n" +
         "j = x ^ 2\n" +
         "k = x >> 2\n" +
         "l = x << 2\n"
         , new VariableMultimap(new TupleList <string, object>
     {
         { "a", PyInteger.Create(12) },
         { "b", PyInteger.Create(8) },
         { "c", PyInteger.Create(20) },
         { "d", PyFloat.Create(5.0) },
         { "e", PyInteger.Create(1) },
         { "f", PyInteger.Create(3) },
         { "g", PyInteger.Create(100) },
         { "h", PyInteger.Create(2) },
         { "i", PyInteger.Create(14) },
         { "j", PyInteger.Create(8) },
         { "k", PyInteger.Create(2) },
         { "l", PyInteger.Create(40) }
     }), 1);
 }
Exemplo n.º 4
0
 public void RepeatedArithmeticOperators()
 {
     // Making sure that we're properly parsing and generating all of these when there's multiples of the operator.
     runBasicTest(
         "x = 100\n" +
         "a = x + 2 + 3\n" +
         "b = x - 2 - 3\n" +
         "c = x * 2 * 3\n" +
         "d = x / 4 / 2\n" +
         "e = x % 9 % 3\n" +
         "f = x // 2 // 3\n" +
         "g = x ** 2 ** 3\n" +
         "h = x & 3 & 2\n" +
         "i = x | 13 | 1 \n" +
         "j = x ^ 2 ^ 1\n" +
         "k = x >> 2 >> 3\n" +
         "l = x << 2 << 3\n"
         , new VariableMultimap(new TupleList <string, object>
     {
         { "a", PyInteger.Create(100 + 2 + 3) },
         { "b", PyInteger.Create(100 - 2 - 3) },
         { "c", PyInteger.Create(100 * 2 * 3) },
         { "d", PyFloat.Create(100.0 / 4.0 / 2.0) },
         { "e", PyInteger.Create(100 % 9 % 3) },
         { "f", PyInteger.Create(100 / 2 / 3) },
         { "g", PyInteger.Create((BigInteger)Math.Pow(100.0, 8.0)) },           // 2 ** 3 gets evaluated first and becomes 8. This is what CPython does too!
         { "h", PyInteger.Create(100 & 3 & 2) },
         { "i", PyInteger.Create(100 | 13 | 1) },
         { "j", PyInteger.Create(100 ^ 2 ^ 1) },
         { "k", PyInteger.Create(100 >> 2 >> 3) },
         { "l", PyInteger.Create(100 << 2 << 3) }
     }), 1);
 }
Exemplo n.º 5
0
        public void IntPtrCtor()
        {
            var i  = new PyFloat(1);
            var ii = new PyFloat(i.Handle);

            Assert.AreEqual(i.Handle, ii.Handle);
        }
Exemplo n.º 6
0
 public async Task SimpleIntMath()
 {
     await runBasicTest("a = 10 * (2 + 4) / 3\n", new VariableMultimap(new TupleList <string, object>
     {
         { "a", PyFloat.Create(20.0) }
     }), 1);
 }
Exemplo n.º 7
0
        public void IsFloatTrue()
        {
            const double a = 4.5;
            var          i = new PyFloat(a);

            Assert.True(PyFloat.IsFloatType(i));
        }
Exemplo n.º 8
0
        public void StringDoubleCtor()
        {
            const string a = "4.5";
            var          i = new PyFloat(a);

            Assert.True(PyFloat.IsFloatType(i));
            // Assert.Assert.AreEqual(i, a.ToInt32());
        }
Exemplo n.º 9
0
        public void FloatCtor()
        {
            const float a = 4.5F;
            var         i = new PyFloat(a);

            Assert.True(PyFloat.IsFloatType(i));
            // Assert.Assert.AreEqual(i, a.ToInt32());
        }
Exemplo n.º 10
0
        public void DoubleCtor()
        {
            const double a = 4.5;
            var          i = new PyFloat(a);

            Assert.True(PyFloat.IsFloatType(i));
            // Assert.Assert.AreEqual(i, a.ToInt32());
        }
Exemplo n.º 11
0
        public void PyObjectCtorGood()
        {
            var i = new PyFloat(5);
            var a = new PyFloat(i);

            Assert.True(PyFloat.IsFloatType(a));
            // Assert.Assert.AreEqual(i, a.ToInt32());
        }
Exemplo n.º 12
0
        public void AsFloatGood()
        {
            const double a = 4.5;
            var          i = new PyFloat(a);
            PyFloat      s = PyFloat.AsFloat(i);

            Assert.True(PyFloat.IsFloatType(s));
            // Assert.Assert.AreEqual(i, a.ToInt32());
        }
Exemplo n.º 13
0
 public void AssignmentOperators()
 {
     // https://www.w3schools.com/python/python_operators.asp
     // +=	x += 3	x = x + 3
     // -=	x -= 3	x = x - 3
     // *=	x *= 3	x = x * 3
     // /=	x /= 3	x = x / 3
     // %=	x %= 3	x = x % 3
     // //=	x //= 3	x = x // 3
     // **=	x **= 3	x = x ** 3
     // &=	x &= 3	x = x & 3
     // |=	x |= 3	x = x | 3
     // ^=	x ^= 3	x = x ^ 3
     // >>=	x >>= 3	x = x >> 3
     // <<=	x <<= 3	x = x << 3
     runBasicTest(
         "a = 10\n" +
         "b = 10\n" +
         "c = 10\n" +
         "d = 10\n" +
         "e = 10\n" +
         "f = 10\n" +
         "g = 10\n" +
         "h = 10\n" +
         "i = 10\n" +
         "j = 10\n" +
         "k = 10\n" +
         "l = 10\n" +
         "a += 2\n" +
         "b -= 2\n" +
         "c *= 2\n" +
         "d /= 2\n" +
         "e %= 9\n" +
         "f //= 3\n" +
         "g **= 2\n" +
         "h &= 2\n" +
         "i |= 14\n" +
         "j ^= 2\n" +
         "k >>= 2\n" +
         "l <<= 2\n"
         , new VariableMultimap(new TupleList <string, object>
     {
         { "a", PyInteger.Create(12) },
         { "b", PyInteger.Create(8) },
         { "c", PyInteger.Create(20) },
         { "d", PyFloat.Create(5.0) },
         { "e", PyInteger.Create(1) },
         { "f", PyInteger.Create(3) },
         { "g", PyInteger.Create(100) },
         { "h", PyInteger.Create(2) },
         { "i", PyInteger.Create(14) },
         { "j", PyInteger.Create(8) },
         { "k", PyInteger.Create(2) },
         { "l", PyInteger.Create(40) }
     }), 1);
 }
Exemplo n.º 14
0
        public void PyObjectCtorBad()
        {
            var     i = new PyString("Foo");
            PyFloat a = null;

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

            StringAssert.StartsWith("object is not a float", ex.Message);
            Assert.IsNull(a);
        }
Exemplo n.º 15
0
        public void AsFloatBad()
        {
            var     s = new PyString("Foo");
            PyFloat a = null;

            var ex = Assert.Throws <PythonException>(() => a = PyFloat.AsFloat(s));

            StringAssert.StartsWith("could not convert string to float", ex.Message);
            Assert.IsNull(a);
        }
Exemplo n.º 16
0
        public void StringBadCtor()
        {
            const string i = "Foo";
            PyFloat      a = null;

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

            StringAssert.StartsWith("could not convert string to float", ex.Message);
            Assert.IsNull(a);
        }
Exemplo n.º 17
0
        public static object CreateNumber(IParseTree context)
        {
            string rawText = context.GetText();

            if (DecimalPointNumberRegex.Match(rawText).Success)
            {
                return(PyFloat.Create(Decimal.Parse(rawText)));
            }
            return(PyInteger.Create(BigInteger.Parse(context.GetText())));
        }
Exemplo n.º 18
0
 public void BasicWait()
 {
     runBasicTest(
         "a = 10 * (2 + 4) / 3\n" +
         "wait\n" +
         "b = a + 3\n", new VariableMultimap(new TupleList <string, object>
     {
         { "a", PyFloat.Create(20.0) },
         { "b", PyFloat.Create(23.0) }
     }), 2);
 }
Exemplo n.º 19
0
        public void TestConvertSingleToManaged(
            [Values(float.PositiveInfinity, float.NegativeInfinity, float.MinValue, float.MaxValue, float.NaN,
                    float.Epsilon)] float testValue)
        {
            var pyFloat = new PyFloat(testValue);

            object convertedValue;
            var    converted = Converter.ToManaged(pyFloat.Handle, typeof(float), out convertedValue, false);

            Assert.IsTrue(converted);
            Assert.IsTrue(((float)convertedValue).Equals(testValue));
        }
Exemplo n.º 20
0
        public void TestConvertDoubleToManaged(
            [Values(double.PositiveInfinity, double.NegativeInfinity, double.MinValue, double.MaxValue, double.NaN,
                    double.Epsilon)] double testValue)
        {
            var pyFloat = new PyFloat(testValue);

            object convertedValue;
            var    converted = Converter.ToManaged(pyFloat.Handle, typeof(double), out convertedValue, false);

            Assert.IsTrue(converted);
            Assert.IsTrue(((double)convertedValue).Equals(testValue));
        }
Exemplo n.º 21
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;
 }
Exemplo n.º 22
0
        public static double GetDouble(dynamic dynDoublePyObj, ref double?doubleMember)
        {
            if (doubleMember != null)
            {
                return((double)doubleMember);
            }

            using (Py.GIL())
            {
                var dynDoublePyFloat = PyFloat.AsFloat(dynDoublePyObj);
                doubleMember = dynDoublePyFloat.As <double>();
                return((double)doubleMember);
            }
        }
Exemplo n.º 23
0
        public void CanDecode()
        {
            var pyint    = new PyInt(10).GetPythonType();
            var pyfloat  = new PyFloat(10).GetPythonType();
            var pystr    = new PyString("world").GetPythonType();
            var decoder1 = new DecoderReturningPredefinedValue <long>(pyint, decodeResult: 42);
            var decoder2 = new DecoderReturningPredefinedValue <string>(pyfloat, decodeResult: "atad:");
            var group    = new DecoderGroup {
                decoder1,
                decoder2,
            };

            Assert.IsTrue(group.CanDecode(pyint, typeof(long)));
            Assert.IsFalse(group.CanDecode(pyint, typeof(int)));
            Assert.IsTrue(group.CanDecode(pyfloat, typeof(string)));
            Assert.IsFalse(group.CanDecode(pystr, typeof(string)));
        }
Exemplo n.º 24
0
        public void Decodes()
        {
            var pyint    = new PyInt(10).GetPythonType();
            var pyfloat  = new PyFloat(10).GetPythonType();
            var decoder1 = new DecoderReturningPredefinedValue <long>(pyint, decodeResult: 42);
            var decoder2 = new DecoderReturningPredefinedValue <string>(pyfloat, decodeResult: "atad:");
            var group    = new DecoderGroup {
                decoder1,
                decoder2,
            };

            Assert.IsTrue(group.TryDecode(new PyInt(10), out long longResult));
            Assert.AreEqual(42, longResult);
            Assert.IsTrue(group.TryDecode(new PyFloat(10), out string strResult));
            Assert.AreSame("atad:", strResult);

            Assert.IsFalse(group.TryDecode(new PyInt(10), out int _));
        }
Exemplo n.º 25
0
        // TODO: Make every part of this much easier. Creating the objects properly will need to be simplified. One-step
        // construction with passed-in values will greatly help. This could be done with a factory, but we should investigate
        // if we can just use the basic type constructors in some way first.
        public async Task <PyTuple> get_player_pos_wrapper(IInterpreter interpreter, FrameContext context)
        {
            PyFloat f1 = (PyFloat)await PyFloatClass.Instance.Call(interpreter, context, new object[0]);

            f1.number = Decimal.Parse(playerXLabel.Text);
            PyFloat f2 = (PyFloat)await PyFloatClass.Instance.Call(interpreter, context, new object[0]);

            f2.number = Decimal.Parse(playerYLabel.Text);

            var tuples = new PyObject[2]
            {
                f1,
                f2
            };
            var new_pytup = (PyTuple)await PyTupleClass.Instance.Call(interpreter, context, new object[0]);

            new_pytup.Values = tuples;
            return(new_pytup);
        }
Exemplo n.º 26
0
        public object ReadValue(uint?me_value)
        {
            PyTypeObject typeObject = new PyObject(me_value.Value, MemoryReader).LoadType(PyMemoryReader);
            object       value      = null;

            switch (typeObject.tp_name_Val)
            {
            case "str":
                value = new PyStr(me_value.Value, MemoryReader);
                break;

            case "float":
                value = new PyFloat(me_value.Value, MemoryReader);
                break;

            case "int":
                value = new PyInt(me_value.Value, MemoryReader);
                break;

            case "bool":
                value = new PyBool(me_value.Value, MemoryReader);
                break;

            case "unicode":
                value = new PyUnicode(me_value.Value, MemoryReader);
                break;

            case "list":
                value = new PyList(me_value.Value, MemoryReader);
                break;

            default:
                break;
            }

            if (value != null)
            {
                return(value);
            }

            return(me_value?.ToString("x"));
        }
Exemplo n.º 27
0
        public object ReadValue(PyObject obj)
        {
            PyTypeObject typeObject = obj.LoadType(PyMemoryReader);
            object       value      = null;

            switch (typeObject.tp_name_Val)
            {
            case "str":
                value = new PyStr(obj.BaseAddress, MemoryReader);
                break;

            case "float":
                value = new PyFloat(obj.BaseAddress, MemoryReader);
                break;

            case "int":
                value = new PyInt(obj.BaseAddress, MemoryReader);
                break;

            case "bool":
                value = new PyBool(obj.BaseAddress, MemoryReader);
                break;

            case "unicode":
                value = new PyUnicode(obj.BaseAddress, MemoryReader);
                break;

            case "list":
                value = new PyList(obj.BaseAddress, MemoryReader);
                break;

            default:
                break;
            }

            if (value != null)
            {
                return(value);
            }

            return(obj.BaseAddress.ToString("x"));
        }
Exemplo n.º 28
0
 public async Task NumericStringConversions()
 {
     await runBasicTest(
         "a_string = '1'\n" +
         "as_int = int(a_string)\n" +
         "as_float = float(a_string)\n" +
         "as_bool = bool(a_string)\n" +
         "int_str = str(1)\n" +
         "float_str = str(1.0)\n" +
         "bool_str = str(True)\n",
         new VariableMultimap(new TupleList <string, object>
     {
         { "as_int", PyInteger.Create(1) },
         { "as_float", PyFloat.Create(1.0) },
         { "as_bool", PyBool.True },
         { "int_str", PyString.Create("1") },
         { "float_str", PyString.Create("1.0") },
         { "bool_str", PyString.Create("True") },
     }), 1);
 }
Exemplo n.º 29
0
        public void GetDecodersByTypes()
        {
            var pyint    = new PyInt(10).GetPythonType();
            var pyfloat  = new PyFloat(10).GetPythonType();
            var pystr    = new PyString("world").GetPythonType();
            var decoder1 = new DecoderReturningPredefinedValue <long>(pyint, decodeResult: 42);
            var decoder2 = new DecoderReturningPredefinedValue <string>(pyfloat, decodeResult: "atad:");
            var group    = new DecoderGroup {
                decoder1,
                decoder2,
            };

            var decoder = group.GetDecoder(pyfloat, typeof(string));

            Assert.AreSame(decoder2, decoder);
            decoder = group.GetDecoder(pystr, typeof(string));
            Assert.IsNull(decoder);
            decoder = group.GetDecoder(pyint, typeof(long));
            Assert.AreSame(decoder1, decoder);
        }
Exemplo n.º 30
0
        public void IsFloatFalse()
        {
            var i = new PyString("Foo");

            Assert.False(PyFloat.IsFloatType(i));
        }