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

Method __getitem__

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

Source from the content-addressed store, hash-verified

601 super().__setitem__(item, value)
602
603 def __getitem__(self, item, _ignore_default=False):
604 try:
605 return super().__getitem__(item)
606 except KeyError as err:
607 if item == "_box_config":
608 cause = _exception_cause(err)
609 raise BoxKeyError("_box_config should only exist as an attribute and is never defaulted") from cause
610 if isinstance(item, slice):
611 # In Python 3.12 this changes to a KeyError instead of TypeError
612 new_box = self._box_config["box_class"](**self.__box_config())
613 for x in list(super().keys())[item.start : item.stop : item.step]:
614 new_box[x] = self[x]
615 return new_box
616 if self.__process_dotted_key(item):
617 try:
618 first_item, children = _parse_box_dots(self, item)
619 except BoxError:
620 if self._box_config["default_box"] and not _ignore_default:
621 return self.__get_default(item)
622 raise BoxKeyError(str(item)) from _exception_cause(err)
623 if first_item in self.keys():
624 if hasattr(self[first_item], "__getitem__"):
625 return self[first_item][children]
626 if self._box_config["camel_killer_box"] and isinstance(item, str):
627 converted = _camel_killer(item)
628 if converted in self.keys():
629 return super().__getitem__(converted)
630 if self._box_config["default_box"] and not _ignore_default:
631 return self.__get_default(item)
632 raise BoxKeyError(str(err)) from _exception_cause(err)
633 except TypeError as err:
634 if isinstance(item, slice):
635 new_box = self._box_config["box_class"](**self.__box_config())
636 for x in list(super().keys())[item.start : item.stop : item.step]:
637 new_box[x] = self[x]
638 return new_box
639 raise BoxTypeError(str(err)) from _exception_cause(err)
640
641 def __getattr__(self, item):
642 try:

Callers 1

__getattr__Method · 0.95

Calls 9

__box_configMethod · 0.95
keysMethod · 0.95
__process_dotted_keyMethod · 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

no test coverage detected