MCPcopy Create free account
hub / github.com/cdgriffith/Box / merge_update

Method merge_update

box/box.py:836–887  ·  view source on GitHub ↗
(self, *args, **kwargs)

Source from the content-addressed store, hash-verified

834 self.__convert_and_store(k, kwargs[k])
835
836 def merge_update(self, *args, **kwargs):
837 merge_type = None
838 if "box_merge_lists" in kwargs:
839 merge_type = kwargs.pop("box_merge_lists")
840 force_unfrozen = kwargs.pop("_force_unfrozen", False)
841
842 was_frozen = self._box_config["frozen_box"]
843 if force_unfrozen:
844 self._box_config["frozen_box"] = False
845
846 try:
847
848 def convert_and_set(k, v):
849 intact_type = self._box_config["box_intact_types"] and isinstance(
850 v, self._box_config["box_intact_types"]
851 )
852 if isinstance(v, dict) and not intact_type:
853 # Box objects must be created in case they are already
854 # in the `converted` box_config set
855 v = self._box_config["box_class"](v, **self.__box_config(extra_namespace=k))
856 if k in self and isinstance(self[k], dict):
857 self[k].merge_update(v, box_merge_lists=merge_type, _force_unfrozen=force_unfrozen)
858 return
859 if isinstance(v, list) and not intact_type:
860 v = box.BoxList(v, **self.__box_config(extra_namespace=k))
861 if merge_type == "extend" and k in self and isinstance(self[k], list):
862 self[k].extend(v)
863 return
864 if merge_type == "unique" and k in self and isinstance(self[k], list):
865 for item in v:
866 if item not in self[k]:
867 self[k].append(item)
868 return
869 self.__setitem__(k, v)
870
871 if (len(args) + int(bool(kwargs))) > 1:
872 raise BoxTypeError(f"merge_update expected at most 1 argument, got {len(args) + int(bool(kwargs))}")
873 single_arg = next(iter(args), None)
874 if single_arg:
875 if hasattr(single_arg, "keys"):
876 for k in single_arg:
877 convert_and_set(k, single_arg[k])
878 else:
879 for k, v in single_arg:
880 convert_and_set(k, v)
881
882 for key in kwargs:
883 convert_and_set(key, kwargs[key])
884
885 finally:
886 if force_unfrozen:
887 self._box_config["frozen_box"] = was_frozen
888
889 def setdefault(self, item, default=None):
890 if item in self:

Callers 6

test_merge_updateMethod · 0.95
__iadd__Method · 0.95
__add__Method · 0.80
__radd__Method · 0.80
convert_and_setMethod · 0.80

Calls 2

BoxTypeErrorClass · 0.90
popMethod · 0.80

Tested by 2

test_merge_updateMethod · 0.76