BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
PyUtils.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Base/Py/PyUtils.h
6 //! @brief Defines PyUtils namespace
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #ifdef SWIG
16 #error no need to expose this header to Swig
17 #endif
18 
19 #ifndef USER_API
20 #ifndef BORNAGAIN_BASE_PY_PYUTILS_H
21 #define BORNAGAIN_BASE_PY_PYUTILS_H
22 
23 #ifdef BORNAGAIN_PYTHON
24 
25 #include "Base/Py/PyObject.h"
26 #include <memory>
27 #include <string>
28 #include <vector>
29 
30 class MultiLayer;
31 
32 namespace PyUtils {
33 
34 //! Converts PyObject into string, if possible, or throws exception.
35 std::string toString(PyObject* obj);
36 
37 //! Converts PyObject into vector of strings, if possible, or throws exception.
38 std::vector<std::string> toVectorString(PyObject* obj);
39 
40 //! Converts char to string. In the case of nullptr will return an empty string.
41 std::string toString(char* c);
42 std::string toString(wchar_t* c);
43 
44 //! Imports BornAgain from given location. If path is empty, tries to rely on PYTHONPATH.
45 void import_bornagain(const std::string& path = "");
46 
47 //! Returns multi-line string representing PATH, PYTHONPATH, sys.path and other info.
48 std::string pythonRuntimeInfo();
49 
50 //! Returns string representing python stack trace.
51 std::string pythonStackTrace();
52 
53 PyObject* createNumpyArray(const std::vector<double>& data);
54 
55 } // namespace PyUtils
56 
57 #endif // BORNAGAIN_PYTHON
58 
59 #endif // BORNAGAIN_BASE_PY_PYUTILS_H
60 #endif // USER_API
PyObvject forward declaration.
_object PyObject
Definition: PyObject.h:25
Our sample model: a stack of layers one below the other.
Definition: MultiLayer.h:41
std::vector< std::string > toVectorString(PyObject *obj)
Converts PyObject into vector of strings, if possible, or throws exception.
Definition: PyUtils.cpp:33
PyObject * createNumpyArray(const std::vector< double > &data)
Definition: PyUtils.cpp:172
std::string toString(PyObject *obj)
Converts PyObject into string, if possible, or throws exception.
Definition: PyUtils.cpp:24
std::string pythonStackTrace()
Returns string representing python stack trace.
Definition: PyUtils.cpp:132
void import_bornagain(const std::string &path="")
Imports BornAgain from given location. If path is empty, tries to rely on PYTHONPATH.
Definition: PyUtils.cpp:71
std::string pythonRuntimeInfo()
Returns multi-line string representing PATH, PYTHONPATH, sys.path and other info.
Definition: PyUtils.cpp:100