2024-03-14 21:44:37 -07:00
|
|
|
[Home](https://kellyjonbrazil.github.io/jc/)
|
2022-02-07 06:29:17 -08:00
|
|
|
<a id="jc.streaming"></a>
|
|
|
|
|
|
|
|
# jc.streaming
|
|
|
|
|
2024-03-14 21:58:43 -07:00
|
|
|
## Table of Contents
|
2022-02-07 06:29:17 -08:00
|
|
|
|
2024-03-14 21:44:37 -07:00
|
|
|
*[jc.streaming](#jc.streaming)
|
|
|
|
*[add_jc_meta](#jc.streaming.add_jc_meta)
|
|
|
|
*[raise_or_yield](#jc.streaming.raise_or_yield)
|
|
|
|
*[stream_error](#jc.streaming.stream_error)
|
|
|
|
*[stream_success](#jc.streaming.stream_success)
|
|
|
|
*[streaming_input_type_check](#jc.streaming.streaming_input_type_check)
|
|
|
|
*[streaming_line_input_type_check](#jc.streaming.streaming_line_input_type_check)
|
2022-02-07 06:29:17 -08:00
|
|
|
|
2024-03-14 21:44:37 -07:00
|
|
|
jc - JSON Convert streaming utils
|
2022-02-07 06:29:17 -08:00
|
|
|
|
|
|
|
<a id="jc.streaming.add_jc_meta"></a>
|
|
|
|
|
2024-03-14 21:44:37 -07:00
|
|
|
### add_jc_meta
|
2022-02-07 06:29:17 -08:00
|
|
|
|
|
|
|
```python
|
2024-03-14 21:44:37 -07:00
|
|
|
def add_jc_meta(func: ~F) -> ~F
|
2022-02-07 06:29:17 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
Decorator for streaming parsers to add stream_success and stream_error
|
|
|
|
objects. This simplifies the yield lines in the streaming parsers.
|
|
|
|
|
|
|
|
With the decorator on parse():
|
|
|
|
|
|
|
|
# successfully parsed line:
|
|
|
|
yield output_line if raw else _process(output_line)
|
|
|
|
|
|
|
|
# unsuccessfully parsed line:
|
|
|
|
except Exception as e:
|
|
|
|
yield raise_or_yield(ignore_exceptions, e, line)
|
|
|
|
|
|
|
|
Without the decorator on parse():
|
|
|
|
|
|
|
|
# successfully parsed line:
|
|
|
|
if raw:
|
|
|
|
yield stream_success(output_line, ignore_exceptions)
|
|
|
|
else:
|
|
|
|
stream_success(_process(output_line), ignore_exceptions)
|
|
|
|
|
|
|
|
# unsuccessfully parsed line:
|
|
|
|
except Exception as e:
|
|
|
|
yield stream_error(raise_or_yield(ignore_exceptions, e, line))
|
|
|
|
|
|
|
|
In all cases above:
|
|
|
|
|
|
|
|
output_line: (Dict) successfully parsed line yielded as a dict
|
|
|
|
|
|
|
|
e: (BaseException) exception object as the first value
|
|
|
|
of the tuple if the line was not successfully parsed.
|
|
|
|
|
|
|
|
line: (str) string of the original line that did not
|
|
|
|
successfully parse.
|
|
|
|
|
|
|
|
ignore_exceptions: (bool) continue processing lines and ignore
|
|
|
|
exceptions if True.
|
|
|
|
|
|
|
|
<a id="jc.streaming.raise_or_yield"></a>
|
|
|
|
|
2024-03-14 21:44:37 -07:00
|
|
|
### raise_or_yield
|
2022-02-07 06:29:17 -08:00
|
|
|
|
|
|
|
```python
|
2022-03-04 13:27:39 -08:00
|
|
|
def raise_or_yield(ignore_exceptions: bool, e: BaseException,
|
2022-10-19 08:33:09 -07:00
|
|
|
line: str) -> Tuple[BaseException, str]
|
2022-02-07 06:29:17 -08:00
|
|
|
```
|
|
|
|
|
|
|
|
Return the exception object and line string if ignore_exceptions is
|
|
|
|
True. Otherwise, re-raise the exception from the exception object with
|
|
|
|
an annotation.
|
|
|
|
|
2024-03-14 21:44:37 -07:00
|
|
|
<a id="jc.streaming.stream_error"></a>
|
|
|
|
|
|
|
|
### stream_error
|
|
|
|
|
|
|
|
```python
|
|
|
|
def stream_error(e: BaseException, line: str) -> Dict[str, Any]
|
|
|
|
```
|
|
|
|
|
|
|
|
Return an error `_jc_meta` field.
|
|
|
|
|
|
|
|
<a id="jc.streaming.stream_success"></a>
|
|
|
|
|
|
|
|
### stream_success
|
|
|
|
|
|
|
|
```python
|
|
|
|
def stream_success(output_line: Dict[str, Any],
|
|
|
|
ignore_exceptions: bool) -> Dict[str, Any]
|
|
|
|
```
|
|
|
|
|
|
|
|
Add `_jc_meta` object to output line if `ignore_exceptions=True`
|
|
|
|
|
|
|
|
<a id="jc.streaming.streaming_input_type_check"></a>
|
|
|
|
|
|
|
|
### streaming_input_type_check
|
|
|
|
|
|
|
|
```python
|
|
|
|
def streaming_input_type_check(data: Iterable[Union[str, bytes]]) -> None
|
|
|
|
```
|
|
|
|
|
|
|
|
Ensure input data is an iterable, but not a string or bytes. Raises
|
|
|
|
`TypeError` if not.
|
|
|
|
|
|
|
|
<a id="jc.streaming.streaming_line_input_type_check"></a>
|
|
|
|
|
|
|
|
### streaming_line_input_type_check
|
|
|
|
|
|
|
|
```python
|
|
|
|
def streaming_line_input_type_check(line: str) -> None
|
|
|
|
```
|
|
|
|
|
|
|
|
Ensure each line is a string. Raises `TypeError` if not.
|
|
|
|
|
|
|
|
|