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.
Metadata
Release files for yi 0.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| yi-0.0.7.tar.gz | 9.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| yi-0.0.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.7 kB
Release files / yi-0.0.7.tar.gz
| Download URL | yi-0.0.7.tar.gz |
|---|---|
| Size | 9.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c73b973accba43cef5dad5e1e6e691a2bb4e90a92dc062028638d3b5c669eff2
|
|
BLAKE2b-256 checksum How to use checksums |
9179101596adfc97403915c9ce401c5a6626a5a383fe27a5e860b87fd80ecc44
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / yi-0.0.7-py3-none-any.whl
| Download URL | yi-0.0.7-py3-none-any.whl |
|---|---|
| Size | 8.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
87b380183d4c34772e66a0cf744cb71714231793fabe93331563c94521a72677
|
|
BLAKE2b-256 checksum How to use checksums |
15bf9821ea5d886c5d35bbe8f38ae06f2a5875cbd6b03c9ed071f10e39bdac75
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|