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

Method __get_default

box/box.py:503–552  ·  view source on GitHub ↗
(self, item, attr=False)

Source from the content-addressed store, hash-verified

501 return False
502
503 def __get_default(self, item, attr=False):
504 if item in ("getdoc", "shape") and _is_ipython():
505 return None
506 default_value = self._box_config["default_box_attr"]
507 if default_value in (self._box_config["box_class"], dict):
508 value = self._box_config["box_class"](**self.__box_config(extra_namespace=item))
509 elif isinstance(default_value, dict):
510 value = self._box_config["box_class"](**self.__box_config(extra_namespace=item), **default_value)
511 elif isinstance(default_value, list):
512 value = box.BoxList(**self.__box_config(extra_namespace=item))
513 elif isinstance(default_value, Callable):
514 args = []
515 kwargs = {}
516 p_sigs = [
517 p.name
518 for p in signature(default_value).parameters.values()
519 if p.kind in (p.POSITIONAL_ONLY, p.POSITIONAL_OR_KEYWORD)
520 ]
521 k_sigs = [p.name for p in signature(default_value).parameters.values() if p.kind is p.KEYWORD_ONLY]
522 for name in p_sigs:
523 if name not in ("key", "box_instance"):
524 raise BoxError("default_box_attr can only have the arguments 'key' and 'box_instance'")
525 if "key" in p_sigs:
526 args.append(item)
527 if "box_instance" in p_sigs:
528 args.insert(p_sigs.index("box_instance"), self)
529 if "key" in k_sigs:
530 kwargs["key"] = item
531 if "box_instance" in k_sigs:
532 kwargs["box_instance"] = self
533 value = default_value(*args, **kwargs)
534 elif hasattr(default_value, "copy"):
535 value = default_value.copy()
536 else:
537 value = default_value
538 if self._box_config["default_box_create_on_get"]:
539 if not attr or not (item.startswith("_") and item.endswith("_")):
540 if self.__process_dotted_key(item):
541 first_item, children = _parse_box_dots(self, item, setting=True)
542 if first_item in self.keys():
543 if hasattr(self[first_item], "__setitem__"):
544 self[first_item].__setitem__(children, value)
545 else:
546 super().__setitem__(
547 first_item, self._box_config["box_class"](**self.__box_config(extra_namespace=first_item))
548 )
549 self[first_item].__setitem__(children, value)
550 else:
551 super().__setitem__(item, value)
552 return value
553
554 def __box_config(self, extra_namespace: Any = NO_NAMESPACE) -> dict:
555 out = {}

Callers 3

getMethod · 0.95
__getitem__Method · 0.95
__getattr__Method · 0.95

Calls 10

__box_configMethod · 0.95
__process_dotted_keyMethod · 0.95
keysMethod · 0.95
BoxErrorClass · 0.90
_is_ipythonFunction · 0.85
_parse_box_dotsFunction · 0.85
appendMethod · 0.80
insertMethod · 0.80
copyMethod · 0.45
__setitem__Method · 0.45

Tested by

no test coverage detected