def __convert_and_store(B, item, value): C = item A = value if B._box_config[_E]: D = B._safe_attr(C) B._box_config[_C][D] = C if isinstance(A, (int, float, str, bytes, bytearray, bool, complex, set, frozenset)): return super().__setitem__(C, A) if B._box_config[_L] and isinstance(A, B._box_config[_L]): return super().__setitem__(C, A) if isinstance(A, dict) and not isinstance(A, Box): A = B.__class__(A, **B.__box_config()) elif isinstance(A, list) and not isinstance(A, box.BoxList): if B._box_config[_D]: A = _recursive_tuples(A, B.__class__, recreate_tuples=B._box_config[_V], **B.__box_config()) else: A = box.BoxList(A, box_class=B.__class__, **B.__box_config()) elif B._box_config[_V] and isinstance(A, tuple): A = _recursive_tuples(A, B.__class__, recreate_tuples=_G, **B.__box_config()) super().__setitem__(C, A)
def setdefault(B, item, default=_A): C = item A = default if C in B: return B[C] if isinstance(A, dict): A = B.__class__(A, **B.__box_config()) if isinstance(A, list): A = box.BoxList(A, box_class=B.__class__, **B.__box_config()) B[C] = A return A
def setdefault(self, item, default=None): if item in self: return self[item] if isinstance(default, dict): default = self.__class__(default, **self.__box_config()) if isinstance(default, list): default = box.BoxList(default, box_class=self.__class__, **self.__box_config()) self[item] = default return default
def __get_default(B, item): A = B._box_config[_X] if A in (B.__class__, dict): C = B.__class__(**B.__box_config()) elif isinstance(A, dict): C = B.__class__(**B.__box_config(), **A) elif isinstance(A, list): C = box.BoxList(**B.__box_config()) elif isinstance(A, Callable): C = A() elif hasattr(A, 'copy'): C = A.copy() else: C = A B.__convert_and_store(item, C) return C
def D(k, v): B = A._box_config[_L] and isinstance(v, A._box_config[_L]) if isinstance(v, dict) and not B: v = A.__class__(v, **A.__box_config()) if k in A and isinstance(A[k], dict): if isinstance(A[k], Box): A[k].merge_update(v) else: A[k].update(v) return if isinstance(v, list) and not B: v = box.BoxList(v, **A.__box_config()) A.__setitem__(k, v)
def get(self, key, default=NO_DEFAULT): if key not in self: if default is NO_DEFAULT: if self._box_config['default_box'] and self._box_config['default_box_none_transform']: return self.__get_default(key) else: return None if isinstance(default, dict) and not isinstance(default, Box): return Box(default, box_settings=self._box_config.get("box_settings")) if isinstance(default, list) and not isinstance(default, box.BoxList): return box.BoxList(default) return default return self[key]
def get(B, key, default=NO_DEFAULT): C = key A = default if C not in B: if A is NO_DEFAULT: if B._box_config[_K] and B._box_config[_T]: return B.__get_default(C) else: return _A if isinstance(A, dict) and not isinstance(A, Box): return Box(A, box_settings=B._box_config.get(_Y)) if isinstance(A, list) and not isinstance(A, box.BoxList): return box.BoxList(A) return A return B[C]
def convert_and_set(k, v): intact_type = (self._box_config['box_intact_types'] and isinstance(v, self._box_config['box_intact_types'])) if isinstance(v, dict) and not intact_type: # Box objects must be created in case they are already # in the `converted` box_config set v = self.__class__(v, **self.__box_config()) if k in self and isinstance(self[k], dict): if isinstance(self[k], Box): self[k].merge_update(v) else: self[k].update(v) return if isinstance(v, list) and not intact_type: v = box.BoxList(v, **self.__box_config()) self.__setitem__(k, v)
def __get_default(self, item): default_value = self._box_config['default_box_attr'] if default_value in (self.__class__, dict): value = self.__class__(**self.__box_config()) elif isinstance(default_value, dict): value = self.__class__(**self.__box_config(), **default_value) elif isinstance(default_value, list): value = box.BoxList(**self.__box_config()) elif isinstance(default_value, Callable): value = default_value() elif hasattr(default_value, 'copy'): value = default_value.copy() else: value = default_value self.__convert_and_store(item, value) return value
def __convert_and_store(self, item, value): if self._box_config['conversion_box']: safe_key = self._safe_attr(item) self._box_config['__safe_keys'][safe_key] = item if isinstance(value, (int, float, str, bytes, bytearray, bool, complex, set, frozenset)): return super().__setitem__(item, value) # If the value has already been converted or should not be converted, return it as-is if self._box_config['box_intact_types'] and isinstance(value, self._box_config['box_intact_types']): return super().__setitem__(item, value) # This is the magic sauce that makes sub dictionaries into new box objects if isinstance(value, dict) and not isinstance(value, Box): value = self.__class__(value, **self.__box_config()) elif isinstance(value, list) and not isinstance(value, box.BoxList): if self._box_config['frozen_box']: value = _recursive_tuples(value, self.__class__, recreate_tuples=self._box_config['modify_tuples_box'], **self.__box_config()) else: value = box.BoxList(value, box_class=self.__class__, **self.__box_config()) elif self._box_config['modify_tuples_box'] and isinstance(value, tuple): value = _recursive_tuples(value, self.__class__, recreate_tuples=True, **self.__box_config()) super().__setitem__(item, value)