LSSTApplications  11.0-24-g0a022a1,12.1-4-g110c6f4+31,15.0+15,15.0+9,15.0-1-g19261fa+7,15.0-1-g60afb23+15,15.0-1-g615e0bb+7,15.0-1-g6668b0b+5,15.0-1-g788a293+15,15.0-1-ga91101e+15,15.0-1-gae1598d+8,15.0-1-gd076f1f+14,15.0-1-gdf18595+2,15.0-1-gf4f1c34+8,15.0-2-g100d730+8,15.0-2-g18f3f21+9,15.0-2-g20c4630+3,15.0-2-g35685a8+10,15.0-2-g5dfaa72+3,15.0-2-gf38729e+9,15.0-23-g309a1dfe0+1,15.0-3-g150fc43+18,15.0-3-g6f085af+8,15.0-3-g707930d,15.0-3-g9103c06+8,15.0-3-ga03b4ca+21,15.0-3-gaec6799+5,15.0-4-g5589a47+1,15.0-4-g654b129+13,15.0-4-gff20472+18,15.0-5-g0db841d+2,15.0-5-g23e394c+2,15.0-6-g4cfb9db,15.0-6-g9a9df217+10,15.0-8-g0cd0e28,15.0-8-g11095dd+1,15.0-8-g306a5613+1
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
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:736