@@ -7,28 +7,31 @@ kernelspec:
77# Basic Usage
88
99``` {code-cell}
10- from compressed_lists import CompressedIntegerList, CompressedStringList
10+ from compressed_lists import CompressedIntegerList, CompressedStringList, Partitioning
1111
1212# Create a CompressedIntegerList
1313int_data = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
1414names = ["A", "B", "C"]
1515int_list = CompressedIntegerList.from_list(int_data, names)
1616
1717# Access elements
18- print(int_list[0]) # [1, 2, 3]
19- print(int_list["B"]) # [4, 5]
20- print(int_list[1:3]) # Slice of elements
18+ print(int_list[0])
19+ print(int_list["B"])
20+ print(int_list[1:3])
2121
2222# Apply a function to each element
2323squared = int_list.lapply(lambda x: [i**2 for i in x])
24- print(squared[0]) # [1, 4, 9]
24+ print(squared[0])
2525
2626# Convert to a regular Python list
2727regular_list = int_list.to_list()
2828
29- # Create a CompressedStringList
30- char_data = [["apple", "banana"], ["cherry", "date", "elderberry"], ["fig"]]
31- char_list = CompressedStringList.from_list(char_data)
29+ # Create a CompressedStringList from lengths
30+ import biocutils as ut
31+ char_data = ut.StringList(["apple", "banana", "cherry", "date", "elderberry", "fig"])
32+
33+ char_list = CompressedStringList(char_data, partitioning=Partitioning.from_lengths([2,3,1]))
34+ print(char_list)
3235```
3336
3437## Partitioning
@@ -57,7 +60,7 @@ print(start, end)
5760Create a new class that inherits from ` CompressedList ` with appropriate type annotations:
5861
5962``` python
60- from typing import List, TypeVar, Generic
63+ from typing import List
6164from compressed_lists import CompressedList, Partitioning
6265import numpy as np
6366
@@ -72,31 +75,32 @@ The constructor should initialize the superclass with the appropriate data:
7275
7376``` python
7477def __init__ (self ,
75- unlist_data : Any, # Replace with your data type
76- partitioning : Partitioning,
77- element_metadata : dict = None ,
78- metadata : dict = None ):
78+ unlist_data : Any, # Replace with your data type
79+ partitioning : Partitioning,
80+ element_type : Any = None ,
81+ element_metadata : Optional[dict ] = None ,
82+ metadata : Optional[dict ] = None ):
7983 super ().__init__ (unlist_data, partitioning,
80- element_type = " custom_type" , # Set your element type
81- element_metadata = element_metadata,
82- metadata = metadata)
84+ element_type = " custom_type" , # Set your element type
85+ element_metadata = element_metadata,
86+ metadata = metadata)
8387```
8488
85- ## 3. Implement _ extract_range Method
89+ ## 3. Implement ` extract_range ` Method
8690
8791This method defines how to extract a range of elements from your unlisted data:
8892
8993``` python
90- def _extract_range (self , start : int , end : int ) -> List[T]:
94+ def extract_range (self , start : int , end : int ) -> List[T]:
9195 """ Extract a range from unlisted data."""
9296 # For example, with numpy arrays:
93- return self .unlist_data[start:end].tolist()
97+ return self .unlist_data[start:end]
9498
9599 # Or for other data types:
96- # return self.unlist_data[start:end]
100+ # return self.unlist_data[start:end, : ]
97101```
98102
99- ## 4. Implement from_list Class Method
103+ ## 4. Implement ` from_list ` Class Method
100104
101105This factory method creates a new instance from a list:
102106
@@ -140,7 +144,7 @@ class CompressedFloatList(CompressedList):
140144 element_metadata=element_metadata,
141145 metadata=metadata)
142146
143- def _extract_range (self, start: int, end: int) -> List[float]:
147+ def extract_range (self, start: int, end: int) -> List[float]:
144148 return self.unlist_data[start:end].tolist()
145149
146150 @classmethod
@@ -176,14 +180,16 @@ class MyObject:
176180 def __init__ (self , value ):
177181 self .value = value
178182
179- class CompressedMyObjectList (CompressedList[List[MyObject]] ):
183+ class CompressedMyObjectList (CompressedList ):
180184 # Implementation details...
181185
182- def _extract_range (self , start : int , end : int ) -> List[MyObject]:
186+ def extract_range (self , start : int , end : int ) -> List[MyObject]:
183187 return self .unlist_data[start:end]
184188
185189 @ classmethod
186190 def from_list (cls , lst : List[List[MyObject]], ...):
187191 # Custom flattening and storage logic
188192 # ...
189193```
194+
195+ Check out the ` CompressedBiocFrameList ` for a complete example of this usecase.
0 commit comments