1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2026-06-19 22:28:17 +02:00

refactor to use get_parser()

This commit is contained in:
Kelly Brazil
2024-01-06 18:23:46 -08:00
parent 973c535c72
commit 8e9ff7fa9f
7 changed files with 90 additions and 63 deletions
+17 -6
View File
@@ -23,10 +23,11 @@ jc - JSON Convert lib module
### get\_parser
```python
def get_parser(parser_mod_name) -> ModuleType
def get_parser(parser_mod_name: Union[str, ModuleType]) -> ModuleType
```
Return the parser module object
Return the parser module object and check that the module is a valid
parser module.
Parameters:
@@ -81,15 +82,25 @@ To get a list of available parser module names, use `parser_mod_list()`.
Alternatively, a parser module object can be supplied:
>>> import jc
>>> import jc.parsers.date as jc_date
>>> jc_date = jc.get_parser('date')
>>> date_obj = jc.parse(jc_date, 'Tue Jan 18 10:23:07 PST 2022')
>>> print(f'The year is: {date_obj["year"]}')
The year is: 2022
You can also use the lower-level parser modules directly:
You can also use the parser modules directly via `get_parser()`:
>>> import jc
>>> jc_date = jc.get_parser('date')
>>> date_obj = jc_date.parse('Tue Jan 18 10:23:07 PST 2022')
>>> print(f'The year is: {date_obj["year"]}')
The year is: 2022
Finally, you can access the low-level parser modules manually:
>>> import jc.parsers.date
>>> jc.parsers.date.parse('Tue Jan 18 10:23:07 PST 2022')
>>> date_obj = jc.parsers.date.parse('Tue Jan 18 10:23:07 PST 2022')
>>> print(f'The year is: {date_obj["year"]}')
The year is: 2022
Though, accessing plugin parsers directly is a bit more cumbersome, so
this higher-level API is recommended. Here is how you can access plugin
@@ -112,7 +123,7 @@ Parameters:
variants of the module name.
A Module object can also be passed
directly or via _get_parser()
directly or via get_parser()
data: (string or data to parse (string or bytes for
bytes or standard parsers, iterable of
+5 -3
View File
@@ -237,9 +237,11 @@ Ensure input data is a string. Raises `TypeError` if not.
### line\_slice
```python
def line_slice(data: Union[str, Iterable],
slice_start: Optional[int] = None,
slice_end: Optional[int] = None) -> Union[str, Iterable]
def line_slice(
data: Union[str, Iterable[str], TextIO, bytes, None],
slice_start: Optional[int] = None,
slice_end: Optional[int] = None
) -> Union[str, Iterable[str], TextIO, bytes, None]
```
Slice input data by lines - lazily, if possible.