RDKit: PySequenceHolder.h Source File

| RDKit Open-source cheminformatics and machine learning.

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

PySequenceHolder.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2003-2006 greg Landrum and Rational Discovery LLC
3 //
4 // @@ All Rights Reserved @@
5 // This file is part of the RDKit.
6 // The contents are covered by the terms of the BSD license
7 // which is included in the file license.txt, found at the root
8 // of the RDKit source tree.
9 //
10
11 #ifndef _RD_PYSEQUENCEHOLDER_H_
12 #define _RD_PYSEQUENCEHOLDER_H_
13
14 //
15 // Defines a class to hold sequences passed in from Python
16 //
17 #include "Wrap.h"
18 #include <RDGeneral/Invariant.h>
19
20 namespace python = boost::python;
21
22 //! \brief Class to hold sequences (lists, tuples, arrays, etc.)
23 //! passed from Python -> C++
24 //!
25 //! PySequenceHolder is templated on the type of the contained object.
26 //!
27 //! The class is \em lazy: elements are not evaluated until requested
28 //! within the C++ code.
29 //!
30 template <typename T>
31 class PySequenceHolder {
32 public:
33 PySequenceHolder(python::object seq) { d_seq = seq; };
34
35 // --------------------------------------------------
36 //! \brief Returns the size of the contained sequence.
37 //!
38 //! NOTE: the sequence must have a \c __len__ attribute, otherwise
39 //! a \c ValueError will be raised.
40 unsigned int size() const {
41 unsigned int res = 0;
42 try {
43 res = python::extract<int>(d_seq.attr("__len__")());
44 } catch (...) {
45 throw_value_error("sequence does not support length query");
46 }
47 return res;
48 };
49
50 // --------------------------------------------------
51 //! \brief Returns an element of the sequence
52 //!
53 //! ARGUMENTS:
54 //! - which: an integer specifying which element should be returned.
55 //!
56 //! NOTES:
57 //! - if the sequence is not \a which elements long, we raise an
58 //! \c IndexError
59 //! - if the element cannot be converted to type \c T, we raise a
60 //! \c ValueError
61 T operator[](unsigned int which) const {
62 if (which > size()) {
63 throw_index_error(which);
64 }
65
66 try {
67 T res = python::extract<T>(d_seq[which]);
68 return res;
69 } catch (...) {
70 throw_value_error("cannot extract desired type from sequence");
71 }
72
73 POSTCONDITION(0, "cannot reach this point");
74 return static_cast<T>(T());
75 };
76
77 private:
78 python::object d_seq;
79 };
80
81 #endif
POSTCONDITION
#define POSTCONDITION(expr, mess)
Definition: Invariant.h:115
PySequenceHolder
Class to hold sequences (lists, tuples, arrays, etc.) passed from Python -> C++.
Definition: PySequenceHolder.h:31
Invariant.h
PySequenceHolder::size
unsigned int size() const
Returns the size of the contained sequence.
Definition: PySequenceHolder.h:40
PySequenceHolder::PySequenceHolder
PySequenceHolder(python::object seq)
Definition: PySequenceHolder.h:33
PySequenceHolder::operator[]
T operator[](unsigned int which) const
Returns an element of the sequence.
Definition: PySequenceHolder.h:61