Skip to content

Commit 57bcbbd

Browse files
committed
Extract annotate
1 parent 95d0cd7 commit 57bcbbd

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

annotate.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
3+
#include "_python.h"
4+
#include "interpreter.h"
5+
6+
namespace matplotlibcpp {
7+
inline bool annotate(std::string annotation, double x, double y)
8+
{
9+
PyObject * xy = PyTuple_New(2);
10+
PyObject * str = PyString_FromString(annotation.c_str());
11+
12+
PyTuple_SetItem(xy,0,PyFloat_FromDouble(x));
13+
PyTuple_SetItem(xy,1,PyFloat_FromDouble(y));
14+
15+
PyObject* kwargs = PyDict_New();
16+
PyDict_SetItemString(kwargs, "xy", xy);
17+
18+
PyObject* args = PyTuple_New(1);
19+
PyTuple_SetItem(args, 0, str);
20+
21+
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_annotate, args, kwargs);
22+
23+
Py_DECREF(args);
24+
Py_DECREF(kwargs);
25+
26+
if(res != nullptr) Py_DECREF(res);
27+
28+
return res != nullptr;
29+
}
30+
} // end namespace matplotlibcpp

matplotlibcpp.h

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,10 @@
1515
#include "_python.h"
1616
#include "interpreter.h"
1717
#include "helpers.h"
18+
#include "annotate.h"
1819

1920

2021
namespace matplotlibcpp {
21-
inline bool annotate(std::string annotation, double x, double y)
22-
{
23-
PyObject * xy = PyTuple_New(2);
24-
PyObject * str = PyString_FromString(annotation.c_str());
25-
26-
PyTuple_SetItem(xy,0,PyFloat_FromDouble(x));
27-
PyTuple_SetItem(xy,1,PyFloat_FromDouble(y));
28-
29-
PyObject* kwargs = PyDict_New();
30-
PyDict_SetItemString(kwargs, "xy", xy);
31-
32-
PyObject* args = PyTuple_New(1);
33-
PyTuple_SetItem(args, 0, str);
34-
35-
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_annotate, args, kwargs);
36-
37-
Py_DECREF(args);
38-
Py_DECREF(kwargs);
39-
40-
if(res != nullptr) Py_DECREF(res);
41-
42-
return res != nullptr;
43-
}
44-
4522
#ifndef WITHOUT_NUMPY
4623
// Type selector for numpy array conversion
4724
template <typename T> struct select_npy_type { const static NPY_TYPES type = NPY_NOTYPE; }; //Default

0 commit comments

Comments
 (0)