LSSTApplications  11.0-24-g0a022a1,14.0+65,15.0,15.0+1,15.0+2,15.0+4,15.0-1-g14e9bfd,15.0-1-g1eca518+3,15.0-1-g499c38d+3,15.0-1-g60afb23+1,15.0-1-g6668b0b,15.0-1-g788a293+1,15.0-1-g82223af+3,15.0-1-ga91101e+1,15.0-1-gae1598d,15.0-1-gc45031d+3,15.0-1-gd076f1f+1,15.0-1-gf4f1c34,15.0-1-gfe1617d+1,15.0-17-g0b88e3da8,15.0-2-g2010ef9+1,15.0-2-g5218728+3,15.0-2-g947dc0d+3,15.0-3-g150fc43+2,15.0-3-g9103c06,15.0-3-ga03b4ca+3,15.0-3-ga695220+4,15.0-3-gaec6799,15.0-3-gb7a597c+1,15.0-4-g0478fed+4,15.0-4-g45f767a,15.0-4-gdd339b63+1,15.0-4-gff20472+4,15.0-5-ge02c9e7e,15.0-6-ge2d9597+3
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
161 
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
Include files required for standard LSST Exception handling.
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)
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:736