GEOSX
Classes | Enumerations | Functions
LvArray::python Namespace Reference

Contains all the Python code. More...

Classes

class  PyObjectRef
 A class that manages an owned Python reference with RAII semantics. More...
 
class  PythonError
 Base class for all C++ exceptions related to Python. More...
 
class  PythonFunction
 A C++ functor wrapper around a Python function. More...
 

Enumerations

enum  PyModify { READ_ONLY = 0, MODIFIABLE = 1, RESIZEABLE = 2 }
 An enumeration of the various access policies for Python objects.
 

Functions

bool import_array_wrapper ()
 Attempt to import the NumPy API if it was not already imported, return true iff successful. More...
 
template<typename T , typename INDEX_TYPE >
std::enable_if_t< internal::canExportToNumpy< T >, PyObject *> createNumPyArray (T *const data, bool const modify, int const ndim, INDEX_TYPE const *const dimsPtr, INDEX_TYPE const *const stridesPtr)
 Return a NumPy ndarray view of data. More...
 
template<typename T >
std::enable_if_t< internal::canExportToNumpy< T >, PyObject *> create (T &value)
 Create a NumPy 1D array of length 1 containing the scalar value. More...
 
std::tuple< PyObjectRef< PyObject >, void const *, long long > parseNumPyArray (PyObject *const obj, std::type_index const expectedType)
 Attempt to parse obj into a NumPy ndarray of type expectedType. More...
 
std::type_index getTypeIndexFromNumPy (int const numpyType)
 Return the std::type_index corresponding to the NumPy type numpyType. More...
 
std::string getNumPyTypeName (int const numpyType)
 Return the name corresponding to the NumPy type numpyType. More...
 
PyObject * getNumPyTypeObject (std::type_index const typeIndex)
 Return the NumPy type object corresponding to typeIndex. More...
 
template<typename T , int NDIM, typename PERM , typename INDEX_TYPE , template< typename > class BUFFER_TYPE>
std::enable_if_t< internal::canExportToNumpy< T >||(std::is_same< T, std::string >::value &&NDIM==1), PyObject *> create (Array< T, NDIM, PERM, INDEX_TYPE, BUFFER_TYPE > &array)
 Create a Python object corresponding to array. More...
 
PyTypeObject * getPyArrayType ()
 Return the Python type for the Array. More...
 
template<typename T , typename INDEX_TYPE , template< typename > class BUFFER_TYPE>
std::enable_if_t< internal::canExportToNumpy< T >, PyObject *> create (ArrayOfArrays< T, INDEX_TYPE, BUFFER_TYPE > &arrayOfArrays)
 Create a Python object corresponding to arrayOfArrays. More...
 
PyTypeObject * getPyArrayOfArraysType ()
 Return the Python type object for the ArrayOfArrays. More...
 
template<typename T , typename INDEX_TYPE , template< typename > class BUFFER_TYPE>
std::enable_if_t< internal::canExportToNumpy< T >, PyObject *> create (ArrayOfSets< T, INDEX_TYPE, BUFFER_TYPE > &arrayOfSets)
 Create a Python object corresponding to arrayOfSets. More...
 
PyTypeObject * getPyArrayOfSetsType ()
 Return the Python type for the ArrayOfSets. More...
 
template<typename T , typename COL_TYPE , typename INDEX_TYPE , template< typename > class BUFFER_TYPE>
std::enable_if_t< internal::canExportToNumpy< T >, PyObject *> create (CRSMatrix< T, COL_TYPE, INDEX_TYPE, BUFFER_TYPE > &matrix)
 Create a Python object corresponding to matrix. More...
 
PyTypeObject * getPyCRSMatrixType ()
 Return the Python type for the CRSMatrix. More...
 
template<typename T , typename INDEX_TYPE , template< typename > class BUFFER_TYPE>
std::enable_if_t< internal::canExportToNumpy< T >, PyObject *> create (SortedArray< T, INDEX_TYPE, BUFFER_TYPE > &sortedArray)
 Create a Python object corresponding to sortedArray. More...
 
PyTypeObject * getPySortedArrayType ()
 Return the Python type for the SortedArray. More...
 
bool addPyLvArrayModule (PyObject *module)
 add the pylvarray module, which defines all of the pylvarray classes, to a module. More...
 
 IS_VALID_EXPRESSION (CanCreate, T, LvArray::python::create(std::declval< T & >()))
 Expands to a static constexpr template bool. More...
 
template<typename T >
T * convert (PyObject *const obj, PyTypeObject *const type)
 Return obj casted to T if obj is an instance of type or nullptr if it is not. More...
 
bool addTypeToModule (PyObject *const module, PyTypeObject *const type, char const *const typeName)
 Add the Python type type to the module module. More...
 
PyObject * create (std::string const &value)
 Return a Python string copy of value. More...
 
PyObject * createPyListOfStrings (std::string const *const strptr, long long const size)
 Create and return a Python list of strings from an array of std::strings. The Python strings will be copies. More...
 
PyObject * create (std::vector< std::string > const &vec)
 Create and return a Python list of strings from a std::vector of std::strings. The Python strings will be copies. More...
 

Detailed Description

Contains all the Python code.

Function Documentation

◆ addPyLvArrayModule()

bool LvArray::python::addPyLvArrayModule ( PyObject *  module)

add the pylvarray module, which defines all of the pylvarray classes, to a module.

Parameters
modulethe Python module object to add pylvarray to as the attribute named "pylvarray". Equivalent to import pylvarray; module.pylvarray = pylvarray.
Returns
true if the import and addition succeeded, false otherwise.
Note
It is recommended to invoke this function before using pylvarray's C++ API, e.g. in a module initialization function, since it ensures that all of the pylvarray types are defined. If the types are not defined but are attempted to be exported to python, it will trigger a segfault.

◆ addTypeToModule()

bool LvArray::python::addTypeToModule ( PyObject *const  module,
PyTypeObject *const  type,
char const *const  typeName 
)

Add the Python type type to the module module.

Parameters
moduleThe Python module to add type to.
typeThe Python type to add to module.
typeNameThe name to give the type.
Returns
true iff the operation was successful.

◆ convert()

template<typename T >
T* LvArray::python::convert ( PyObject *const  obj,
PyTypeObject *const  type 
)

Return obj casted to T if obj is an instance of type or nullptr if it is not.

Template Parameters
TThe type to cast obj to.
Parameters
objThe object.
typeThe type.
Returns
obj casted to T if obj is an instance of type or nullptr if it is not.

Definition at line 272 of file pythonHelpers.hpp.

◆ create() [1/8]

template<typename T >
std::enable_if_t< internal::canExportToNumpy< T >, PyObject * > LvArray::python::create ( T &  value)

Create a NumPy 1D array of length 1 containing the scalar value.

Template Parameters
TThe type of the scalar value, if const then the returned array is immutable.
Parameters
valueThe type to export.
Returns
A NumPy 1D array of length 1.

Definition at line 147 of file numpyHelpers.hpp.

◆ create() [2/8]

template<typename T , typename INDEX_TYPE , template< typename > class BUFFER_TYPE>
std::enable_if_t< internal::canExportToNumpy< T >, PyObject * > LvArray::python::create ( SortedArray< T, INDEX_TYPE, BUFFER_TYPE > &  sortedArray)

Create a Python object corresponding to sortedArray.

Template Parameters
TThe type of values in the SortedArray.
INDEX_TYPEThe index type of the SortedArray.
BUFFER_TYPEThe buffer type of the SortedArray.
Parameters
sortedArrayThe SortedArray to export to Python.
Returns
A Python object corresponding to sortedArray.
Note
The returned Python object holds a reference to sortedArray, you must ensure the reference remains valid throughout the lifetime of the object.

Definition at line 216 of file PySortedArray.hpp.

◆ create() [3/8]

template<typename T , typename INDEX_TYPE , template< typename > class BUFFER_TYPE>
std::enable_if_t< internal::canExportToNumpy< T >, PyObject * > LvArray::python::create ( ArrayOfSets< T, INDEX_TYPE, BUFFER_TYPE > &  arrayOfSets)

Create a Python object corresponding to arrayOfSets.

Template Parameters
TThe type of the entries in the ArrayOfSets.
INDEX_TYPEThe index type of the ArrayOfSets.
BUFFER_TYPEThe buffer type of the ArrayOfSets.
Parameters
arrayOfSetsThe ArrayOfSets to export to Python.
Returns
A Python object corresponding to arrayOfSets.
Note
The returned Python object holds a reference to arrayOfSets, you must ensure the reference remains valid throughout the lifetime of the object.

Definition at line 271 of file PyArrayOfSets.hpp.

◆ create() [4/8]

template<typename T , int NDIM, typename PERM , typename INDEX_TYPE , template< typename > class BUFFER_TYPE>
std::enable_if_t< internal::canExportToNumpy< T > || ( std::is_same< T, std::string >::value && NDIM == 1 ), PyObject * > LvArray::python::create ( Array< T, NDIM, PERM, INDEX_TYPE, BUFFER_TYPE > &  array)

Create a Python object corresponding to array.

Template Parameters
TThe type of values in the array.
NDIMThe dimensionality of the array.
PERMThe permutation of the array.
INDEX_TYPEThe index type of the array.
BUFFER_TYPEThe buffer type of the array.
Parameters
arrayThe Array to export to Python.
Returns
A Python object corresponding to array.
Note
The returned Python object holds a reference to array, you must ensure the reference remains valid throughout the lifetime of the object.

Definition at line 279 of file PyArray.hpp.

◆ create() [5/8]

template<typename T , typename INDEX_TYPE , template< typename > class BUFFER_TYPE>
std::enable_if_t< internal::canExportToNumpy< T >, PyObject * > LvArray::python::create ( ArrayOfArrays< T, INDEX_TYPE, BUFFER_TYPE > &  arrayOfArrays)

Create a Python object corresponding to arrayOfArrays.

Template Parameters
TThe type of the entries in the ArrayOfArrays.
INDEX_TYPEThe index type of the ArrayOfArrays.
BUFFER_TYPEThe buffer type of the ArrayOfArrays.
Parameters
arrayOfArraysThe ArrayOfArrays to export to Python.
Returns
A Python object corresponding to arrayOfArrays.
Note
The returned Python object holds a reference to arrayOfArrays, you must ensure the reference remains valid throughout the lifetime of the object.

Definition at line 287 of file PyArrayOfArrays.hpp.

◆ create() [6/8]

PyObject* LvArray::python::create ( std::string const &  value)

Return a Python string copy of value.

Parameters
valueThe string to copy into Python.
Returns
A Python string copy of value.

◆ create() [7/8]

PyObject* LvArray::python::create ( std::vector< std::string > const &  vec)
inline

Create and return a Python list of strings from a std::vector of std::strings. The Python strings will be copies.

Parameters
vecthe vector to convert.
Returns
A Python list of strings, or nullptr if there was an error.

Definition at line 315 of file pythonHelpers.hpp.

◆ create() [8/8]

template<typename T , typename COL_TYPE , typename INDEX_TYPE , template< typename > class BUFFER_TYPE>
std::enable_if_t< internal::canExportToNumpy< T >, PyObject * > LvArray::python::create ( CRSMatrix< T, COL_TYPE, INDEX_TYPE, BUFFER_TYPE > &  matrix)

Create a Python object corresponding to matrix.

Template Parameters
TThe type of the entries in the CRSMatrix.
COL_TYPEThe type of the columns in the CRSMatrix.
INDEX_TYPEThe index type of the CRSMatrix.
BUFFER_TYPEThe buffer type of the CRSMatrix.
Parameters
matrixThe CRSMatrix to export to Python.
Returns
A Python object corresponding to matrix.
Note
The returned Python object holds a reference to matrix, you must ensure the reference remains valid throughout the lifetime of the object.

Definition at line 408 of file PyCRSMatrix.hpp.

◆ createNumPyArray()

template<typename T , typename INDEX_TYPE >
std::enable_if_t< internal::canExportToNumpy< T >, PyObject * > LvArray::python::createNumPyArray ( T *const  data,
bool const  modify,
int const  ndim,
INDEX_TYPE const *const  dimsPtr,
INDEX_TYPE const *const  stridesPtr 
)

Return a NumPy ndarray view of data.

Template Parameters
TThe type of values in the array.
INDEX_TYPEThe index type of the array.
Parameters
dataA pointer to the data to export.
modifyIf the ndarray should have modifiable values, only has an effect iff
Template Parameters
Tis not const.
Parameters
ndimThe number of dimensions of data.
dimsPtrThe size of each dimension.
stridesPtrThe strides of each dimension.
Returns
A NumPy ndarray view of data.

Definition at line 107 of file numpyHelpers.hpp.

◆ createPyListOfStrings()

PyObject* LvArray::python::createPyListOfStrings ( std::string const *const  strptr,
long long const  size 
)

Create and return a Python list of strings from an array of std::strings. The Python strings will be copies.

Parameters
strptrA pointer to the strings to convert, must be of length size.
sizeThe number of strings in strptr.
Returns
A Python list of strings, or nullptr if there was an error.

◆ getNumPyTypeName()

std::string LvArray::python::getNumPyTypeName ( int const  numpyType)

Return the name corresponding to the NumPy type numpyType.

Parameters
numpyTypeThe NumPy type to convert.
Returns
The name corresponding to the NumPy type numpyType.

◆ getNumPyTypeObject()

PyObject* LvArray::python::getNumPyTypeObject ( std::type_index const  typeIndex)

Return the NumPy type object corresponding to typeIndex.

Parameters
typeIndexThe type to convert to a NumPy type.
Returns
The NumPy type object corresponding to typeIndex. If an error occurs a Python exception is set and a nullptr is returned.

◆ getPyArrayOfArraysType()

PyTypeObject* LvArray::python::getPyArrayOfArraysType ( )

Return the Python type object for the ArrayOfArrays.

Returns
The Python type object for the ArrayOfArrays.

◆ getPyArrayOfSetsType()

PyTypeObject* LvArray::python::getPyArrayOfSetsType ( )

Return the Python type for the ArrayOfSets.

Returns
The Python type object for the ArrayOfSets.

◆ getPyArrayType()

PyTypeObject* LvArray::python::getPyArrayType ( )

Return the Python type for the Array.

Returns
The Python type object for the Array.

◆ getPyCRSMatrixType()

PyTypeObject* LvArray::python::getPyCRSMatrixType ( )

Return the Python type for the CRSMatrix.

Returns
The Python type for the CRSMatrix.

◆ getPySortedArrayType()

PyTypeObject* LvArray::python::getPySortedArrayType ( )

Return the Python type for the SortedArray.

Returns
The Python type for the SortedArray.

◆ getTypeIndexFromNumPy()

std::type_index LvArray::python::getTypeIndexFromNumPy ( int const  numpyType)

Return the std::type_index corresponding to the NumPy type numpyType.

Parameters
numpyTypeThe NumPy type to convert.
Returns
The std::type_index corresponding to the NumPy type numpyType.

◆ import_array_wrapper()

bool LvArray::python::import_array_wrapper ( )

Attempt to import the NumPy API if it was not already imported, return true iff successful.

Returns
true iff the NumPy API was successfully imported.

◆ IS_VALID_EXPRESSION()

LvArray::python::IS_VALID_EXPRESSION ( CanCreate  ,
,
LvArray::python::create(std::declval< T &>())   
)

Expands to a static constexpr template bool.

CanCreate< T >

which is true iff T is a type which LvArray can export to Python.

◆ parseNumPyArray()

std::tuple< PyObjectRef< PyObject >, void const *, long long > LvArray::python::parseNumPyArray ( PyObject *const  obj,
std::type_index const  expectedType 
)

Attempt to parse obj into a NumPy ndarray of type expectedType.

Parameters
objThe object to parse.
expectedTypeThe expected type of the values in the ndarray.
Returns
A tripple containing a PyArrayObject, a pointer to the data, and the size of the array. If there was an error then the returned value is
{ nullptr, nullptr, 0 }
.