Re: [Numpy-discussion] Extending C with Python
With PyObject_CallMethod(pInstance, method_name, "O", py_dims); Can I pass in a reference and modify its content in python? Best, Jialin On Sat, Feb 3, 2018 at 10:29 AM, Jialin Liuwrote: > Thank you guys. > > Best, > Jialin > > Sent from my iPhone > > On Feb 3, 2018, at 8:11 AM, Sylvain Corlay > wrote: > > You can also check out pybind11, xtensor, and xtensor-python > > The latter enables a high-level numpy-like API on the C++ side. > > You can check out the numpy to xtensor cheat sheet: > > http://xtensor.readthedocs.io/en/latest/numpy.html > > Best, > > Sylvain > > > On Thu, Feb 1, 2018 at 12:11 AM, Stefan Seefeld > wrote: > >> On 31.01.2018 17:58, Chris Barker wrote: >> >> I'm guessing you could use Cython to make this easier. >> >> >> ... or Boost.Python (http://boostorg.github.io/python), which has >> built-in support for NumPy (http://boostorg.github.io/pyt >> hon/doc/html/numpy/index.html), and supports both directions: extending >> Python with C++, as well as embedding Python into C++ applications. >> >> >> >> >> -- >> >> ...ich hab' noch einen Koffer in Berlin... >> >> >> >> ___ >> NumPy-Discussion mailing list >> NumPy-Discussion@python.org >> https://mail.python.org/mailman/listinfo/numpy-discussion >> >> > ___ > NumPy-Discussion mailing list > NumPy-Discussion@python.org > https://mail.python.org/mailman/listinfo/numpy-discussion > > ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Extending C with Python
Thank you guys. Best, Jialin Sent from my iPhone > On Feb 3, 2018, at 8:11 AM, Sylvain Corlaywrote: > > You can also check out pybind11, xtensor, and xtensor-python > > The latter enables a high-level numpy-like API on the C++ side. > > You can check out the numpy to xtensor cheat sheet: > > http://xtensor.readthedocs.io/en/latest/numpy.html > > Best, > > Sylvain > > >> On Thu, Feb 1, 2018 at 12:11 AM, Stefan Seefeld wrote: >>> On 31.01.2018 17:58, Chris Barker wrote: >>> I'm guessing you could use Cython to make this easier. >> >> ... or Boost.Python (http://boostorg.github.io/python), which has built-in >> support for NumPy >> (http://boostorg.github.io/python/doc/html/numpy/index.html), and supports >> both directions: extending Python with C++, as well as embedding Python into >> C++ applications. >> >> >> >> -- >> >> ...ich hab' noch einen Koffer in Berlin... >> >> >> ___ >> NumPy-Discussion mailing list >> NumPy-Discussion@python.org >> https://mail.python.org/mailman/listinfo/numpy-discussion >> > > ___ > NumPy-Discussion mailing list > NumPy-Discussion@python.org > https://mail.python.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Extending C with Python
You can also check out pybind11, xtensor, and xtensor-python The latter enables a high-level numpy-like API on the C++ side. You can check out the numpy to xtensor cheat sheet: http://xtensor.readthedocs.io/en/latest/numpy.html Best, Sylvain On Thu, Feb 1, 2018 at 12:11 AM, Stefan Seefeldwrote: > On 31.01.2018 17:58, Chris Barker wrote: > > I'm guessing you could use Cython to make this easier. > > > ... or Boost.Python (http://boostorg.github.io/python), which has > built-in support for NumPy (http://boostorg.github.io/ > python/doc/html/numpy/index.html), and supports both directions: > extending Python with C++, as well as embedding Python into C++ > applications. > > > [image: Stefan] > > -- > > ...ich hab' noch einen Koffer in Berlin... > > > > ___ > NumPy-Discussion mailing list > NumPy-Discussion@python.org > https://mail.python.org/mailman/listinfo/numpy-discussion > > ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Extending C with Python
On 31.01.2018 17:58, Chris Barker wrote: > I'm guessing you could use Cython to make this easier. ... or Boost.Python (http://boostorg.github.io/python), which has built-in support for NumPy (http://boostorg.github.io/python/doc/html/numpy/index.html), and supports both directions: extending Python with C++, as well as embedding Python into C++ applications. Stefan -- ...ich hab' noch einen Koffer in Berlin... ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Extending C with Python
I'm guessing you could use Cython to make this easier. It's usually used for calling C from Python, but can do the sandwich in both directions... Just a thought -- it will help with some of that boilerplate code... -CHB On Tue, Jan 30, 2018 at 10:57 PM, Jialin Liuwrote: > Amazing! It works! Thank you Robert. > > I've been stuck with this many days. > > Best, > Jialin > LBNL/NERSC > > On Tue, Jan 30, 2018 at 10:52 PM, Robert Kern > wrote: > >> On Wed, Jan 31, 2018 at 3:25 PM, Jialin Liu wrote: >> >>> Hello, >>> I'm extending C with python (which is opposite way of what people >>> usually do, extending python with C), I'm currently stuck in passing a C >>> array to python layer, could anyone plz advise? >>> >>> I have a C buffer in my C code and want to pass it to a python function. >>> In the C code, I have: >>> >>> npy_intp dims [2]; dims[0] = 10; dims[1] = 20; import_array(); npy_intp m=2; PyObject * py_dims = PyArray_SimpleNewFromData(1, , NPY_INT16 ,(void *)dims ); // I also tried NPY_INT PyObject_CallMethod(pInstance, method_name, "O", py_dims); >>> >>> >>> In the Python code, I want to just print that array: >>> >>> def f(self, dims): >>> >>>print ("np array:%d,%d"%(dims[0],dims[1])) >>> >>> >>> >>> But it only prints the first number correctly, i.e., dims[0]. The second >>> number is always 0. >>> >> >> The correct typecode would be NPY_INTP. >> >> -- >> Robert Kern >> >> ___ >> NumPy-Discussion mailing list >> NumPy-Discussion@python.org >> https://mail.python.org/mailman/listinfo/numpy-discussion >> >> > > ___ > NumPy-Discussion mailing list > NumPy-Discussion@python.org > https://mail.python.org/mailman/listinfo/numpy-discussion > > -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR(206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception chris.bar...@noaa.gov ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Extending C with Python
Amazing! It works! Thank you Robert. I've been stuck with this many days. Best, Jialin LBNL/NERSC On Tue, Jan 30, 2018 at 10:52 PM, Robert Kernwrote: > On Wed, Jan 31, 2018 at 3:25 PM, Jialin Liu wrote: > >> Hello, >> I'm extending C with python (which is opposite way of what people usually >> do, extending python with C), I'm currently stuck in passing a C array to >> python layer, could anyone plz advise? >> >> I have a C buffer in my C code and want to pass it to a python function. >> In the C code, I have: >> >> npy_intp dims [2]; >>> dims[0] = 10; >>> dims[1] = 20; >>> import_array(); >>> npy_intp m=2; >>> PyObject * py_dims = PyArray_SimpleNewFromData(1, , NPY_INT16 ,(void >>> *)dims ); // I also tried NPY_INT >>> PyObject_CallMethod(pInstance, method_name, "O", py_dims); >> >> >> In the Python code, I want to just print that array: >> >> def f(self, dims): >> >>print ("np array:%d,%d"%(dims[0],dims[1])) >> >> >> >> But it only prints the first number correctly, i.e., dims[0]. The second >> number is always 0. >> > > The correct typecode would be NPY_INTP. > > -- > Robert Kern > > ___ > NumPy-Discussion mailing list > NumPy-Discussion@python.org > https://mail.python.org/mailman/listinfo/numpy-discussion > > ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Extending C with Python
On Wed, Jan 31, 2018 at 3:25 PM, Jialin Liuwrote: > Hello, > I'm extending C with python (which is opposite way of what people usually > do, extending python with C), I'm currently stuck in passing a C array to > python layer, could anyone plz advise? > > I have a C buffer in my C code and want to pass it to a python function. > In the C code, I have: > > npy_intp dims [2]; >> dims[0] = 10; >> dims[1] = 20; >> import_array(); >> npy_intp m=2; >> PyObject * py_dims = PyArray_SimpleNewFromData(1, , NPY_INT16 ,(void >> *)dims ); // I also tried NPY_INT >> PyObject_CallMethod(pInstance, method_name, "O", py_dims); > > > In the Python code, I want to just print that array: > > def f(self, dims): > >print ("np array:%d,%d"%(dims[0],dims[1])) > > > > But it only prints the first number correctly, i.e., dims[0]. The second > number is always 0. > The correct typecode would be NPY_INTP. -- Robert Kern ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion
[Numpy-discussion] Extending C with Python
Hello, I'm extending C with python (which is opposite way of what people usually do, extending python with C), I'm currently stuck in passing a C array to python layer, could anyone plz advise? I have a C buffer in my C code and want to pass it to a python function. In the C code, I have: npy_intp dims [2]; > dims[0] = 10; > dims[1] = 20; > import_array(); > npy_intp m=2; > PyObject * py_dims = PyArray_SimpleNewFromData(1, , NPY_INT16 ,(void > *)dims ); // I also tried NPY_INT > PyObject_CallMethod(pInstance, method_name, "O", py_dims); In the Python code, I want to just print that array: def f(self, dims): print ("np array:%d,%d"%(dims[0],dims[1])) But it only prints the first number correctly, i.e., dims[0]. The second number is always 0. Best, Jialin LBNL/NERSC ___ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion