21 template <
typename Map,
22 typename holder_type = std::unique_ptr<Map>,
25 const std::string &
name,
27 using KeyType =
typename Map::key_type;
28 using MappedType =
typename Map::mapped_type;
29 using Class_ = py::class_<Map, holder_type>;
34 auto tinfo = py::detail::get_type_info(
typeid(MappedType));
35 bool local = !tinfo || tinfo->module_local;
37 tinfo = py::detail::get_type_info(
typeid(KeyType));
38 local = !tinfo || tinfo->module_local;
41 Class_ cl(scope,
name.c_str(), pybind11::module_local(
local),
42 std::forward<Args>(args)...);
47 py::detail::map_if_insertion_operator<Map, Class_>(cl,
name);
50 "__bool__", [](
const Map &m) ->
bool {
return !m.empty(); },
51 "Check whether the map is nonempty");
56 [](Map &m) {
return py::make_key_iterator(m.begin(), m.end()); },
57 py::keep_alive<0, 1>()
64 [](Map &m) {
return py::make_iterator(m.begin(), m.end()); },
65 py::keep_alive<0, 1>());
69 [](Map &m,
const KeyType &k) -> MappedType & {
93 cl.def(
"__setitem__", [](Map &m,
const KeyType &k,
const MappedType &v) {
98 fmt::format(
"Cannot assign to reserved key \"{}\"", k));
102 cl.def(
"__contains__", [](Map &m,
const KeyType &k) ->
bool {
104 if (it == m.end())
return false;
109 py::detail::map_assignment<Map, Class_>(cl);
114 cl.def(
"__len__", [](
const Map &m) ->
size_t {
return m.size(); });
127 auto tm = bind_tensor_map<TensorMap>(
128 m,
"TensorMap",
"Map of String to Tensor with a primary key.");
130 tm.def(
"__delitem__",
131 [](
TensorMap &m,
const std::string &k) {
return m.Erase(k); });
134 [](
TensorMap &m,
const std::string &k) {
return m.Erase(k); });
137 tm.def(py::init<const std::string &>(),
"primary_key"_a);
138 tm.def(py::init<
const std::string &,
139 const std::unordered_map<std::string, core::Tensor> &>(),
140 "primary_key"_a,
"map_keys_to_tensors"_a);
152 std::unordered_map<std::string, core::Tensor> map;
153 for (
const auto &kv : m) {
154 map[kv.first] = kv.second;
157 return py::make_tuple(m.GetPrimaryKey(), map);
163 "Cannot unpickle TensorMap! Expecting a tuple of "
166 return TensorMap(t[0].cast<std::string>(),
167 t[1].cast<std::unordered_map<std::string,
171 tm.def(
"__setattr__",
177 "Cannot assign to reserved key \"{}\"", key));
181 tm.def(
"__getattr__",
183 auto it = m.find(key);
186 fmt::format(
"Key {} not found in TensorMap", key));
191 tm.def(
"__delattr__", [](
TensorMap &m,
const std::string &key) {
192 auto it = m.find(key);
195 fmt::format(
"Key {} not found in TensorMap", key));
205 auto keys = py::list();
206 for (
const auto &kv : m) {
207 keys.append(kv.first);
filament::Texture::InternalFormat format
bool IsSizeSynchronized() const
Returns true if all tensors in the map have the same size.
std::string ToString() const
Print the TensorMap to string.
void AssertSizeSynchronized() const
Assert IsSizeSynchronized().
static std::unordered_set< std::string > GetReservedKeys()
Get reserved keys for the map. A map cannot contain any of these keys.
std::string GetPrimaryKey() const
Returns the primary key of the TensorMap.
void pybind_tensormap(py::module &m)
static py::class_< Map, holder_type > bind_tensor_map(py::handle scope, const std::string &name, Args &&...args)
Generic file read and write utility for python interface.