def _attach(self, cpp_obj, param_name, types): """Attach type parameter to a C++ object with per type data. Args: cpp_obj: A pybind11 wrapped C++ object to set and get the type parameters from. param_name (str): A snake case parameter name (handled automatically by ``TypeParameter``) that when changed to camel case prefixed by get or set is the str name for the pybind11 exported getter and setter. types (list[str]): The str names of the available types for the type parameter. """ # store info to communicate with c++ self._setter = "set" + _to_camel_case(param_name) self._getter = "get" + _to_camel_case(param_name) self._indexer.valid_types = types # add all types to c++ parameters = { key: _to_base(self._dict.get(key, self.default)) for key in self._indexer.yield_all_keys() } self._cpp_obj = cpp_obj for key in self: try: _raise_if_required_arg(parameters[key]) except IncompleteSpecificationError as err: self._cpp_obj = None raise IncompleteSpecificationError(f"for key {key} {str(err)}") self._single_setitem(key, parameters[key])
def _write(self, obj): if not self._attached: return # the _dict attribute is the store for the Python copy of the data. parent = obj._parent with parent._suspend_read: self._single_setitem(obj._identity, _to_base(obj._parent))
def to_base(self): """Return a plain dictionary with equivalent data. This recursively convert internal data to base HOOMD types. """ # Using _dict prevents unnecessary _read calls for # _HOOMDSyncedCollection objects. return {key: _to_base(value) for key, value in self._dict.items()}
def _cpp_setting(self, key, value): """Handles setting a new value to C++. Assumes value is completely updated and validated. """ setter = self._setters.get(key) if setter is not None: setter(self, key, value) return if hasattr(value, "_cpp_obj"): setattr(self._cpp_obj, key, value._cpp_obj) return if isinstance(value, _HOOMDSyncedCollection): with value._suspend_read_and_write: setattr(self._cpp_obj, key, _to_base(value)) return setattr(self._cpp_obj, key, _to_base(value))
def to_base(self): """Convert to a `dict`.""" if not self._attached: return {k: _to_base(v) for k, v in self._dict.items()} return {key: self[key] for key in self}