Metadata-Version: 2.3
Name: corelibs-iterator
Version: 1.0.0
Summary: CoreLibs Dict and List support
Author: Clemens Schwaighofer
Author-email: Clemens Schwaighofer <clemens.schwaighofer@omc.com>
Requires-Python: >=3.13
Description-Content-Type: text/markdown

# CoreLibs Python Iterator support

This is part of the Python CoreLibs

## Overview

Dict and list support

## Install

```sh
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

```py
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

```py
def delete_keys_from_set(
    set_data: dict[str, Any] | list[Any] | str, keys: list[str]
) -> dict[str, Any] | list[Any] | Any:
```

Example

```py
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

```txt
{
    "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.

```py
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

```py
def set_entry(dict_set: dict[str, Any], key: str, value_set: Any) -> dict[str, Any]:
```

Example

```py
from corelibs_iterator.dict_support import set_entry

test = {
    'a': 5
}
test = set_entry(test, 'k', ['a', 'b'])
print(test)
```

Output will be

```py
test = {
    'a': 5,
    'b': ['a', 'b']
}
```

### corelibs_iterator.list_support usage

```py
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

```py
def convert_to_list(
    entry: str | int | float | bool | Sequence[str | int | float | bool | Sequence[Any]]
) -> Sequence[str | int | float | bool | Sequence[Any]]:
```

Example

```py
from corelibs_iterator.list_support import convert_to_list

print(convert_to_list('foo'))
print(convert_to_list({'a': 'foo'}))
```

Will result in

```txt
['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

```py
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

```py
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

```txt
 [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

```py
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

```py
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:

```txt
[
    {
        "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](https://docs.astral.sh/uv/getting-started/installation/)

### Python venv setup

After clone, run the command below to install all dependenciss

```sh
uv sync
```

### Build and Publish

```sh
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

```sh
uv run pytest
```

Get a coverate report

```sh
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

```sh
uv run test-run/<script>
```
