Skip to main content

Set of tools to handle Dict type in Python.

Project description

Python Dictionary Utility Package

This package contains a collection of functions that perform various operations on Python dictionaries.

Features

concat_dict(dict1, dict2):

Takes two dictionaries as input and returns a new dictionary that is the result of merging the keys and values of both input dictionaries.

key_exists(dictionary, key):

Takes a dictionary and a key as input and returns a Boolean indicating whether the key exists in the dictionary.

value_exists(dictionary, value):

Takes a dictionary and a value as input and returns a Boolean indicating whether the value exists in the dictionary.

clone(obj):

Takes an object as input and returns a deep copy of the object.

is_empty(dictionary):

Takes a dictionary as input and returns a Boolean indicating whether the dictionary is empty.

get_value_from_dict(dictionary, key_path): Takes a dictionary and a key path as input and returns the value associated with the key path.

is_subset(dict1, dict2):

Takes two dictionaries as input and returns a Boolean indicating whether dict1 is a subset of dict2.

sort_dict_by_key(d):

Takes a dictionary as input and returns a new dictionary with the items sorted by key.

sort_dict_by_custom_key(d, key_extractor):

Takes a dictionary and a key extractor function as input and returns a new dictionary with the items sorted by the custom key extractor.

pretty_print_dict(d, indent):

Takes a dictionary and an indentation level as input and pretty prints the dictionary.

iterate_dict(d):

Takes a dictionary as input and returns an iterator over the key-value pairs of the dictionary.

clone_async(obj):

Takes an object as input and returns a deep copy of the object asynchronously.

Installation

To install the package, run the following command:

pip install pydicttool

Usage

from python_dictionary_utility import *

dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}

# Concatenate two dictionaries
result = concat_dict(dict1, dict2)
print(result) # {'a': 1, 'b': 2, 'c': 3, 'd': 4}

# Check if key exists in dictionary
result = key_exists(dict1, 'a')
print(result) # True

# Check if value exists in dictionary
result = value_exists(dict1, 2)
print(result) # True

# Clone object
result = clone({'a': [1, 2, 3]})
print(result) # {'a': [1, 2, 3]}

# Check if dictionary is empty
result = is_empty({})
print(result) # True

# Get value from dictionary by key path
result = get_value_from_dict({'a': {'b': {'c': 1}}}, 'a.b.c')
print(result) # 1

# Check if one dictionary is subset of another
original_dict = {'c': 3, 'a': 1, 'b': 2}
sorted_dict = sort_dict_by_key(original_dict)
print(sorted_dict) # {'a': 1, 'b': 2, 'c': 3}

original_dict = {'c': 3, 'a': 1, 'b': 2}

# Custom key extractor function
def custom_key_extractor(key, value):
    return value

sorted_dict = sort_dict_by_custom_key(original_dict, custom_key_extractor)
print(sorted_dict) # {'a': 1, 'b': 2, 'c': 3}

original_dict = {'c': 3, 'a': 1, 'b': 2}
pretty_print_dict(original_dict, 2)
# Output:
# {
#   "a": 1,
#   "b": 2,
#   "c": 3
# }

original_dict = {'c': 3, 'a': 1, 'b': 2}
for key, value in iterate_dict(original_dict):
    print(f'{key}: {value}')
# Output:
# c: 3
# a: 1
# b: 2
``

# Clone object
result = clone({'a': [1, 2, 3]})
print(result) # {'a': [1, 2, 3]}
clone_async example usage for python 3.8>=
import asyncio

# Original object
original_obj = {'a': [1, 2, 3], 'b': {'c': 4, 'd': 5}}

async def main():
    # Asynchronously clone the object
    cloned_obj = await clone_async(original_obj)

    # Print the cloned object
    print(cloned_obj) # {'a': [1, 2, 3], 'b': {'c': 4, 'd': 5}}

# Run the main function
await main()
clone_async example usage for python 3.7<
import asyncio

# Original object
original_obj = {'a': [1, 2, 3], 'b': {'c': 4, 'd': 5}}

async def main():
    # Asynchronously clone the object
    cloned_obj = await clone_async(original_obj)

    # Print the cloned object
    print(cloned_obj) # {'a': [1, 2, 3], 'b': {'c': 4, 'd': 5}}

# Run the main function
asyncio.run(main())

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

pydicttools-0.1.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

pydicttools-0.1.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file pydicttools-0.1.0.tar.gz.

File metadata

  • Download URL: pydicttools-0.1.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.6 Linux/5.10.16.3-microsoft-standard-WSL2

File hashes

Hashes for pydicttools-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dc23c1d21d2cb949fb46a334f65a3ec5cebf8185a736b7fd0b28a44cfed5e2b8
MD5 289e7e12af83e9eaa082f8e094ca2c05
BLAKE2b-256 76ab197b73ba64500f65b736f346455d87eaa3be8025371e84e48177915eff32

See more details on using hashes here.

File details

Details for the file pydicttools-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pydicttools-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.6 Linux/5.10.16.3-microsoft-standard-WSL2

File hashes

Hashes for pydicttools-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f214e53604d430f1e4450cef6924445211aa63629b541bc2a5e0cd35a0dc4487
MD5 9746d3a74fada890d07e855b545462fa
BLAKE2b-256 5918ba02e779e2b4384bb0cc7d0e789579075831ef74c0dbeec5c7f2b4157110

See more details on using hashes here.

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