yl
Get dictionaries from other objects
To install: pip install yl
Overview
The yl package provides a set of utilities to convert data structures into more usable dictionary formats, particularly focusing on data extraction and transformation from pandas DataFrames. It also includes functionalities to safely read JSON files and merge dictionaries with customizable strategies.
Features
DataFrame to Dictionary Conversion
keyval_df: Convert a DataFrame into a dictionary using one column for keys and another for values.df_to_dict_list: Transform a DataFrame into a list of dictionaries, with options to specify which columns to use for keys and values.df_to_keyed_dict: Convert a DataFrame into a dictionary, using one column as keys and applying an optional transformation function to the rest of the data in each row.
JSON Utilities
json_str_file: Load and parse a JSON file into a Python object.safe_json_str_file: Safely read a JSON file, ensuring the file is properly closed after reading.
Dictionary Utilities
filter_dicts_by_keys: Filter a list of dictionaries, retaining only specified keys.merge_dicts: Merge multiple dictionaries into one, with a customizable strategy for handling key collisions.
Usage Examples
Convert DataFrame to Dictionary
import pandas as pd
df = pd.DataFrame({
'id': [1, 2, 3],
'name': ['Alice', 'Bob', 'Charlie'],
'age': [25, 30, 35]
})
result = df_to_keyed_dict(df, 'id')
print(result)
# Output: {1: {'name': 'Alice', 'age': 25}, 2: {'name': 'Bob', 'age': 30}, 3: {'name': 'Charlie', 'age': 35}}
Read JSON File Safely
json_data = safe_json_str_file('example.json')
print(json_data)
# Assuming 'example.json' contains '{"key": "value"}'
# Output: {'key': 'value'}
Filter List of Dictionaries
dict_list = [{'name': 'Alice', 'age': 25, 'city': 'New York'}, {'name': 'Bob', 'age': 30, 'city': 'Chicago'}]
filtered = filter_dicts_by_keys(dict_list, ['name', 'city'])
print(filtered)
# Output: [{'name': 'Alice', 'city': 'New York'}, {'name': 'Bob', 'city': 'Chicago'}]
Merge Dictionaries with Custom Strategy
d1 = {'name': 'Alice', 'age': 25}
d2 = {'name': 'Bob', 'city': 'New York'}
merged = merge_dicts(d1, d2, merge_strategy=lambda values: ', '.join(map(str, values)))
print(merged)
# Output: {'name': 'Alice, Bob', 'age': 25, 'city': 'New York'}
Documentation
Each function in the yl package is documented with Python docstrings, providing detailed descriptions of their purpose, parameters, return values, and examples. This documentation can be accessed using the help function in Python, e.g., help(df_to_keyed_dict).
Metadata
Release files for yl 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 | |
|---|---|---|---|
| yl-0.0.7.tar.gz | 8.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| yl-0.0.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.4 kB
Release files / yl-0.0.7.tar.gz
| Download URL | yl-0.0.7.tar.gz |
|---|---|
| Size | 8.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6bee31efe1d92358e6ff6b68d56caa592b1d75b01e8bfcc91f780b331c713916
|
|
BLAKE2b-256 checksum How to use checksums |
d3ae54d4995bf00e22cc5e369ce3b3c5543f9081f4073b924a8cf79e7d8599c8
|
| 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 / yl-0.0.7-py3-none-any.whl
| Download URL | yl-0.0.7-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
58092c869964711da168e1bc06c91872d11b44d883b9b9e11fb1ce038f060516
|
|
BLAKE2b-256 checksum How to use checksums |
e533c87f62a1e6a5af4779e894c9295553d9cfef73172ce69ca2fb33d8e8592b
|
| 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}
|