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); }
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); }
public void TestIsIntTypeFalse() { var s = new PyString("Foo"); Assert.False(PyInt.IsIntType(s)); }
public void TestIsIntTypeTrue() { var i = new PyInt(5); Assert.True(PyInt.IsIntType(i)); }
public static bool IsInt(this PyObject obj) { return(PyInt.IsIntType(obj)); }