Dictionary key mapping tool that enables the user to declare a single keyword regardless of depth.
Project description
KeyMapper
KeyMapper is a subclass of the dictionary class, which allows you to:
- Assign keys and values dynamically
- Choose what delimiter you want (default is dot-notation: .)
- Access all keys and values in a single key declaration regardless of type (list, tuple, set)
- Is treated as a dict type by default
- Initializes an empty dictionary, or accepts an existing dict upon class declaration
- Great for key iteration and loops
- KeyMapper functionality is not forced on you
Installation
-
You can install either via pypi:
pip install keymapper
-
Or, install it locally after the package has been unarchived:
pip install -e /destination
Examples
Standard Dict:
from keymapper import KeyMapper
km_dict = KeyMapper()
print(km_dict)
# Prints: {}
km_dict['key1'] = 'All the keys!'
print(km_dict)
# Prints: {'key1': 'All the keys!'}
Nested Dict:
from keymapper import KeyMapper
my_dict = {'messages': {'message_1': 'Hey there!'}}
km_dict = KeyMapper(my_dict)
# Standard
print(km_dict['messages']['message_1']) # Prints: 'Hey there!'
# KeyMapper
print(km_dict['messages.message_1']) # Prints: 'Hey there!'
Dict with iterables:
from keymapper import KeyMapper
my_dict = {
'messages': {'message_1': 'Hey there!'},
'objects': [{'obj1': 'Hi!'}],
'objects2': ({'obj2': 'There!'}),
'objects3': {'A', 'Peoples!', 'Friend?'}
}
km_dict = KeyMapper(my_dict)
# Standard
print(km_dict['objects'][1]['obj2']) # Prints: 'There!'
# KeyMapper - does not care what type of iterable
print(km_dict['objects2.0.obj2']) # Prints: 'There!'
print(km_dict['objects3.1']) # Prints: 'Peoples!'
print(km_dict['objects.0.obj1']) # Prints: 'Hi!'
# Or don't even declare the data type
print(km_dict['objects.1.obj2']) # Prints: 'There!'
Dict with different delimieter:
from keymapper import KeyMapper
my_dict = {'messages': {'message_1': 'Hey there!'}, 'objects': [{'obj1': 'Hi!'}, {'obj2': 'There!'}, {'obj3': 'Peoples!'}]}
km_dict = KeyMapper(my_dict, delimiter=',')
# Standard
print(km_dict['messages']['message_1']) # Prints: 'Hey there!'
# KeyMapper with new delimeter
print(km_dict['messages,message_1']) # Prints: 'Hey there!'
Example
from keymapper import KeyMapper
questions = ['What is your name: ', 'What is your email: ', 'What is your password: ', 'How many servers do you want to add: ', 'What is the ip: ', 'What is the port: ']
keys = ['user.name', 'user.email', 'user.password', 'servers']
config = {'user': {'name': '', 'email': '', 'password': ''}, 'servers': []}
km_dict = KeyMapper(config)
for i in range(len(questions)):
if i < 3:
km_dict[keys[i]] = input(questions[i])
else:
for r in range(int(input(questions[i]))):
server_info = {'ip': '', 'port': ''}
for i, k in enumerate(questions[4:]):
if i < 1:
server_info['ip'] = input(k)
else:
server_info['port'] = input(k)
km_dict[keys[len(keys) - 1]].append(server_info)
break
print(km_dict) # Prints: ... Well, whatever you entered as your input values! Try it if you don't believe me ;)
for i in range(len(km_dict['servers'])):
print(km_dict['servers.{}.ip'.format(i)]) # Prints the IP for each index iterated through
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
keymapper-1.0.5.tar.gz
(4.1 kB
view details)
File details
Details for the file keymapper-1.0.5.tar.gz
.
File metadata
- Download URL: keymapper-1.0.5.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0432b63f1ab8fbab0007844b897dbab37297056fa9912bee070aa21817b36d0c |
|
MD5 | d29946448ff257ab5e9531cf6ab75acc |
|
BLAKE2b-256 | 2502615ecc087dc478b4f50f7e167150beb17be86c2ec9b62d5648647cfd96c5 |