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

325 lines
7.7 KiB
Markdown
Raw Normal View History

2024-03-14 21:44:37 -07:00
[Home](https://kellyjonbrazil.github.io/jc/)
2022-01-25 17:07:47 -08:00
<a id="jc.utils"></a>
# jc.utils
2020-07-30 16:20:24 -07:00
2024-03-14 21:58:43 -07:00
## Table of Contents
2022-05-23 13:58:24 -07:00
2024-03-16 18:28:52 -07:00
* [jc.utils](#jc.utils)
* [compatibility](#jc.utils.compatibility)
* [convert_size_to_int](#jc.utils.convert_size_to_int)
* [convert_to_bool](#jc.utils.convert_to_bool)
* [convert_to_float](#jc.utils.convert_to_float)
* [convert_to_int](#jc.utils.convert_to_int)
* [error_message](#jc.utils.error_message)
* [has_data](#jc.utils.has_data)
* [input_type_check](#jc.utils.input_type_check)
* [is_compatible](#jc.utils.is_compatible)
* [line_slice](#jc.utils.line_slice)
* [normalize_key](#jc.utils.normalize_key)
* [remove_quotes](#jc.utils.remove_quotes)
* [warning_message](#jc.utils.warning_message)
2022-05-23 13:58:24 -07:00
2024-03-14 21:44:37 -07:00
jc - JSON Convert utils
2022-05-23 13:58:24 -07:00
2022-01-25 17:07:47 -08:00
<a id="jc.utils.compatibility"></a>
2022-03-05 12:15:14 -08:00
### compatibility
2020-07-30 16:20:24 -07:00
2019-11-11 18:30:46 -08:00
```python
2022-03-04 13:27:39 -08:00
def compatibility(mod_name: str,
2022-10-15 14:36:35 -07:00
compatible: List[str],
2022-03-04 13:27:39 -08:00
quiet: bool = False) -> None
2019-11-11 18:30:46 -08:00
```
2022-01-19 11:08:59 -08:00
2022-05-23 13:58:24 -07:00
Checks for the parser's compatibility with the running OS platform and
2022-06-16 18:00:40 -07:00
prints a warning message to `STDERR` if not compatible and
`quiet=False.`
2019-11-11 18:30:46 -08:00
2022-01-25 18:03:34 -08:00
Parameters:
2019-11-12 07:08:35 -08:00
2022-01-25 18:03:34 -08:00
mod_name: (string) __name__ of the calling module
2019-11-11 18:30:46 -08:00
2022-01-25 18:03:34 -08:00
compatible: (list) sys.platform name(s) compatible with
the parser. compatible options:
linux, darwin, cygwin, win32, aix, freebsd
2021-12-01 16:12:51 -08:00
quiet: (bool) suppress compatibility message if True
2022-01-25 18:03:34 -08:00
Returns:
None - just prints output to STDERR
2019-11-12 14:20:59 -08:00
2024-03-14 21:44:37 -07:00
<a id="jc.utils.convert_size_to_int"></a>
2019-11-12 14:20:59 -08:00
2024-03-14 21:44:37 -07:00
### convert_size_to_int
2020-07-30 16:20:24 -07:00
2020-06-14 17:23:10 -07:00
```python
2024-09-22 20:01:40 -07:00
def convert_size_to_int(size: str,
binary: bool = False,
posix_mode: bool = False,
decimal_bias: bool = False) -> Optional[int]
2020-06-14 17:23:10 -07:00
```
2024-03-14 21:44:37 -07:00
Parse a human readable data size and return the number of bytes.
2020-06-14 17:23:10 -07:00
2022-01-25 18:03:34 -08:00
Parameters:
2020-06-14 17:23:10 -07:00
2024-03-14 21:44:37 -07:00
size: (string) The human readable file size to parse.
binary: (boolean) `True` to use binary multiples of bytes
(base-2) for ambiguous unit symbols and names,
`False` to use decimal multiples of bytes (base-10).
2024-09-22 20:01:40 -07:00
posix_mode: Treat one-letter units (k, m, g, etc.) as binary.
decimal_bias: (boolean) `True` to treat slightly ambiguous two-
letter unit symbols ending in "i" (e.g. Ki, Gi) to
use decimal multiples of bytes (base-10). `False`
(default) to use binary multiples of bytes.
2022-01-25 18:03:34 -08:00
Returns:
2020-06-14 17:23:10 -07:00
2024-03-14 21:44:37 -07:00
integer/None Integer if successful conversion, otherwise None
2020-06-14 17:23:10 -07:00
2024-03-14 21:44:37 -07:00
This function knows how to parse sizes in bytes, kilobytes, megabytes,
gigabytes, terabytes and petabytes. Some examples:
>>> convert_size_to_int('42')
42
>>> convert_size_to_int('13b')
13
>>> convert_size_to_int('5 bytes')
5
>>> convert_size_to_int('1 KB')
1000
>>> convert_size_to_int('1 kilobyte')
1000
>>> convert_size_to_int('1 KiB')
1024
2024-09-22 20:01:40 -07:00
>>> convert_size_to_int('1 Ki')
1024
>>> convert_size_to_int('1 Ki', decimal_bias=True)
1000
2024-03-14 21:44:37 -07:00
>>> convert_size_to_int('1 KB', binary=True)
1024
>>> convert_size_to_int('1.5 GB')
1500000000
>>> convert_size_to_int('1.5 GB', binary=True)
1610612736
<a id="jc.utils.convert_to_bool"></a>
2024-03-14 21:44:37 -07:00
### convert_to_bool
```python
2024-03-14 21:44:37 -07:00
def convert_to_bool(value: object) -> bool
```
2024-03-14 21:44:37 -07:00
Converts string, integer, or float input to boolean by checking
for 'truthy' values.
2024-02-02 15:14:30 -08:00
Parameters:
2024-03-14 21:44:37 -07:00
value: (string/integer/float) Input value
2024-02-02 15:14:30 -08:00
Returns:
2024-03-14 21:44:37 -07:00
True/False False unless a 'truthy' number or string is found
('y', 'yes', 'true', '1', 1, -1, etc.)
2024-02-02 15:14:30 -08:00
2024-03-14 21:44:37 -07:00
<a id="jc.utils.convert_to_float"></a>
2024-02-02 15:14:30 -08:00
2024-03-14 21:44:37 -07:00
### convert_to_float
2024-02-02 15:14:30 -08:00
```python
2024-03-14 21:44:37 -07:00
def convert_to_float(value: object) -> Optional[float]
2024-02-02 15:14:30 -08:00
```
2024-03-14 21:44:37 -07:00
Converts string and int input to float. Strips all non-numeric
characters from strings.
2024-02-02 15:14:30 -08:00
Parameters:
2024-03-14 21:44:37 -07:00
value: (string/integer) Input value
Returns:
2024-03-14 21:44:37 -07:00
float/None Float if successful conversion, otherwise None
2022-01-25 17:07:47 -08:00
<a id="jc.utils.convert_to_int"></a>
2024-03-14 21:44:37 -07:00
### convert_to_int
2021-03-24 12:47:57 -07:00
2021-04-18 11:46:42 -07:00
```python
2022-10-19 08:33:09 -07:00
def convert_to_int(value: object) -> Optional[int]
2021-04-18 11:46:42 -07:00
```
2022-01-19 11:08:59 -08:00
Converts string and float input to int. Strips all non-numeric
characters from strings.
2021-04-18 11:46:42 -07:00
2022-01-25 18:03:34 -08:00
Parameters:
2022-01-25 17:07:47 -08:00
2022-01-26 16:55:39 -08:00
value: (string/float) Input value
2021-04-18 11:46:42 -07:00
2022-01-25 18:03:34 -08:00
Returns:
2021-04-18 11:46:42 -07:00
2022-01-25 18:03:34 -08:00
integer/None Integer if successful conversion, otherwise None
2021-04-18 11:46:42 -07:00
2024-03-14 21:44:37 -07:00
<a id="jc.utils.error_message"></a>
2021-04-18 11:46:42 -07:00
2024-03-14 21:44:37 -07:00
### error_message
2021-04-18 11:46:42 -07:00
```python
2024-03-14 21:44:37 -07:00
def error_message(message_lines: List[str]) -> None
2021-04-18 11:46:42 -07:00
```
2024-03-14 21:44:37 -07:00
Prints an error message to `STDERR` for fatal issues. The first line is
prepended with 'jc: Error - ' and subsequent lines are indented.
Wraps text as needed based on the terminal width.
2021-04-18 11:46:42 -07:00
2022-01-25 18:03:34 -08:00
Parameters:
2021-04-18 11:46:42 -07:00
2024-03-14 21:44:37 -07:00
message: (list) list of string lines
2021-04-18 11:46:42 -07:00
2022-01-25 18:03:34 -08:00
Returns:
2021-04-18 11:46:42 -07:00
2024-03-14 21:44:37 -07:00
None - just prints output to STDERR
2021-04-18 11:46:42 -07:00
2024-03-14 21:44:37 -07:00
<a id="jc.utils.has_data"></a>
2022-01-25 17:07:47 -08:00
2024-03-14 21:44:37 -07:00
### has_data
2021-04-18 11:46:42 -07:00
```python
2024-03-14 21:44:37 -07:00
def has_data(data: Union[str, bytes]) -> bool
2021-04-18 11:46:42 -07:00
```
2024-03-14 21:44:37 -07:00
Checks if the string input contains data. If there are any
non-whitespace characters then return `True`, else return `False`.
For bytes, returns True if there is any data.
2021-04-18 11:46:42 -07:00
2022-01-25 18:03:34 -08:00
Parameters:
2022-01-25 17:07:47 -08:00
2024-03-14 21:44:37 -07:00
data: (string, bytes) input to check whether it contains data
2021-04-18 11:46:42 -07:00
2022-01-25 18:03:34 -08:00
Returns:
2021-04-18 11:46:42 -07:00
2024-03-14 21:44:37 -07:00
Boolean True if input string (data) contains non-whitespace
characters, otherwise False. For bytes data, returns
True if there is any data, otherwise False.
2021-04-18 11:46:42 -07:00
2024-03-14 21:44:37 -07:00
<a id="jc.utils.input_type_check"></a>
2023-12-09 11:41:06 -08:00
2024-03-14 21:44:37 -07:00
### input_type_check
2023-12-09 11:41:06 -08:00
```python
2024-03-14 21:44:37 -07:00
def input_type_check(data: object) -> None
2023-12-09 11:41:06 -08:00
```
2024-03-14 21:44:37 -07:00
Ensure input data is a string. Raises `TypeError` if not.
2023-12-09 11:41:06 -08:00
2024-03-14 21:44:37 -07:00
<a id="jc.utils.is_compatible"></a>
2022-01-25 17:07:47 -08:00
2024-03-14 21:44:37 -07:00
### is_compatible
2021-09-13 21:14:56 -07:00
2021-12-01 16:12:51 -08:00
```python
2024-03-14 21:44:37 -07:00
def is_compatible(compatible: List[str]) -> bool
2021-12-01 16:12:51 -08:00
```
2022-01-25 17:07:47 -08:00
2024-03-14 21:44:37 -07:00
Returns True if the parser is compatible with the running OS platform.
2021-12-01 16:12:51 -08:00
2024-01-05 15:05:59 -08:00
<a id="jc.utils.line_slice"></a>
2024-03-14 21:44:37 -07:00
### line_slice
2024-01-05 15:05:59 -08:00
```python
2024-01-06 18:23:46 -08:00
def line_slice(
2024-03-14 21:44:37 -07:00
data: Union[str, Iterable[str], TextIO, bytes, NoneType],
2024-01-06 18:23:46 -08:00
slice_start: Optional[int] = None,
slice_end: Optional[int] = None
2024-03-14 21:44:37 -07:00
) -> Union[str, Iterable[str], TextIO, bytes, NoneType]
2024-01-05 15:05:59 -08:00
```
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)
2024-03-14 21:44:37 -07:00
<a id="jc.utils.normalize_key"></a>
2022-01-25 17:07:47 -08:00
2024-03-14 21:44:37 -07:00
### normalize_key
2022-01-25 17:07:47 -08:00
2021-03-24 12:47:57 -07:00
```python
2024-03-14 21:44:37 -07:00
def normalize_key(data: str) -> str
2021-03-24 12:47:57 -07:00
```
2024-03-14 21:44:37 -07:00
Normalize a key name by shifting to lower-case and converting special
characters to underscores.
Special characters are defined as `space` and the following:
!"#$%&'()*+,-./:;<=>?@[\]^`{|}~
This is a lossy algorithm. Repeating and trailing underscores are
removed.
Parameters:
data: (string) Input value
Returns:
string
2022-01-26 14:21:06 -08:00
2024-03-14 21:44:37 -07:00
<a id="jc.utils.remove_quotes"></a>
### remove_quotes
2022-01-26 14:21:06 -08:00
```python
2024-03-14 21:44:37 -07:00
def remove_quotes(data: str) -> str
2022-01-26 14:21:06 -08:00
```
2024-03-14 21:44:37 -07:00
Remove single or double quotes surrounding a string. If no quotes are
found then the string is returned unmodified.
2021-03-24 12:47:57 -07:00
2022-01-25 18:03:34 -08:00
Parameters:
2024-03-14 21:44:37 -07:00
data: (string) Input value
Returns:
string
2022-01-25 18:03:34 -08:00
2024-03-14 21:44:37 -07:00
<a id="jc.utils.warning_message"></a>
2022-02-07 15:44:54 -08:00
2024-03-14 21:44:37 -07:00
### warning_message
2022-01-25 18:03:34 -08:00
2024-03-14 21:44:37 -07:00
```python
def warning_message(message_lines: List[str]) -> None
```
2022-01-25 18:03:34 -08:00
2024-03-14 21:44:37 -07:00
Prints warning message to `STDERR` for non-fatal issues. The first line
is prepended with 'jc: Warning - ' and subsequent lines are indented.
Wraps text as needed based on the terminal width.
Parameters:
2022-01-25 18:03:34 -08:00
2024-03-14 21:44:37 -07:00
message: (list) list of string lines
2022-01-25 18:03:34 -08:00
2024-03-14 21:44:37 -07:00
Returns:
None - just prints output to STDERR
2021-03-24 12:47:57 -07:00
2022-12-13 13:30:50 -08:00