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

Method __getitem__

box/box.py:781–820  ·  view source on GitHub ↗
(self, item, _ignore_default=False)

Source from the content-addressed store, hash-verified

779 super().__setitem__(item, value)
780
781 def __getitem__(self, item, _ignore_default=False):
782 try:
783 return super().__getitem__(item)
784 except KeyError as err:
785 if item == "_box_config":
786 cause = _exception_cause(err)
787 raise BoxKeyError("_box_config should only exist as an attribute and is never defaulted") from cause
788 if isinstance(item, slice):
789 # In Python 3.12 this changes to a KeyError instead of TypeError
790 new_box = self._box_config["box_class"](**self.__box_config())
791 for x in list(super().keys())[item.start : item.stop : item.step]:
792 new_box[x] = self[x]
793 return new_box
794 if self.__process_dotted_key(item):
795 first_item, children = _parse_box_dots(self, item)
796 first_item = self.__dots_int_key(first_item)
797 if first_item not in self.keys() and self.__camel_destructive():
798 first_item = _camel_killer(first_item)
799 if first_item in self.keys():
800 child = self[first_item]
801 if isinstance(child, Box):
802 children = child.__dots_int_key(children)
803 if hasattr(child, "__getitem__"):
804 if _ignore_default and isinstance(child, (Box, box.BoxList, _BoxTuple)):
805 return child.__getitem__(children, _ignore_default=True)
806 return child[children]
807 if self.__camel_destructive() and isinstance(item, str):
808 converted = _camel_killer(item)
809 if converted in self.keys():
810 return super().__getitem__(converted)
811 if self._box_config["default_box"] and not _ignore_default:
812 return self.__get_default(item)
813 raise BoxKeyError(str(err)) from _exception_cause(err)
814 except TypeError as err:
815 if isinstance(item, slice): # pragma: no cover (Python 3.11, slices became hashable in 3.12)
816 new_box = self._box_config["box_class"](**self.__box_config())
817 for x in list(super().keys())[item.start : item.stop : item.step]:
818 new_box[x] = self[x]
819 return new_box
820 raise BoxTypeError(str(err)) from _exception_cause(err)
821
822 def __getattr__(self, item):
823 try:

Callers 10

__getattr__Method · 0.95
__delitem__Method · 0.95
popMethod · 0.95
apply_commentsFunction · 0.45
commented_treeFunction · 0.45
__getitem__Method · 0.45
_comment_ownerMethod · 0.45
__contains__Method · 0.45
_comment_ownerMethod · 0.45

Calls 11

__box_configMethod · 0.95
keysMethod · 0.95
__process_dotted_keyMethod · 0.95
__dots_int_keyMethod · 0.95
__camel_destructiveMethod · 0.95
__get_defaultMethod · 0.95
BoxKeyErrorClass · 0.90
BoxTypeErrorClass · 0.90
_exception_causeFunction · 0.85
_parse_box_dotsFunction · 0.85
_camel_killerFunction · 0.85

Tested by 1