Skip to content

Commit ce04265

Browse files
committed
Support empty compressed list objects of size n
1 parent a5132c3 commit ce04265

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Changelog
22

3-
## Version 0.4.0
3+
## Version 0.4.0 - 0.4.1
44

55
- Classes extend `BiocObject` from biocutils. `metadata` is a named list.
66
- Update actions to run from 3.10-3.14
7+
- Support empty compressed list objects of size `n`.
78

89
## Version 0.3.0
910

src/compressed_lists/base.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,14 @@ def __getitem__(self, key: Union[int, str, slice]) -> Any:
387387

388388
current_class_const = type(self)
389389
return current_class_const.from_list(
390-
result, names=[self.names[i] for i in indices] if self.names[0] is not None else None
390+
result, names=[self.names[i] for i in indices] if self.names is not None else None
391391
)
392392

393393
else:
394-
raise TypeError("'key' must be int, str, or slice.")
394+
try:
395+
return self.extract_subset(indices=key)
396+
except Exception as e:
397+
raise TypeError("'key' must be int, str, slice or list of indices.") from e
395398

396399
##################################
397400
######>> abstract methods <<######
@@ -575,3 +578,19 @@ def lapply(self, func: Callable) -> CompressedList:
575578

576579
current_class_const = type(self)
577580
return current_class_const.from_list(result, self.names, self._metadata)
581+
582+
@classmethod
583+
def empty(cls, n: int):
584+
"""Create an zero-length `CompressedGenomicRangesList` object.
585+
586+
Args:
587+
n:
588+
Number of elements.
589+
590+
Returns:
591+
same type as caller, in this case a `CompressedGenomicRangesList`.
592+
"""
593+
594+
_range_lengths = [0] * n
595+
596+
return CompressedList(unlist_data=[], partitioning=Partitioning(ends=_range_lengths))

tests/test_base.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ def test_base_list_of_empty():
3939
assert list(cl_list_empty[2]) == []
4040

4141

42+
def test_base_list_empty_classmeth():
43+
cl_list_empty = CompressedList.empty(n=3)
44+
assert len(cl_list_empty) == 3
45+
assert len(cl_list_empty.unlist_data) == 0
46+
assert list(cl_list_empty.get_element_lengths()) == [0, 0, 0]
47+
assert list(cl_list_empty[0]) == []
48+
assert list(cl_list_empty[1]) == []
49+
assert list(cl_list_empty[2]) == []
50+
51+
subset = cl_list_empty[[0, 2]]
52+
assert isinstance(subset, CompressedList)
53+
assert len(subset) == 2
54+
4255
def test_base_set_names(base_list):
4356
new_names = ["X", "Y", "Z"]
4457

0 commit comments

Comments
 (0)