public IEnumerable <PyType> GetBaseTypes(Type type, IList <PyType> existingBases) { if (type == typeof(InheritanceTestBaseClassWrapper)) { return(new[] { PyType.Get(type.BaseType), ExtraBase }); } return(existingBases); }
private static bool IsList(PyType objectType) { //TODO accept any python object that implements the sequence and list protocols //must implement sequence protocol to fully implement list protocol //if (!SequenceDecoder.IsSequence(objectType)) return false; //returns wheter the type is a list. return(PythonReferenceComparer.Instance.Equals(objectType, Runtime.PyListType)); }
public static IntPtr DoThrowSimple() { using (Py.GIL()) { dynamic builtins = Py.Import("builtins"); var typeErrorType = new PyType(builtins.TypeError); var pyerr = new PythonException(typeErrorType, value: null, traceback: null, "Type error, the first", innerException: null); throw new ArgumentException("Bogus bad parameter", pyerr); } }
public PyDynamic(PyType type, Scene.Entity3D ent) { Sent = ent; PhysicsManager.AddObj(this); Mat = PhysicsManager.py.CreateMaterial(0.7f, 0.7f, 0.1f); switch (type) { case PyType.Box: CreateBox(ent); break; break; } }
internal static bool IsSequence(PyType objectType) { //must implement iterable protocol to fully implement sequence protocol if (!IterableDecoder.IsIterable(objectType)) { return(false); } //returns wheter it implements the sequence protocol //according to python doc this needs to exclude dict subclasses //but I don't know how to look for that given the objectType //rather than the instance. return(objectType.HasAttr("__getitem__")); }
/// <summary> /// Returns the size of the given type (tuple, otherwise list) /// </summary> /// <param name="type"></param> /// <returns></returns> public int Size(PyType type) { if (!IsValid) { return(-1); } try { var getSize = (type == PyType.TupleType || type == PyType.DerivedTupleType) ? (Func <IntPtr, int>)Py.PyTuple_Size : Py.PyList_Size; return(getSize(this)); } finally { HandlePythonError(); } }
/// <summary> /// Returns a list item from the current Python object /// </summary> /// <param name="index"></param> /// <param name="type">Force the PyType to List or Tuple</param> /// <returns></returns> public PyObject Item(int index, PyType type) { if (!IsValid) { return(PySharp.PyZero); } var getItem = (type == PyType.TupleType || type == PyType.DerivedTupleType) ? (Func <IntPtr, int, IntPtr>)Py.PyTuple_GetItem : Py.PyList_GetItem; PyObject result; if (!_itemCache.TryGetValue(index, out result)) { result = new PyObject(_pySharp, getItem(this, index), false); _itemCache[index] = result; } return(result); }
/// <summary> /// Gets a concrete instance of <see cref="IPyObjectDecoder"/> /// (potentially selecting one from a collection), /// that can decode from <paramref name="objectType"/> to <paramref name="targetType"/>, /// or <c>null</c> if a matching decoder can not be found. /// </summary> public static IPyObjectDecoder GetDecoder( this IPyObjectDecoder decoder, PyType objectType, Type targetType) { if (decoder is null) { throw new ArgumentNullException(nameof(decoder)); } if (decoder is IEnumerable <IPyObjectDecoder> composite) { return(composite .Select(nestedDecoder => nestedDecoder.GetDecoder(objectType, targetType)) .FirstOrDefault(d => d != null)); } return(decoder.CanDecode(objectType, targetType) ? decoder : null); }
/// <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(); } }
public bool CanDecode(PyType objectType, Type targetType) { return(targetType.IsEnum && objectType.IsSubclass(Runtime.PyLongType)); }
public bool CanDecode(PyType objectType, Type targetType) { return(targetType == typeof(DateTime)); }
public bool CanDecode(PyType objectType, Type targetType) => PythonReferenceComparer.Instance.Equals(objectType, TheOnlySupportedSourceType) && targetType == typeof(TTarget);
public bool CanDecode(PyType objectType, Type targetType) => PythonReferenceComparer.Instance.Equals(PyErr, objectType);
public bool CanDecode(PyType objectType, Type targetType) => this.CanEncode(targetType) && PythonReferenceComparer.Instance.Equals(objectType, PythonEngine.Eval("ValueError"));
public bool CanDecode(PyType objectType, Type targetType) { return(targetType.IsAssignableFrom(typeof(EverythingElseToSelfDecoder))); }
public bool CanDecode(PyType objectType, Type targetType) => objectType.Handle == Runtime.PyTupleType && this.CanEncode(targetType);
public bool CanDecode(PyType objectType, Type targetType) { return(IsIterable(objectType) && IsIterable(targetType)); }
internal static bool IsIterable(PyType objectType) { return(objectType.HasAttr("__iter__")); }
public bool CanDecode(PyType objectType, Type targetType) => objectType.Handle == TheOnlySupportedSourceType.Handle && targetType == typeof(TTarget);
public bool CanDecode(PyType objectType, Type targetType) { return(IsSequence(objectType) && IsSequence(targetType)); }
/// <inheritdoc /> public bool CanDecode(PyType objectType, Type targetType) => this.decoders.Any(decoder => decoder.CanDecode(objectType, targetType));
public bool CanDecode(PyType objectType, Type targetType) { var type = objectType.ToString(); return _decoders.ContainsKey(type); }
public bool CanDecode(PyType objectType, Type targetType) { return(IsList(objectType) && IsList(targetType)); }
public bool CanDecode(PyType objectType, Type targetType) { return(targetType.IsEnum && objectType.IsSubclass(new BorrowedReference(Runtime.PyLongType))); }
/// <summary> /// Create an instance of <see cref="ColumnInfo"/> /// </summary> /// <param name="utils">Python utilities</param> /// <param name="name">Column name</param> /// <param name="pyType">Column type</param> public ColumnInfo(PythonUtils utils, string name, PyType pyType) { _utils = utils ?? throw new ArgumentNullException(nameof(utils)); if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException("Name can't be null or empty", nameof(name)); } Name = name; PythonType = pyType; switch (PythonType) { case PyType.Undefined: ManagedType = typeof(string); break; case PyType.BoolType: ManagedType = typeof(bool); break; case PyType.Int8Type: ManagedType = typeof(sbyte); break; case PyType.Uint8Type: ManagedType = typeof(byte); break; case PyType.Int16Type: ManagedType = typeof(short); break; case PyType.Uint16Type: ManagedType = typeof(ushort); break; case PyType.Uint32Type: ManagedType = typeof(uint); break; case PyType.Int64Type: ManagedType = typeof(long); break; case PyType.Uint64Type: ManagedType = typeof(ulong); break; case PyType.Int32Type: ManagedType = typeof(int); break; case PyType.Float16Type: ManagedType = typeof(float); break; case PyType.Float32Type: ManagedType = typeof(float); break; case PyType.Float64Type: ManagedType = typeof(double); break; case PyType.Complex128Type: ManagedType = typeof(string); break; case PyType.Complex64Type: ManagedType = typeof(string); break; case PyType.ObjectType: ManagedType = typeof(string); break; case PyType.Datetime64Type: ManagedType = typeof(DateTime); break; case PyType.Timedelta64Type: ManagedType = typeof(TimeSpan); break; default: throw new ArgumentOutOfRangeException(); } }
public bool CanDecode(PyType objectType, Type targetType) => PythonReferenceComparer.Instance.Equals(objectType, Runtime.PyTupleType) && this.CanEncode(targetType);
/// <summary> /// Create slots holder for holding the delegate of slots and be able to reset them. /// </summary> /// <param name="type">Steals a reference to target type</param> public SlotsHolder(PyType type) { this.Type = type; }