Skip to main content

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 PyPI - version PyPI - Status PyPI - Python Version PyPI - Downloads
GitHub GitHub repo size GitHub last commit (by committer) Contributors
Tools PyCharm Ruff uv
CI/CD Tests Publish
Scans Coverage
Quality Gate Status Security Rating Maintainability Rating Reliability Rating
Lines of Code Vulnerabilities Bugs
Codacy Quality Codacy Coverage

Usage

dictify

Converts objects to dictionaries using handlers for mappings, iterables, and classes.

from collections import Counter
from dataclasses import dataclass
from datetime import datetime
from typing import Mapping

from mappingtools import dictify

data = {'key1': 'value1', 'key2': ['item1', 'item2']}
dictified_data = dictify(data)
print(dictified_data)
# Output: {'key1': 'value1', 'key2': ['item1', 'item2']}

counter = Counter({'a': 1, 'b': 2})
print(counter)
# Output: Counter({'b': 2, 'a': 1})

dictified_counter = dictify(counter)
print(dictified_counter)


# Output: {'a': 1, 'b': 2}


@dataclass
class SampleDataClass:
    a: int
    b: int
    aa: str
    bb: str
    c: list[int]
    d: Mapping
    e: datetime


sample_datetime = datetime(2024, 7, 22, 21, 42, 17, 314159)
sample_dataclass = SampleDataClass(1, 2, '11', '22', [1, 2], {'aaa': 111, 'bbb': '222'}, sample_datetime)

print(sample_dataclass)
# Output: SampleDataClass(a=1, b=2, aa='11', bb='22', c=[1, 2], d={'aaa': 111, 'bbb': '222'}, e=datetime.datetime(2024, 7, 22, 21, 42, 17, 314159))

dictified_sample_dataclas = dictify(sample_dataclass)
print(dictified_sample_dataclas)
# Output: {'a': 1, 'aa': '11', 'b': 2, 'bb': '22', 'c': [1, 2], 'd': {'aaa': 111, 'bbb': '222'}, 'e': datetime.datetime(2024, 7, 22, 21, 42, 17, 314159)}

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: defaultdict(<class 'set'>, {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(1, 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']}]

MappingCollector

A class designed to collect key-value pairs into an internal mapping, with two modes of operation: one_to_one and one_to_many. The mode determines whether each key maps to a single value or multiple values.

from mappingtools import MappingCollector, MappingCollectorMode

collector = MappingCollector(MappingCollectorMode.one_to_many)
collector.add('a', 1)
collector.add('a', 2)
collector.collect([('b', 3), ('b', 4)])
print(collector.mapping)
# Output: {'a': [1, 2], 'b': [3, 4]}

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

HTML Coverage Report

python -m pytest tests -n auto --cov=src --cov-branch --doctest-modules --cov-report=html

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

mappingtools-0.0.4.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

mappingtools-0.0.4-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file mappingtools-0.0.4.tar.gz.

File metadata

  • Download URL: mappingtools-0.0.4.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for mappingtools-0.0.4.tar.gz
Algorithm Hash digest
SHA256 50f477fe10d73e754a6ec646bf17ef229c010d8a8fc576873feb521913292e6e
MD5 ce952694be16e761b7f7b7dd8c047e32
BLAKE2b-256 5059c581e1ad52a1fbfb6bc5362557240c1d32cc9589772e94ff9ff184e850cb

See more details on using hashes here.

Provenance

File details

Details for the file mappingtools-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for mappingtools-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8a2587f24799da68cb3aa8c9a7564bc62810b91b4bdcaf291f16baf700376836
MD5 7624dbcfa809c9b26456419bccb16e0b
BLAKE2b-256 35967ced97a6a0c38370d469bfdd4e9517d733a17f7b2f88ed6b0db61cd68530

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page