pyd.pydobject

The PydObject class wraps a PyObject*, using the D garbage collector to handle the reference count so that you don't have to. It also overloads quite a lot of operators, and tries to make using Python objects in D code as much like using them in Python as possible. However, it is incomplete (the function and method call methods in particular need work, and there are a number of helper functions that need to be written), and remains a work in progress.

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

See Also:
The Python/C API

this(PyObject * o, bool borrowed = false);
Wrap around a passed PyObject*.

Params:
PyObject * o The PyObject to wrap.
bool borrowed Whether o is a borrowed reference. Instances of PydObject always own their references. Therefore, Py_INCREF will be called if borrowed is true.

this();
The default constructor constructs an instance of the Py_None PydObject.

PyObject * ptr();
Returns a borrowed reference to the PyObject.

bool hasattr(char[] attr_name);
Same as hasattr(this, attr_name) in Python.

bool hasattr(PydObject attr_name);
Same as hasattr(this, attr_name) in Python.

PydObject getattr(char[] attr_name);
Same as getattr(this, attr_name) in Python.

PydObject getattr(PydObject attr_name);
Same as getattr(this, attr_name) in Python.

void setattr(char[] attr_name, PydObject v);
Same as setattr(this, attr_name, v) in Python.

void setattr(PydObject attr_name, PydObject v);
Same as setattr(this, attr_name, v) in Python.

void delattr(char[] attr_name);
Same as del this.attr_name in Python.

void delattr(PydObject attr_name);
Same as del this.attr_name in Python.

int opCmp(PydObject rhs);
Exposes Python object comparison to D. Same as cmp(this, rhs) in Python.

bool opEquals(PydObject rhs);
Exposes Python object equality check to D.

PydObject repr();
Same as repr(this) in Python.

PydObject str();
Same as str(this) in Python.

char[] toString();
Allows use of PydObject in writef via %s

PydObject unicode();
Same as unicode(this) in Python.

bool isInstance(PydObject cls);
Same as isinstance(this, cls) in Python.

bool isSubclass(PydObject cls);
Same as issubclass(this, cls) in Python. Only works if this is a class.

bool callable();
Same as callable(this) in Python.

PydObject opCall(PydObject args = null);
Calls the PydObject. (Note: The opCall functions will be changing in the future to something more useful.)

Params:
PydObject args Should be a PydTuple of the arguments to pass. Omit to call with no arguments.

Returns:
Whatever the function PydObject returns.

PydObject opCall(PydObject args, PydObject kw);
Calls the PydObject with positional and keyword arguments.

Params:
PydObject args Positional arguments. Should be a PydTuple. Pass an empty PydTuple for no positional arguments.
PydObject kw Keyword arguments. Should be a PydDict.

Returns:
Whatever the function PydObject returns.

PydObject method(char[] name, PydObject args = null);


int hash();
Same as hash(this) in Python.

bool toBool();
Same as "not not this" in Python.

bool not();
Same as "not this" in Python.

PydObject type();
Gets the type of this PydObject. Same as type(this) in Python.

Returns:
The type PydObject of this PydObject.

int length();
The length of this PydObject. Same as len(this) in Python.

int size();
Same as length()

PydObject dir();
Same as dir(this) in Python.

PydObject opIndex(PydObject key);
Equivalent to o[key] in Python.

PydObject opIndex(char[] 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(PydObject value, PydObject key);
Equivalent to o[key] = value in Python.

void opIndexAssign(PydObject value, char[] key);
Equivalent to o['key'] = value in Python. Usually only makes sense for mappings.

void opIndexAssign(PydObject value, int i);
Equivalent to o[i] = value in Python. Usually only makes sense for sequences.

void delItem(PydObject key);
Equivalent to del o[key] in Python.

void delItem(char[] key);
Equivalent to del o['key'] in Python. Usually only makes sense for mappings.

void delItem(int i);
Equivalent to del o[i] in Python. Usually only makes sense for sequences.

PydObject opSlice(int i1, int i2);
Equivalent to o[i1:i2] in Python.

PydObject opSlice();
Equivalent to o[:] in Python.

void opSliceAssign(PydObject v, int i1, int i2);
Equivalent to o[i1:i2] = v in Python.

void opSliceAssign(PydObject v);
Equivalent to o[:] = v in Python.

void delSlice(int i1, int i2);
Equivalent to del o[i1:i2] in Python.

void delSlice();
Equivalent to del o[:] in Python.

int opApply(int delegate(inout 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(inout PydObject, inout 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 opAdd(PydObject o);


PydObject opSub(PydObject o);


PydObject opMul(PydObject o);


PydObject opMul(int count);
Sequence repetition

PydObject opDiv(PydObject o);


PydObject floorDiv(PydObject o);


PydObject opMod(PydObject o);


PydObject divmod(PydObject o);


PydObject pow(PydObject o1, PydObject o2 = null);


PydObject opPos();


PydObject opNeg();


PydObject abs();


PydObject opCom();


PydObject opShl(PydObject o);


PydObject opShr(PydObject o);


PydObject opAnd(PydObject o);


PydObject opXor(PydObject o);


PydObject opOr(PydObject o);


PydObject opAddAssign(PydObject o);


PydObject opSubAssign(PydObject o);


PydObject opMulAssign(PydObject o);


PydObject opMulAssign(int count);
In-place sequence repetition

PydObject opDivAssign(PydObject o);


PydObject floorDivAssign(PydObject o);


PydObject opModAssign(PydObject o);


PydObject powAssign(PydObject o1, PydObject o2 = null);


PydObject opShlAssign(PydObject o);


PydObject opShrAssign(PydObject o);


PydObject opAndAssign(PydObject o);


PydObject opXorAssign(PydObject o);


PydObject opOrAssign(PydObject o);


PydObject asInt();


PydObject asLong();


PydObject asFloat();


int toLong();


long toLongLong();


double toDouble();


cdouble toComplex();


PydObject opCat(PydObject o);
Sequence concatenation

PydObject opCatAssign(PydObject o);
In-place sequence concatenation

int count(PydObject v);


int index(PydObject v);


PydObject asList();
Converts any iterable PydObject to a list

PydObject asTuple();
Converts any iterable PydObject to a tuple

bool opIn_r(PydObject v);
Same as "v in this" in Python.

bool hasKey(PydObject key);
Same as opIn_r

bool opIn_r(char[] key);
Same as "'v' in this" in Python.

bool hasKey(char[] key);
Same as opIn_r

PydObject keys();


PydObject values();


PydObject items();



Page generated by Ddoc.