pyd.pydobject
Contains utilities for operating on generic python objects.
- class
PydObject
;
- Wrapper class for a Python/C API PyObject.
Nearly all of these member functions may throw a PythonException if the
underlying Python API raises a Python exception.
Authors:
Kirk McDonald
Date:
June 18, 2006
See Also:
The Python/C API
- this(PyObject* o);
- Wrap an owned PyObject*.
- this(Borrowed!(PyObject)* o);
- Own a borrowed PyObject* and wrap it.
- this();
- Constructs an instance of the Py_None PydObject.
- class
BufferView
;
- exposes a lowish-level wrapper of the new-style buffer interface
See Also:
Buffers and MemoryView Objects
- bool
has_simple
;
- supports PyBUF_SIMPLE.
should always be true.
- bool
has_nd
;
- supports PyBUF_ND.
i.e. buffer supplies ndim, shape.
- bool
has_strides
;
- supports PyBUF_STRIDES.
i.e. buffer supplies strides.
- bool
has_indirect
;
- supports PyBUF_INDIRECT.
i.e. buffer supplies suboffsets.
- bool
c_contiguous
;
- supports PyBUF_C_CONTIGUOUS.
buffer is row-major.
- bool
fortran_contiguous
;
- supports PyBUF_F_CONTIGUOUS.
buffer is column-major
- this();
- construct buffer view. Probe for capabilities this object supports.
- this(int flags);
- Construct buffer view. Don't probe for capabilities; assume
object supports capabilities implied by flags.
- @property ubyte[]
buf
();
- Get the raw bytes of this buffer
- @property bool
readonly
();
- @property string
format
();
- Get the struct-style format of the element type of this buffer.
See Also:
Struct Format Strings
- @property int
ndim
();
- Get number of dimensions of this buffer.
- @property long[]
shape
();
- @property long[]
strides
();
- @property long[]
suboffsets
();
- @property auto
itemsize
();
- T
item
(T)(Py_ssize_t[] indeces...);
- void
set_item
(T)(T value, Py_ssize_t[] indeces...);
- BufferView
buffer_view
();
- Get a BufferView of this object.
Will fail if this does not support the new buffer interface.
- BufferView
buffer_view
(int flags);
- Get a BufferView of this object without probing for capabilities.
Will fail if this does not support the new buffer interface.
- @property Borrowed!(PyObject)*
ptr
();
- Returns a borrowed reference to the PyObject.
- bool
hasattr
(string attr_name);
- Equivalent to hasattr(this, attr_name) in Python.
- bool
hasattr
(PydObject attr_name);
- Equivalent to hasattr(this, attr_name) in Python.
- PydObject
getattr
(string attr_name);
- Equivalent to getattr(this, attr_name) in Python.
- PydObject
getattr
(PydObject attr_name);
- Equivalent to getattr(this, attr_name) in Python.
- void
setattr
(string attr_name, PydObject v);
- Equivalent to setattr(this, attr_name, v) in Python.
- void
setattr
(PydObject attr_name, PydObject v);
- Equivalent to setattr(this, attr_name, v) in Python.
- void
delattr
(string attr_name);
- Equivalent to del this.attr_name in Python.
- void
delattr
(PydObject attr_name);
- Equivalent to del this.attr_name in Python.
- int
opCmp
(Object o);
- Exposes Python object comparison to D. Equivalent to cmp(this, rhs) in Python.
- bool
opEquals
(Object o);
- Exposes Python object equality check to D.
- PydObject
repr
();
- Equivalent to repr(this) in Python.
- PydObject
str
();
- Equivalent to str(this) in Python.
- string
toString
();
- Allows use of PydObject in writeln via %s
- PydObject
unicode
();
- Equivalent to unicode(this) in Python.
- PydObject
bytes
();
- Equivalent to bytes(this) in Python.
- bool
isinstance
(PydObject cls);
- Equivalent to
isinstance
(this, cls) in Python.
- bool
issubclass
(PydObject cls);
- Equivalent to
issubclass
(this, cls) in Python. Only works if this is a class.
- bool
callable
();
- Equivalent to callable(this) in Python.
- PydObject
unpack_call
(PydObject args = null);
- Calls the PydObject with args.
Params:
| PydObject args |
Should be a tuple of the arguments to pass. Omit to
call with no arguments. |
Returns:
Whatever this function object returns.
- PydObject
unpack_call
(PydObject args, PydObject kw);
- Calls the PydObject with positional and keyword arguments.
Params:
| PydObject args |
Positional arguments. Should be a tuple. Pass an empty
tuple for no positional arguments. |
| PydObject kw |
Keyword arguments. Should be a dict. |
Returns:
Whatever this function object returns.
- PydObject
opCall
(T...)(T t);
- Calls the PydObject with any convertible D items.
- PydObject
method_unpack
(string name, PydObject args = null);
- Calls the PydObject method with args.
Params:
| string name |
name of method to call |
| PydObject args |
Should be a tuple of the arguments to pass. Omit to
call with no arguments. |
Returns:
Whatever this object's method returns.
- PydObject
method_unpack
(string name, PydObject args, PydObject kw);
- Calls the PydObject method with positional and keyword arguments.
Params:
| string name |
name of method to call. |
| PydObject args |
Positional arguments. Should be a tuple. Pass an empty
tuple for no positional arguments. |
| PydObject kw |
Keyword arguments. Should be a dict. |
Returns:
Whatever this object's method returns.
- PydObject
method
(T...)(string name, T t);
- Calls a
method
of the object with any convertible D items.
- hash_t
hash
();
- Equivalent to hash(this) in Python.
- T
to_d
(T)();
- Convert this object to instance of T.
- bool
not
();
- Equivalent to "not this" in Python.
- PydObject
type
();
- Gets the type of this PydObject. Equivalent to type(this) in Python.
Returns:
The type PydObject of this PydObject.
- Py_ssize_t
length
();
- The length of this PydObject. Equivalent to len(this) in Python.
- Py_ssize_t
size
();
- Equivalent to length()
- PydObject
dir
();
- Equivalent to dir(this) in Python.
- PydObject
opIndex
(PydObject key);
- Equivalent to o[key] in Python.
- PydObject
opIndex
(string key);
- Equivalent to o['key'] in Python; usually only makes sense for
mappings.
- PydObject
opIndex
(int i);
- Equivalent to o[i] in Python; usually only makes sense for sequences.
- void
opIndexAssign
(T, S)(T value, S key);
- Equivalent to o[key] = value in Python.
- void
del_item
(PydObject key);
- Equivalent to del o[key] in Python.
- void
del_item
(string key);
- Equivalent to del o['key'] in Python. Usually only makes sense for
mappings.
- void
del_item
(int i);
- Equivalent to del o[i] in Python. Usually only makes sense for
sequences.
- PydObject
opSlice
(Py_ssize_t i1, Py_ssize_t i2);
- Equivalent to o[i1:i2] in Python.
- PydObject
opSlice
();
- Equivalent to o[:] in Python.
- void
opSliceAssign
(PydObject v, Py_ssize_t i1, Py_ssize_t i2);
- Equivalent to o[i1:i2] = v in Python.
- void
opSliceAssign
(PydObject v);
- Equivalent to o[:] = v in Python.
- void
del_slice
(Py_ssize_t i1, Py_ssize_t i2);
- Equivalent to del o[i1:i2] in Python.
- void
del_slice
();
- Equivalent to del o[:] in Python.
- int
opApply
(int delegate(ref PydObject) dg);
- Iterates over the items in a collection, be they the items in a
sequence, keys in a dictionary, or some other iteration defined for the
PydObject's type.
- int
opApply
(int delegate(ref PydObject, ref PydObject) dg);
- Iterate over (key, value) pairs in a dictionary. If the PydObject is not
a dict, this simply does nothing. (It iterates over no items.) You
should not attempt to modify the dictionary while iterating through it,
with the exception of modifying values. Adding or removing items while
iterating through it is an especially bad idea.
- PydObject
opBinary
(string op, T)(T o);
- Forwards to appropriate Python binary operator overload.
Note the result of / in python 3 (and python 2, if CO_FUTURE_DIVISION
is set) is interpreted as "true division", otherwise it is integer
division for integer arguments.
See Also:
PEP 238
- PydObject
opUnary
(string op)();
- Forwards to appropriate Python unary operator overload.
- PydObject
floor_div
(PydObject o);
- Forwards to PyNumber_FloorDivide for numbers, and method otherwise.
See Also:
PyNumber_FloorDivide
- PydObject
true_div
(PydObject o);
- Forwards to PyNumber_TrueDivide for numbers, and method otherwise.
See Also:
PyNumber_TrueDivide
- PydObject
divmod
(PydObject o);
- Equivalent to divmod(this, o) for numbers, and this.divmod(o)
otherwise.
See Also:
divmod
- PydObject
pow
(PydObject exp, PydObject mod = null);
- Equivalent to pow(this, exp, mod) for numbers, and this.pow(exp,mod)
otherwise.
See Also:
pow
- PydObject
abs
();
- Equivalent to abs(this) for numbers, and this.abs()
otherwise.
See Also:
abs
- PydObject
opOpAssign
(string op, T)(T o);
- Forwards to appropriate python in-place operator overload.
- PydObject
as_int
();
- Converts any Python number to int.
- PydObject
as_long
();
- Converts any Python number to long.
- PydObject
as_float
();
- Converts any Python number to float.
- Py_ssize_t
count
(PydObject v);
- Equivalent to 'this.
count
(v)' in Python.
- Py_ssize_t
index
(PydObject v);
- Equivalent to 'this.
index
(v)' in Python
- PydObject
as_list
();
- Converts any iterable PydObject to a list
- PydObject
as_tuple
();
- Converts any iterable PydObject to a tuple
- void
insert
(int i, PydObject item);
- Equivalent to 'this.insert(i,item)' in python.
- void
append
(PydObject item);
- Equivalent to 'this.append(item)' in python.
- void
sort
();
- Equivalent to 'this.sort()' in Python.
- void
reverse
();
- Equivalent to 'this.
reverse
()' in Python.
- bool
opBinaryRight
(string op, T)(T v);
bool
opBinaryRight
(string op, T)(T key);
- Equivalent to "v in this" in Python.
- bool
has_key
(string key);
bool
has_key
(PydObject key);
- Equivalent to 'key in this' in Python.
- PydObject
keys
();
- Equivalent to 'this.keys()' in Python.
- PydObject
values
();
- Equivalent to 'this.values()' in Python.
- PydObject
items
();
- Equivalent to 'this.items()' in Python.
- void
clear
();
- For dicts, wraps PyDict_Clear. Otherwise forwards to method.
See Also:
PyDict_Clear
- PydObject
copy
();
- For dicts, wraps PyDict_Copy. Otherwise forwards to method.
See Also:
PyDict_Copy
- void
merge
(PydObject o, bool override_ = true);
- For dicts, wraps PyDict_Merge. Otherwise forwards to method.
See Also:
PyDict_Merge
- PydObject
getdict
();
- For module objects, wraps PyModule_GetDict (essentially a dir()
operation in Python). Otherwise forwards to method.
See Also:
PyModule_GetDict
- @property auto
opDispatch
(string nom)();
- Forwards to getattr
- void
opDispatch
(string nom, T)(T val);
- Forwards to setattr
- auto
opDispatch
(string nom, T...)(T ts);
- Forwards to method.
- @property PydObject
None
();
- Convenience wrapper for Py_None
- struct
PydInputRange
(E = PydObject);
- Wrap a python iterator in a D input range.
Params:
| E |
element type of this range. converts elements of iterator to E. |
- this(PyObject* obj);
- this(Borrowed!(PyObject)* bobj);
- @property auto
front
();
- @property auto
empty
();
- void
popFront
();
|