def _set_doc_offset(offset, document, value): """ :type offset: list[str] :type document: dict >>> doc = {'a': 4} >>> _set_doc_offset(['a'], doc, 5) >>> doc {'a': 5} >>> doc = {'a': {'b': 4}} >>> _set_doc_offset(['a', 'b'], doc, 'c') >>> doc {'a': {'b': 'c'}} """ read_offset = offset[:-1] sub_doc = get_doc_offset(read_offset, document) sub_doc[offset[-1]] = value
def safe_get_doc_offset(offset, document): try: return get_doc_offset(offset, document) except KeyError: return None
def extract(self, document): v = get_doc_offset(self.offset, document) if v is None: return None return self.from_string(v)
def extract(self, document): return get_doc_offset(self.offset, document)
def _unsafe_get_field(self, field): if isinstance(field, list): return get_doc_offset(field, self._doc) else: return field.extract(self._doc)