Skip to main content

Convert dictionaries to other objects

Project description

yi

Convert dictionaries to other objects

To install: pip install yi

Overview

The yi package provides a collection of utilities for transforming, analyzing, and handling dictionaries in Python. It offers functions to convert dictionaries into different formats, manipulate dictionary keys and values, and create structured objects from dictionaries. This package is useful for data manipulation, especially when dealing with configurations, data records, or any structured data represented as dictionaries.

Features

  • Tuple Conversion: Convert dictionaries into a list of tuples, making it easy to reconstruct the original dictionary or manipulate dictionary items.
  • Table String Representation: Generate a formatted string representing the dictionary as a table, which is useful for logging or displaying dictionary contents in a readable format.
  • Namedtuple Creation: Create namedtuples from dictionaries, allowing for the use of dot notation to access dictionary values.
  • Hashable Dictionary: Provides a hashable dictionary class that can be used as keys in other dictionaries or stored in sets.
  • Struct Class: A flexible class that converts nested dictionaries into objects that can be accessed using attribute notation.
  • Dictionary Inversion: Functions to invert dictionaries, supporting one-to-one, one-to-many, and many-to-one mappings.
  • Word Replacement in Strings: Replace words in text based on a dictionary mapping, with support for regular expressions.

Usage Examples

Converting Dictionary to Tuple List

from yi import kv_tuple_list

d = {'apple': 1, 'banana': 2}
tuple_list = kv_tuple_list(d)
print(tuple_list)  # Output: [('apple', 1), ('banana', 2)]

Displaying Dictionary as Table String

from yi import table_str_with_key_and_value_columns

d = {'id': 1, 'name': 'John Doe'}
table_str = table_str_with_key_and_value_columns(d)
print(table_str)

Creating a Namedtuple from Dictionary

from yi import namedtuple

d = {'x': 1, 'y': 2}
Point = namedtuple(d, name='Point')
point = Point(x=1, y=2)
print(point.x, point.y)  # Output: 1 2

Using Hashable Dictionary

from yi import hashabledict

hd = hashabledict({'apple': 1, 'banana': 2})
dictionary_set = {hd}
print(dictionary_set)  # Output: {hashabledict({'apple': 1, 'banana': 2})}

Inverting Dictionary Mappings

from yi import inverse_one_to_one, inverse_one_to_many, inverse_many_to_one

# One-to-one inversion
d = {'A': 'a', 'B': 'b'}
print(inverse_one_to_one(d))  # Output: {'a': 'A', 'b': 'B'}

# One-to-many inversion
d = {'A': ['a', 'aa'], 'B': ['b']}
print(inverse_one_to_many(d))  # Output: {'a': 'A', 'aa': 'A', 'b': 'B'}

# Many-to-one inversion
d = {'a': 'A', 'aa': 'A', 'b': 'B'}
print(inverse_many_to_one(d))  # Output: {'A': ['a', 'aa'], 'B': ['b']}

Word Replacement in Text

from yi import word_replacer

rep_dict = {'hello': 'hi', 'world': 'earth'}
replace = word_replacer(rep_dict)
text = "hello world"
print(replace(text))  # Output: hi earth

Documentation

Each function and class in the yi package is documented with docstrings, providing examples and detailed usage instructions. Users are encouraged to refer to the source code for additional details on the functionality and to explore the wide range of utilities offered by the package.

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

yi-0.0.6.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yi-0.0.6-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file yi-0.0.6.tar.gz.

File metadata

  • Download URL: yi-0.0.6.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for yi-0.0.6.tar.gz
Algorithm Hash digest
SHA256 cee78658daed8eb3aeaaf03aba1d92ca0be7eba6c4b735987f825f999e6c7161
MD5 43c7014e53010b8532835688d9648e05
BLAKE2b-256 1379e3af6e67a179029c80df02ac36afa86654bd46fbeb88ddce6b482f944540

See more details on using hashes here.

File details

Details for the file yi-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: yi-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for yi-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 fdde6a311bb9b481298ca5b5c8a5248ab75dde71b866d1468bea07aa56b85e17
MD5 e7e398215ab2ac3c33da26d9e21df513
BLAKE2b-256 0c63d0beb3d0c518aa1fc999de3aacf143de3dd478af12db8e6f0115fbf20e49

See more details on using hashes here.

Supported by

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