core._base¶
Classes¶
IterateSortDict ¶
A class for recursively sorting dictionary keys with natural sorting.
This class provides methods to sort dictionary keys recursively, handling nested dictionaries and using natural sorting for strings with embedded numbers.
Attributes:
| Name | Type | Description |
|---|---|---|
reverse | bool | If True, sorts keys in descending order. Defaults to False. |
Example
sorter = IterateSortDict(reverse=False) data = {"item10": {"sub2": 1, "sub1": 2}, "item2": 3} sorted_data = sorter.dict_update(data)
Initialize the IterateSortDict instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reverse | bool | If True, sorts keys in descending order. Defaults to False. | False |
Source code in pyformatjson/core/_base.py
Functions¶
dict_sort ¶
Sort dictionary keys using natural sorting.
This method sorts the top-level keys of the dictionary using natural sorting that handles embedded numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
old | dict | The dictionary whose keys are to be sorted. | required |
Returns:
| Name | Type | Description |
|---|---|---|
dict | dict | A new dictionary with sorted keys. |
Source code in pyformatjson/core/_base.py
dict_sort_iteration ¶
Recursively sort nested dictionaries.
This method iterates through the dictionary and recursively sorts any nested dictionary values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
old | dict | The dictionary to be processed recursively. | required |
Returns:
| Name | Type | Description |
|---|---|---|
dict | dict | The dictionary with nested dictionaries sorted. |
Source code in pyformatjson/core/_base.py
dict_update ¶
Update and sort a dictionary recursively.
This method sorts the dictionary keys and recursively processes any nested dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
old | dict | The dictionary to be sorted and updated. | required |
Returns:
| Name | Type | Description |
|---|---|---|
dict | dict | The updated dictionary with sorted keys at all levels. |
Source code in pyformatjson/core/_base.py
Functions¶
sort_int_str ¶
Sort list of strings with embedded numbers naturally.
This function sorts a list of strings using natural sorting that handles embedded numbers correctly (e.g., "item2" comes before "item10").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
str_int | list[str] | List of strings to be sorted. | required |
reverse | bool | If True, sorts in descending order. Defaults to False. | False |
Returns:
| Type | Description |
|---|---|
list[str] | list[str]: Sorted list of strings. |
Example
sort_int_str(["item10", "item2", "item1"]) ['item1', 'item2', 'item10']
Source code in pyformatjson/core/_base.py
sort_strings_with_embedded_numbers ¶
Split string into pieces for natural sorting with embedded numbers.
This function splits a string into pieces where numbers are converted to integers for proper natural sorting (e.g., "item2" comes before "item10").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s | str | The string to be split into sortable pieces. | required |
Returns:
| Type | Description |
|---|---|
list[str] | list[str]: List of string pieces with numbers converted to integers. |
Example
sort_strings_with_embedded_numbers("item10") ['item', 10]
Source code in pyformatjson/core/_base.py
split_data_list ¶
Split data list according to the split pattern.
This function splits each string in the data list using the provided regex pattern and reconstructs the data based on the last_next parameter. The pattern must use capturing parentheses to define split points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
split_pattern | str | Regular expression pattern for splitting. Must use capturing parentheses, e.g., r"(\n)" for newline splits. | required |
data_list | list[str] | List of strings to be split and processed. | required |
last_next | str | Determines how to handle split parts. "next" places the split character at the beginning of the next part, "last" places it at the end of the current part. Defaults to "next". | 'next' |
Returns:
| Type | Description |
|---|---|
list[str] | list[str]: New list of processed strings with empty strings filtered out. |
Raises:
| Type | Description |
|---|---|
error | If the split_pattern is not a valid regular expression. |
Example
split_data_list(r"(\n)", ["line1\nline2", "line3\nline4"], "next") ['line1', 'line2', 'line3', 'line4']
Source code in pyformatjson/core/_base.py
split_text_by_length ¶
Split text into lines of specified maximum length.
This function breaks long text into multiple lines, ensuring each line does not exceed the specified maximum length. It attempts to break at word boundaries when possible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text | str | The input text to be split into lines. | required |
max_length | int | Maximum length for each line. Defaults to 120. | 120 |
Returns:
| Type | Description |
|---|---|
list[str] | list[str]: A list of text lines, each not exceeding max_length characters. |
Example
split_text_by_length("This is a very long text that needs to be split", 20) ['This is a very long', 'text that needs to be', 'split']
Source code in pyformatjson/core/_base.py
standardize_path ¶
Standardize and ensure a directory path exists.
This function expands environment variables and user home directory references in the path, then creates the directory if it doesn't exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_input | str | The input path to be standardized and created. | required |
Returns:
| Name | Type | Description |
|---|---|---|
str | str | The standardized absolute path. |
Example
standardize_path("~/Documents/data") '/Users/username/Documents/data'