corelibs-iterator (1.0.0)
Installation
pip install --index-url https://git.gullevek.org/api/packages/PyPI/pypi/simple/ --extra-index-url https://pypi.org/simple corelibs-iteratorAbout this package
CoreLibs Dict and List support
CoreLibs Python Iterator support
This is part of the Python CoreLibs
Overview
Dict and list support
Install
uv add --index opj-pypi=https://git.egplusww.jp/api/packages/PyPI/pypi/simple/ corelibs-iterator
Usage
The following packages are available
- corelibs_iterator.dict_support
- corelibs_iterator.list_support
corelibs_iterator.dict_support usage
from corelibs_iterator.dict_support import delete_keys_from_set, convert_to_dict_type, set_entry
delete_keys_from_set
Deletes all keys with their values from a dictionary or a list of dictionaries
def delete_keys_from_set(
set_data: dict[str, Any] | list[Any] | str, keys: list[str]
) -> dict[str, Any] | list[Any] | Any:
Example
from corelibs_iterator.dict_support import delete_keys_from_set
data = {
"a": 1,
"i": "CORE",
"b": {
"c": 2,
"d": 3,
"i": "P",
"e": {
"f": 4,
"g": 5,
"i": "F",
}
},
"h": [
{"i": 6, "j": 7},
{"k": 8, "l": 9}
],
"m": "string_value"
}
print(f"RESULT: {delete_keys_from_set(data, ['c', 'f', 'i', 'm'])}")
Will result in
{
"a": 1,
"b": {
"d": 3,
"e": {
"g": 5
}
},
"h": [
{
"j": 7
},
{
"k": 8,
"l": 9
}
]
}
convert_to_dict_type
Builds a dictionary out of any input. This can be used if some TypeDef needs to be converted into a plain dict type If ignore_entries is set, than any key that match this list will be removed.
def convert_to_dict_type(
any_dict: Any, ignore_entries: list[str] | None = None
) -> dict[str, Any | list[Any] | dict[Any, Any]]:
set_entry
Set a dictionary entry with key and values attached, if key exists it will overwrite the values, not merge them
def set_entry(dict_set: dict[str, Any], key: str, value_set: Any) -> dict[str, Any]:
Example
from corelibs_iterator.dict_support import set_entry
test = {
'a': 5
}
test = set_entry(test, 'k', ['a', 'b'])
print(test)
Output will be
test = {
'a': 5,
'b': ['a', 'b']
}
corelibs_iterator.list_support usage
from corelibs_iterator.list_support import convert_to_list, is_list_in_list, make_unique_list_of_dicts
convert_to_list
Convert anything into a list unless it is already a list
def convert_to_list(
entry: str | int | float | bool | Sequence[str | int | float | bool | Sequence[Any]]
) -> Sequence[str | int | float | bool | Sequence[Any]]:
Example
from corelibs_iterator.list_support import convert_to_list
print(convert_to_list('foo'))
print(convert_to_list({'a': 'foo'}))
Will result in
['foo']
[{'a': 'foo'}]
is_list_in_list
Check if one list is in the other list and returns the entries from the first list that are not in the second list
def is_list_in_list(
list_a: Sequence[str | int | float | bool | Sequence[Any]],
list_b: Sequence[str | int | float | bool | Sequence[Any]]
) -> Sequence[str | int | float | bool | Sequence[Any]]:
Example
from corelibs_iterator.list_support import is_list_in_list,
list_a = [1, "hello", 3.14, True, "world"]
list_b = ["hello", True, 42]
result = is_list_in_list(list_a, list_b)
print(result)
Will output
[1, 'world', 3.14]
make_unique_list_of_dicts
Take a list of dictionaries and create a list of unique entries. All elements in the dictionary must match to be seen as unique, the order of elements in the dictionary is not relevant
def make_unique_list_of_dicts(dict_list: list[Any]) -> list[Any]:
Example below will combined {"a": 1, 1: "one"} and {"A": 1, 1: "one"} into one and keep the rest
from corelibs_iterator.list_support import make_unique_list_of_dicts
dict_list = [
{"a": 1, 1: "one"},
{"A": 1, 1: "one"},
{1: "one", "a": 1},
{"a": 2, 1: "one"}
]
unique_list = make_unique_list_of_dicts(dict_list)
print(unqiue_list)
Will output:
[
{
"a": 1,
"1": "one"
},
{
"A": 1,
"1": "one"
},
{
"a": 2,
"1": "one"
}
]
for more example look in the test-run/list_support.py
Development
UV setup
uv must be installed
Python venv setup
After clone, run the command below to install all dependenciss
uv sync
Build and Publish
uv build
uv publish --index opj-pypi --token <gitea token>
Python tests
All python tests are the tests/ folder. They are structured by the source folder layout
run them with
uv run pytest
Get a coverate report
uv run pytest --cov=<project>
uv run pytest --cov=<project> --cov-report=term-missing
Other tests
In the test-run folder usage and run tests are located, runt them below
uv run test-run/<script>