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

Class BoxList

box/box_list.py:38–547  ·  view source on GitHub ↗

Drop in replacement of list, that converts added objects to Box or BoxList objects as necessary.

Source from the content-addressed store, hash-verified

36
37
38class BoxList(list):
39 """
40 Drop in replacement of list, that converts added objects to Box or BoxList
41 objects as necessary.
42 """
43
44 def __new__(cls, *args, **kwargs):
45 obj = super().__new__(cls, *args, **kwargs)
46 # This is required for pickling to work correctly
47 obj.box_options = {"box_class": box.Box}
48 obj.box_options.update(kwargs)
49 obj.box_org_ref = None
50 return obj
51
52 def __init__(self, iterable: Iterable | None = None, box_class: type[box.Box] = box.Box, **box_options):
53 self.box_options = box_options
54 self.box_options["box_class"] = box_class
55 self.box_org_ref = iterable
56 if iterable:
57 for x in iterable:
58 self.append(x)
59 self.box_org_ref = None
60 if box_options.get("frozen_box"):
61
62 def frozen(*args, **kwargs):
63 raise BoxError("BoxList is frozen")
64
65 for method in ["append", "extend", "insert", "pop", "remove", "reverse", "sort"]:
66 self.__setattr__(method, frozen)
67
68 def __getitem__(self, item):
69 if self.box_options.get("box_dots") and isinstance(item, str) and item.startswith("["):
70 list_pos = _list_pos_re.search(item)
71 value = super().__getitem__(int(list_pos.groups()[0]))
72 if len(list_pos.group()) == len(item):
73 return value
74 return value.__getitem__(item[len(list_pos.group()) :].lstrip("."))
75 if isinstance(item, tuple):
76 result = self
77 for idx in item:
78 if isinstance(result, list):
79 result = result[idx]
80 else:
81 raise BoxTypeError(f"Cannot numpy-style indexing on {type(result).__name__}.")
82 return result
83 return super().__getitem__(item)
84
85 def __delitem__(self, key):
86 if self.box_options.get("frozen_box"):
87 raise BoxError("BoxList is frozen")
88 if self.box_options.get("box_dots") and isinstance(key, str) and key.startswith("["):
89 list_pos = _list_pos_re.search(key)
90 pos = int(list_pos.groups()[0])
91 if len(list_pos.group()) == len(key):
92 return super().__delitem__(pos)
93 if hasattr(self[pos], "__delitem__"):
94 return self[pos].__delitem__(key[len(list_pos.group()) :].lstrip(".")) # type: ignore
95 super().__delitem__(key)

Callers 15

test_box_listMethod · 0.90
test_frozen_listMethod · 0.90
test_box_list_to_jsonMethod · 0.90
test_box_list_to_yamlMethod · 0.90
test_box_list_to_tomlMethod · 0.90
test_to_csvMethod · 0.90
test_bad_csvMethod · 0.90
test_toon_stringsMethod · 0.90
test_toon_filesMethod · 0.90
test_box_list_dotsMethod · 0.90

Calls

no outgoing calls

Tested by 15

test_box_listMethod · 0.72
test_frozen_listMethod · 0.72
test_box_list_to_jsonMethod · 0.72
test_box_list_to_yamlMethod · 0.72
test_box_list_to_tomlMethod · 0.72
test_to_csvMethod · 0.72
test_bad_csvMethod · 0.72
test_toon_stringsMethod · 0.72
test_toon_filesMethod · 0.72
test_box_list_dotsMethod · 0.72