pyd.make_object
This module contains some useful type conversion functions. The two
most interesting operations here are python_to_d and d_to_python.
Additionally, the py function is provided as a convenience to directly
convert a D object into an instance of PydObject.
To convert a PydObject to a D type, use PydObject.to_d.
- void
ex_d_to_python
(dg_t)(dg_t dg);
- Extend pyd's conversion mechanism. Will be used by d_to_python only if d_to_python cannot
convert its argument by regular means.
Params:
| dg |
A callable which takes a D type and returns a PyObject*, or any
type convertible by d_to_python. |
- void
ex_python_to_d
(dg_t)(dg_t dg);
- Extend pyd's conversion mechanims. Will be used by python_to_d only if python_to_d
cannot convert its argument by regular means.
Params:
| dg |
A callable which takes a PyObject*, or any type convertible by python_to_d,
and returns a D type. |
- PyObject*
d_to_python
(T)(T t);
- Returns a new (owned) reference to a Python object based on the passed
argument. If the passed argument is a PyObject*, this "steals" the
reference. (In other words, it returns the PyObject* without changing its
reference count.) If the passed argument is a PydObject, this returns a new
reference to whatever the PydObject holds a reference to.
If the passed argument can't be converted to a PyObject, a Python
RuntimeError will be raised and this function will return null.
- PyObject*
PyTuple_FromItems
(T...)(T t);
- Helper function for creating a PyTuple from a series of D items.
- PydObject
py
(T)(T t);
- Constructs an object based on the type of the argument passed in.
For example, calling
py
(10) would return a PydObject holding the value 10.
Calling this with a PydObject will return back a reference to the very same
PydObject.
- class
PydConversionException
: object.Exception;
- An exception class used by python_to_d.
- T
python_to_d
(T)(PyObject* o);
- This converts a PyObject* to a D type. The template argument is the type to
convert to. The function argument is the PyObject* to convert. For instance:
PyObject* i = PyInt_FromLong(20);
int n = python_to_d!(int)(i);
assert(n == 20);
This throws a PydConversionException if the PyObject can't be converted to
the given D type.
- T
python_array_array_to_d
(T)(PyObject* o);
- Convert an array.array object to a D object.
Used by python_to_d.
- PyObject*
d_to_python_array_array
(T)(T t);
- Convert a d array to a python array.array.
array.array does not support 8 byte integers.
Not used by d_to_python.
- PyObject*
d_to_python_bytes
(T)(T t);
- Convert a D object to python bytes (str, in python 2).
- T
python_iter_to_d
(T)(PyObject* o);
- Convert an iterable Python object to a D object.
Used by python_to_d.
- T
python_buffer_to_d
(T)(PyObject* o);
- Convert a Python new-style buffer to a D object.
Used by python_to_d.
- auto
wrap_range
(Range)(Range range);
- Wrap a D input range as a python iterator object.
Does not work for UFCS ranges (e.g. arrays), classes
- struct
RangeWrapper
;
- Wrapper type wrapping a D input range as a python iterator object
Lives in reserved python module "pyd".
- bool
match_format_type
(T)(string format);
- Check T against format
See Also:
Struct Format Strings
- template
SimpleFormatType
(T)
- generate a struct format string from T
- template
IsStaticArrayPointer
(T)
- Check that T is a pointer to a rectangular static array.
- template
MatrixInfo
(T) if (isArray!(T) || IsStaticArrayPointer!(T))
- Some reflective information about multidimensional arrays
Handles dynamic arrays, static arrays, and pointers to static arrays.
- Py_ssize_t[]
build_shape
(T t);
- Build shape from t. Assumes all arrays in a dimension are initialized
and of uniform length.
- bool
check
(Py_ssize_t[] shape);
- Ensures that T can store a matrix of shape shape.
- string
matrixIter
(string arr_name, string shape_name, string index_name, size_t ndim, string pre_code, string post_code);
- Generate a mixin string of nested for loops that iterate over the
first ndim dimensions of an array of type T (or, preferrably
MatrixInfo!T.unqual).
Params:
| string arr_name |
name of array to iterate. |
| string shape_name |
name of array of dimension lengths. |
| string index_name |
name to use for index vector. Declared in a new nested scoped. |
| size_t ndim |
number of dimensions to iterate over. |
| string pre_code |
code to mixin each for loop before beginning the nested for loop. |
| string post_code |
code to mix in to each for loop after finishing the nested for loop. |
- alias
unqual
;
- T, with all nonmutable qualifiers stripped away.
- alias
dim_list
;
- tuple of dimensions of T.
dim_list
[0] will be the dimension furthest from the MatrixElementType
i.e. for double[1][2][3],
dim_list
== (3, 2, 1).
Lists -1 as dimension of dynamic arrays.
- bool
isRectArray
;
- T is a RectArray if:
* it is any multidimensional static array (or a pointer to)
* it is a 1 dimensional dynamic array
- size_t
rectArrayAt
;
- Highest dimension where it and all subsequent dimensions form a
RectArray.
- alias
RectArrayType
;
- unqualified highest dimension subtype of T forming RectArray
- string
dimstring
;
- Pretty string of dimension list for T
- alias
MatrixElementType
;
- Matrix element type of T
E.g. immutable(double) for T=immutable(double[4][4])
|