7 #ifndef MDS_UTILS_PYTHON_DICTIONARY_HPP_INCLUDED 8 #define MDS_UTILS_PYTHON_DICTIONARY_HPP_INCLUDED 52 static bool self_type_check(
const Obj& o) {
53 return PyDict_Check(o);
77 assert(self_type_check(o));
92 m_po = PyDict_Copy(rhs);
129 template<
class Key_T>
133 throw std::runtime_error(
"Error while searching for the " 136 return (retval == 1);
151 template<
class Key_T,
class Val_T>
157 pykey.get_ownership();
160 if (PyDict_SetItem(
m_po,pykey,pyval) == -1) {
161 throw std::runtime_error(
"Could not set item into dictionary.");
175 template<
class Key_T>
181 if (PyDict_DelItem(
m_po,key) == -1) {
182 throw std::runtime_error(
"Could not remove item from dictionary.");
198 template<
class Key_T>
202 throw std::runtime_error(
"Could not get item for the provided key");
213 out(PyDict_Items(
m_po));
221 out(PyDict_Keys(
m_po));
229 out(PyDict_Values(
m_po));
236 return PyDict_Size(
m_po);
250 if (PyDict_Update(
m_po,b) == -1) {
251 throw std::runtime_error(
"Cannot update dictionary.");
void reset()
Resets the object to the state given by the default constructor.
Contains a wrapper class for the Python list datatype.
virtual void incref()
Increments the reference count using Py_XINCREF.
PyObject * to_python(void)
Converts a value into a Python object.
Dictionary(PyObject *po)
Construct from a Python object.
void del_item(const Key_T &key)
Removes an item from the dictionary.
void set_item(const Key_T &key, const Val_T &val)
Inserts a key-value pair into the dictionary.
Dictionary()
Default constructor.
void clear()
Empty an existing dictionary of all key-value pairs.
Obj get_item(const Key_T &key) const
Returns the item corresponding to a key.
Main namespace of all Michele De Stefano's C++ utilities.
Dictionary & operator=(Dictionary &&rhs)
Move assignment.
Dictionary(Dictionary &&rhs)
Move constructor.
PyObject * m_po
Underlying pointer to the wrapped Python object.
Contains a wrapper class for the PyObject* datatype.
List values() const
Returns a list of all the values of the dictionary.
void get_ownership()
Used in place of incref, when the wrapped PyObject* was increfed already.
void update(const Dictionary &b)
Updates the current dictionary with the key-value pairs from another dictionary.
Obj & operator=(const Obj &rhs)
Standard assignment.
bool contains(const Key_T &key) const
Determine if the dictionary contains key.
void copy(const Dictionary &rhs)
copies a dictionary into the current one.
Dictionary(const Dictionary &rhs)
Copy constructor.
size_t len() const
Returns the length of the dictionary.
List keys() const
Returns a list of all the keys of the dictionary.
Wraps a Python dictionary.
List items() const
Returns a list of all the items of the dictionary.
This is a simple wrapper around the PyObject* datatype.