LSSTApplications  12.1-5-gbdcc3ab,15.0+10,15.0+24,15.0-1-g19261fa+15,15.0-1-g60afb23+24,15.0-1-g615e0bb+16,15.0-1-g6668b0b+6,15.0-1-g788a293+24,15.0-1-ga91101e+24,15.0-1-gae1598d+9,15.0-1-gd076f1f+22,15.0-1-gdf18595+3,15.0-1-gf4f1c34+9,15.0-11-g7db6e543+2,15.0-12-g3681e7a+2,15.0-13-g5992896b,15.0-13-g7441b25,15.0-2-g100d730+17,15.0-2-g1f9c9cf+2,15.0-2-gf38729e+19,15.0-27-g6268be7ac,15.0-3-g11fe1a0+11,15.0-3-g52118bc+2,15.0-3-g707930d+1,15.0-3-g7b706ea+2,15.0-3-g9103c06+9,15.0-3-gd3cbb57+1,15.0-4-g535e784+8,15.0-4-g92ca6c3+2,15.0-4-gf906033,15.0-5-g23e394c+12,15.0-5-g993f083+4,15.0-5-gf788cd9+2,15.0-6-gdf787ba+1,15.0-6-gfa9b38f+2,15.0-7-g949993c+1,15.0-7-gbf600c9+3,15.0-8-gc213bcc,w.2018.20
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