1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-23 00:29:59 +02:00

add line_slice function

This commit is contained in:
Kelly Brazil
2024-01-05 15:05:59 -08:00
parent 3a95407161
commit c97a8fb48f
3 changed files with 106 additions and 1 deletions

View File

@ -11,6 +11,7 @@
* [convert\_to\_bool](#jc.utils.convert_to_bool)
* [convert\_size\_to\_int](#jc.utils.convert_size_to_int)
* [input\_type\_check](#jc.utils.input_type_check)
* [line\_slice](#jc.utils.line_slice)
* [timestamp](#jc.utils.timestamp)
* [\_\_init\_\_](#jc.utils.timestamp.__init__)
@ -231,6 +232,35 @@ def input_type_check(data: object) -> None
Ensure input data is a string. Raises `TypeError` if not.
<a id="jc.utils.line_slice"></a>
### line\_slice
```python
def line_slice(data: Union[str, Iterable],
slice_start: Optional[int] = None,
slice_end: Optional[int] = None) -> Union[str, Iterable]
```
Slice input data by lines - lazily, if possible.
Accepts a string (for normal parsers) or an iterable (for streaming
parsers). Uses normal start/stop slicing values, but will always slice
on lines instead of characters. Positive slices will use less memory as
the function will attempt to lazily iterate over the input. A negative
slice parameter will force the function to read in all of the data and
then slice, which will use more memory.
Parameters:
data: (string or iterable) - input to slice by lines
slice_start: (int) - starting line
slice_end: (int) - ending line
Returns:
string if input is a string.
iterable of strings if input is an iterable (for streaming parsers)
<a id="jc.utils.timestamp"></a>
### timestamp Objects