LSSTApplications  16.0-1-gce273f5+15,16.0-1-gf77f410+8,16.0-10-g230e10e+8,16.0-10-g5a5abec+4,16.0-10-gc1446dd+8,16.0-11-g5cf5345,16.0-12-g1dc09ba+2,16.0-12-g726f8f3+6,16.0-13-g4c33ca5+8,16.0-13-g5f6c0b1+8,16.0-13-gd9b1b71+8,16.0-14-g71e547a+4,16.0-17-g0bdc215,16.0-17-g6a7bfb3b+8,16.0-18-g95848a16+2,16.0-2-g0febb12+12,16.0-2-g839ba83+46,16.0-2-g9d5294e+35,16.0-3-g404ea43+7,16.0-3-gbc759ec+6,16.0-3-gcfd6c53+33,16.0-4-g03cf288+24,16.0-4-g13a27c5+10,16.0-4-g2cc461c+3,16.0-4-g5f3a788+11,16.0-4-g8a0f11a+30,16.0-4-ga3eb747+1,16.0-43-gaa65a4573+4,16.0-5-g1991253+8,16.0-5-g865efd9+8,16.0-5-gb3f8a4b+40,16.0-5-gd0f1235+4,16.0-6-g316b399+8,16.0-6-gf0acd13+27,16.0-6-gf9cb114+9,16.0-7-ga8e1655+4,16.0-8-g23bbf3f+1,16.0-8-g4dec96c+21,16.0-8-gfd407c0,w.2018.39
LSSTDataManagementBasePackage
python.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * See COPYRIGHT file at the top of the source tree.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 
24 #ifndef LSST_UTILS_PYTHON_H
25 #define LSST_UTILS_PYTHON_H
26 
27 #include "pybind11/pybind11.h"
28 
29 #include <cstddef>
30 #include <memory>
31 #include <string>
32 #include <sstream>
33 #include <utility>
34 
35 #include "lsst/pex/exceptions.h"
36 
37 namespace lsst {
38 namespace utils {
39 namespace python {
40 
57 template<typename T, typename PyClass>
58 inline void addSharedPtrEquality(PyClass & cls) {
59  cls.def("__eq__",
60  [](std::shared_ptr<T> self, std::shared_ptr<T> other) { return self.get() == other.get(); },
61  pybind11::is_operator());
62  cls.def("__ne__",
63  [](std::shared_ptr<T> self, std::shared_ptr<T> other) { return self.get() != other.get(); },
64  pybind11::is_operator());
65 }
66 
81 template <class PyClass>
82 void addOutputOp(PyClass &cls, std::string const &method) {
83  cls.def(method.c_str(), [](typename PyClass::type const &self) {
85  os << self;
86  return os.str();
87  });
88 }
89 
98 template <class PyClass>
99 void addHash(PyClass &cls) {
100  using Class = typename PyClass::type;
101  cls.def("__hash__", [](Class const &self) {
102  static auto const hash = std::hash<Class>();
103  return hash(self);
104  });
105 }
106 
120  auto const i_orig = i;
121  if (i < 0) {
122  // index backwards from the end
123  i += size;
124  }
125  if (i < 0 || i >= size) {
127  os << "Index " << i_orig << " not in range [" << -size << ", " << size - 1 << "]";
128  throw pybind11::index_error(os.str());
129  }
130  return static_cast<std::size_t>(i);
131 }
132 
147  try {
148  return {cppIndex(size_i, i), cppIndex(size_j, j)};
151  os << "Index (" << i << ", " << j << ") not in range ["
152  << -size_i << ", " << size_i - 1 << "], ["
153  << -size_j << ", " << size_j - 1 << "]";
154  throw pybind11::index_error(os.str());
155  }
156 }
157 
158 }}} // namespace lsst::utils::python
159 
160 #endif
void addOutputOp(PyClass &cls, std::string const &method)
Add __str__ or __repr__ method implemented by operator<<.
Definition: python.h:82
std::size_t cppIndex(std::ptrdiff_t size, std::ptrdiff_t i)
Compute a C++ index from a Python index (negative values count from the end) and range-check.
Definition: python.h:119
STL class.
A base class for image defects.
Definition: cameraGeom.dox:3
T str(T... args)
void addHash(PyClass &cls)
Add __hash__ method implemented by std::hash.
Definition: python.h:99
T get(T... args)
Reports attempts to access elements outside a valid range of indices.
Definition: Runtime.h:89
void addSharedPtrEquality(PyClass &cls)
Add __eq__ and __ne__ methods based on two std::shared_ptr<T> pointing to the same address...
Definition: python.h:58
T c_str(T... args)
ItemVariant const * other
Definition: Schema.cc:55
std::ostream * os
Definition: Schema.cc:737