Michele De Stefano's C++ Utilities
mds_utils::python::Dictionary Class Reference

Wraps a Python dictionary. More...

#include <mds_utils/python/dictionary.hpp>

Inheritance diagram for mds_utils::python::Dictionary:
mds_utils::python::Obj

Public Member Functions

 Dictionary ()
 Default constructor.
 
 Dictionary (PyObject *po)
 Construct from a Python object. More...
 
 Dictionary (const Dictionary &rhs)
 Copy constructor.
 
 Dictionary (Dictionary &&rhs)
 Move constructor.
 
void copy (const Dictionary &rhs)
 copies a dictionary into the current one.
 
void clear ()
 Empty an existing dictionary of all key-value pairs.
 
Dictionaryoperator= (Dictionary &&rhs)
 Move assignment.
 
Dictionaryoperator= (const Dictionary &rhs)
 Standard assignment.
 
template<class Key_T >
bool contains (const Key_T &key) const
 Determine if the dictionary contains key. More...
 
template<class Key_T , class Val_T >
void set_item (const Key_T &key, const Val_T &val)
 Inserts a key-value pair into the dictionary. More...
 
template<class Key_T >
void del_item (const Key_T &key)
 Removes an item from the dictionary. More...
 
template<class Key_T >
Obj get_item (const Key_T &key) const
 Returns the item corresponding to a key. More...
 
List items () const
 Returns a list of all the items of the dictionary.
 
List keys () const
 Returns a list of all the keys of the dictionary.
 
List values () const
 Returns a list of all the values of the dictionary.
 
size_t len () const
 Returns the length of the dictionary.
 
void update (const Dictionary &b)
 Updates the current dictionary with the key-value pairs from another dictionary. More...
 
- Public Member Functions inherited from mds_utils::python::Obj
 Obj ()
 Default constructor.
 
 Obj (const Obj &rhs)
 The copy-constructor. More...
 
 Obj (Obj &&rhs)
 Move constructor. More...
 
 Obj (ProxyAttr &&rhs)
 Move constructor from ProxyAttr objects. More...
 
 Obj (PyObject *po)
 Construct from a Python object. More...
 
Objoperator= (const Obj &rhs)
 Standard assignment.
 
Objoperator= (Obj &&rhs)
 Move assignment.
 
virtual Objoperator= (PyObject *po)
 Assignment from a Python object. More...
 
template<class T >
Objoperator= (const T &val)
 Assignment from a value. More...
 
virtual ~Obj ()
 Destructor.
 
PyObject * getPyObject () const
 Returns the underlying PyObject. More...
 
 operator PyObject * () const
 Automatic type conversion towards a Python object. More...
 
virtual PyObject * transfer ()
 Returns the Python object with transferred ownership. More...
 
virtual void incref ()
 Increments the reference count using Py_XINCREF. More...
 
virtual void decref ()
 Decrements the reference count using Py_XDECREF.
 
void reset ()
 Resets the object to the state given by the default constructor.
 
void get_ownership ()
 Used in place of incref, when the wrapped PyObject* was increfed already.
 
bool has_attr (const std::string &name) const
 Tests if the object has a particular attribute.
 
ProxyAttr attr (const std::string &name)
 Retrieves an attribute. More...
 
bool is_callable () const
 Checks if the object is callable. More...
 
Obj operator() ()
 Calling operator. More...
 
Obj operator() (const Obj &args)
 Calling operator, with positional arguments. More...
 
Obj operator() (const Obj &args, const Obj &kw)
 Calling operator, with positional and keyword arguments. More...
 

Additional Inherited Members

- Protected Member Functions inherited from mds_utils::python::Obj
void check_callable ()
 Checks if the object is callable. More...
 
void check_call_result (PyObject *result)
 Checks if the result of the object call was successful. More...
 
- Protected Attributes inherited from mds_utils::python::Obj
size_t decref_on_destroy
 
PyObject * m_po
 Underlying pointer to the wrapped Python object.
 

Detailed Description

Wraps a Python dictionary.

Not all the dictionary API has currently been wrapped.

Warning
  • Be sure to have include proper overloads of the mds_utils::python::to_python function.
  • Remember that the copy/move constructors and the assignement operators do not duplicate the underlying Python object. They only increment its reference count.
Author
Michele De Stefano
Date
20/04/2014

Definition at line 50 of file dictionary.hpp.

Constructor & Destructor Documentation

mds_utils::python::Dictionary::Dictionary ( PyObject *  po)
inline

Construct from a Python object.

Parameters
poPointer to the Python object.
Warning
It does not incref the po pointer. You have to do it manually, if needed, on the Dictionary instance. In Python terminology, the reference to po is borrowed.
Author
Michele De Stefano
Date
20/04/2014

Definition at line 76 of file dictionary.hpp.

Member Function Documentation

template<class Key_T >
bool mds_utils::python::Dictionary::contains ( const Key_T &  key) const
inline

Determine if the dictionary contains key.

This is equivalent to the Python expression key in p.

Template Parameters
Key_TType of the key.
Parameters
keyThe key to be searched.
Returns
If an item matches key, return true, otherwise return false. On error, throws an exception.
Author
Michele De Stefano
Date
20/04/2014

Definition at line 130 of file dictionary.hpp.

References mds_utils::python::Obj::m_po, and mds_utils::python::to_python().

template<class Key_T >
void mds_utils::python::Dictionary::del_item ( const Key_T &  key)
inline

Removes an item from the dictionary.

Template Parameters
Key_TType of the key.
Parameters
keyThe key of the element to be removed.
Author
Michele De Stefano
Date
20/04/2014

Definition at line 176 of file dictionary.hpp.

References mds_utils::python::Obj::get_ownership(), mds_utils::python::Obj::m_po, and mds_utils::python::to_python().

template<class Key_T >
Obj mds_utils::python::Dictionary::get_item ( const Key_T &  key) const
inline

Returns the item corresponding to a key.

Template Parameters
Key_TType of the key.
Parameters
keyThe key of the element to be extracted.
Returns
The extracted value (the returned object owns the reference).
Author
Michele De Stefano
Date
21/04/2014

Definition at line 199 of file dictionary.hpp.

References mds_utils::python::Obj::incref(), mds_utils::python::Obj::m_po, and mds_utils::python::to_python().

template<class Key_T , class Val_T >
void mds_utils::python::Dictionary::set_item ( const Key_T &  key,
const Val_T &  val 
)
inline

Inserts a key-value pair into the dictionary.

Template Parameters
Key_TType of the key.
Val_TType of the value.
Parameters
keyThe key.
valThe value.
Author
Michele De Stefano
Date
20/04/2014

Definition at line 152 of file dictionary.hpp.

References mds_utils::python::Obj::get_ownership(), mds_utils::python::Obj::m_po, and mds_utils::python::to_python().

void mds_utils::python::Dictionary::update ( const Dictionary b)
inline

Updates the current dictionary with the key-value pairs from another dictionary.

Parameters
bThe second dictionary.
Author
Michele De Stefano
Date
20/04/2014

Definition at line 249 of file dictionary.hpp.

References mds_utils::python::Obj::m_po.


The documentation for this class was generated from the following file: