mappingtools. Do stuff with Mappings
Project description
MappingTools
This library provides utility functions for manipulating and transforming data structures which have or include Mapping-like characteristics. Including inverting dictionaries, converting class like objects to dictionaries, creating nested defaultdicts, and unwrapping complex objects.
Package |
|
Tools |
|
CI/CD |
|
Scans |
|
Usage
dictify
Converts objects to dictionaries using handlers for mappings, iterables, and classes.
from mappingtools import dictify
data = {'key1': 'value1', 'key2': ['item1', 'item2']}
dictified_data = dictify(data)
print(dictified_data)
# Output: {'key1': 'value1', 'key2': ['item1', 'item2']}
distinct
Yields distinct values for a specified key across multiple mappings.
from mappingtools import distinct
mappings = [
{'a': 1, 'b': 2},
{'a': 2, 'b': 3},
{'a': 1, 'b': 4}
]
distinct_values = list(distinct('a', *mappings))
print(distinct_values)
# Output: [1, 2]
keep
Yields subsets of mappings by retaining only the specified keys.
from mappingtools import keep
mappings = [
{'a': 1, 'b': 2, 'c': 3},
{'a': 4, 'b': 5, 'd': 6}
]
keys_to_keep = ['a', 'b']
result = list(keep(keys_to_keep, *mappings))
# result: [{'a': 1, 'b': 2}, {'a': 4, 'b': 5}]
inverse
Swaps keys and values in a dictionary.
from mappingtools import inverse
original_mapping = {'a': {1, 2}, 'b': {3}}
inverted_mapping = inverse(original_mapping)
print(inverted_mapping)
# Output: {1: 'a', 2: 'a', 3: 'b'}
nested_defaultdict
Creates a nested defaultdict with specified depth and factory.
from mappingtools import nested_defaultdict
nested_dd = nested_defaultdict(2, list)
nested_dd[0][1].append('value')
print(nested_dd)
# Output: defaultdict(<function nested_defaultdict.<locals>.factory at ...>, {0: defaultdict(<function nested_defaultdict.<locals>.factory at ...>, {1: ['value']})})
remove
Yields mappings with specified keys removed. It takes an iterable of keys and multiple mappings, and returns a generator of mappings with those keys excluded.
from mappingtools import remove
mappings = [
{'a': 1, 'b': 2, 'c': 3},
{'a': 4, 'b': 5, 'd': 6}
]
keys_to_remove = ['a', 'b']
result = list(remove(keys_to_remove, *mappings))
# result: [{'c': 3}, {'d': 6}]
unwrap
Transforms complex objects into a list of dictionaries with key and value pairs.
from mappingtools import unwrap
wrapped_data = {'key1': {'subkey': 'value'}, 'key2': ['item1', 'item2']}
unwrapped_data = unwrap(wrapped_data)
print(unwrapped_data)
# Output: [{'key': 'key1', 'value': [{'key': 'subkey', 'value': 'value'}]}, {'key': 'key2', 'value': ['item1', 'item2']}]
Development
Ruff
ruff check src
Test
Standard (cobertura) XML Coverage Report
python -m pytest tests -n auto --cov=src --cov-branch --doctest-modules --cov-report=xml --junitxml=test_results.xml
HTML Coverage Report
python -m pytest tests -n auto --cov=src --cov-branch --doctest-modules --cov-report=html --junitxml=test_results.xml
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for mappingtools-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82afc646b0ac1333135996803a081aa65fc452815fea7338aedd7c7fe449e755 |
|
MD5 | 370d8b759684434992016b70d294be40 |
|
BLAKE2b-256 | a1d4b550c0a84df28a6b26d09b7c483a16682e62a00a14aed13547f8d094706e |