1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00
Files
jc/docs/streaming.md

116 lines
2.9 KiB
Markdown
Raw Normal View History

2022-02-07 06:29:17 -08:00
# Table of Contents
* [jc.streaming](#jc.streaming)
* [streaming\_input\_type\_check](#jc.streaming.streaming_input_type_check)
* [streaming\_line\_input\_type\_check](#jc.streaming.streaming_line_input_type_check)
* [stream\_success](#jc.streaming.stream_success)
* [stream\_error](#jc.streaming.stream_error)
* [add\_jc\_meta](#jc.streaming.add_jc_meta)
* [raise\_or\_yield](#jc.streaming.raise_or_yield)
<a id="jc.streaming"></a>
# jc.streaming
2022-03-04 13:27:39 -08:00
jc - JSON Convert streaming utils
2022-02-07 06:29:17 -08:00
<a id="jc.streaming.streaming_input_type_check"></a>
2022-03-05 12:15:14 -08:00
### streaming\_input\_type\_check
2022-02-07 06:29:17 -08:00
```python
def streaming_input_type_check(data: Iterable) -> 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>
2022-03-05 12:15:14 -08:00
### streaming\_line\_input\_type\_check
2022-02-07 06:29:17 -08:00
```python
def streaming_line_input_type_check(line: str) -> None
```
Ensure each line is a string. Raises `TypeError` if not.
<a id="jc.streaming.stream_success"></a>
2022-03-05 12:15:14 -08:00
### stream\_success
2022-02-07 06:29:17 -08:00
```python
def stream_success(output_line: Dict, ignore_exceptions: bool) -> Dict
```
Add `_jc_meta` object to output line if `ignore_exceptions=True`
<a id="jc.streaming.stream_error"></a>
2022-03-05 12:15:14 -08:00
### stream\_error
2022-02-07 06:29:17 -08:00
```python
def stream_error(e: BaseException, line: str) -> Dict
```
Return an error `_jc_meta` field.
<a id="jc.streaming.add_jc_meta"></a>
2022-03-05 12:15:14 -08:00
### add\_jc\_meta
2022-02-07 06:29:17 -08:00
```python
def add_jc_meta(func)
```
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>
2022-03-05 12:15:14 -08: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,
line: str) -> tuple
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.