LSSTApplications  16.0+22,16.0-1-g14407aa+7,16.0-1-g16dcc35+1,16.0-1-g928b041+1,16.0-1-g9884240+1,16.0-1-gce273f5+1,16.0-10-g0b41441+3,16.0-10-g549e159,16.0-11-ge4eb5c1,16.0-11-gf74ee13d,16.0-11-gf9130ea+4,16.0-2-g0febb12,16.0-2-g636dfb1,16.0-2-g839ba83+11,16.0-2-g9d5294e+8,16.0-25-g88abbd732,16.0-3-g0e3a094+1,16.0-3-g19f2ef8,16.0-3-g3806c63+1,16.0-3-g5dc86b7+11,16.0-3-g6923fb6+1,16.0-3-g9515088+5,16.0-3-gcfd6c53+6,16.0-4-g347813e+5,16.0-4-g50d071e+1,16.0-4-g5f3a788,16.0-4-g6c14c47+7,16.0-4-g7690030+6,16.0-4-gd6aeece+1,16.0-4-ge3254b7,16.0-5-gb3f8a4b+5,16.0-6-g9134ce5,16.0-6-gbe2f956+3,16.0-6-gf0acd13,16.0-7-g3dac777+5,16.0-8-g4aca173+6,16.0-9-g377a976,16.0-9-gc30114dc+3,16.0-9-gf78d8dd,w.2018.32
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:737