Package: pydicttools
Python Dictionary Utility PackageThis package contains a collection of functions that perform various operations on Python dictionaries.Featuresconcat_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.InstallationTo install the package, run the following command:pipinstallpydicttoolUsagefrompython_dictionary_utilityimport*dict1={'a':1,'b':2}dict2={'c':3,'d':4}# Concatenate two dictionariesresult=concat_dict(dict1,dict2)print(result)# {'a': 1, 'b': 2, 'c': 3, 'd': 4}# Check if key exists in dictionaryresult=key_exists(dict1,'a')print(result)# True# Check if value exists in dictionaryresult=value_exists(dict1,2)print(result)# True# Clone objectresult=clone({'a':[1,2,3]})print(result)# {'a': [1, 2, 3]}# Check if dictionary is emptyresult=is_empty({})print(result)# True# Get value from dictionary by key pathresult=get_value_from_dict({'a':{'b':{'c':1}}},'a.b.c')print(result)# 1# Check if one dictionary is subset of anotheroriginal_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 functiondefcustom_key_extractor(key,value):returnvaluesorted_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}forkey,valueiniterate_dict(original_dict):print(f'{key}:{value}')# Output:# c: 3# a: 1# b: 2``# Clone objectresult=clone({'a':[1,2,3]})print(result)# {'a': [1, 2, 3]}clone_async example usage for python 3.8>=importasyncio# Original objectoriginal_obj={'a':[1,2,3],'b':{'c':4,'d':5}}asyncdefmain():# Asynchronously clone the objectcloned_obj=awaitclone_async(original_obj)# Print the cloned objectprint(cloned_obj)# {'a': [1, 2, 3], 'b': {'c': 4, 'd': 5}}# Run the main functionawaitmain()clone_async example usage for python 3.7<importasyncio# Original objectoriginal_obj={'a':[1,2,3],'b':{'c':4,'d':5}}asyncdefmain():# Asynchronously clone the objectcloned_obj=awaitclone_async(original_obj)# Print the cloned objectprint(cloned_obj)# {'a': [1, 2, 3], 'b': {'c': 4, 'd': 5}}# Run the main functionasyncio.run(main())