Skip to main content

Python dictionaries, but on steroids.

Project description

hyperdict

Python dictionaries, but on steroids.

hyperdict works just like the old dictionary but with more additional features. It makes working with dictionaries relatively quicker and easier!

  • Built for clean and shorter adders, getters, and setters.

  • It significantly reduces the lines of code written for dictionary manipulations.

  • Variable names need not be re-written to build the hyperdict (see here).

  • hyperdict retrieve keys when values are given (value-key pairs).

  • Inbuilt unary operations developed with specific functionalities.

  • All inbuilt python dictionary methods work in hyperdict.

Installation

$ pip install hyperdict
---> 100%

# or

$ poetry add hyperdict
---> 100%

User Guide

import hyperdict as hd

Create a hyperdict object

Using the HyperDict class, we can build a hyper dictionary.

d = hd.HyperDict()

Basic Usage

Multiple keys can be assigned in a single line.

d[1, 2, 'name'] = None 
# HyperDict({1: None, 2: None, 'name': None})

# without hyperdict
d = {i: None for i in [1, 2, 'name']}

Using each() function, multiple keys can be assigned with coressponding multiple values.

d['name', 'age', 'skills'] = hd.each('Magnus', 31, ['chess', 'football'])
# HyperDict({'name': 'Magnus', 'age': 31, 'skills': ['chess', 'football']})

# without hyperdict
d['name'] = 'Magnus'
d['age'] = 31
d['skills'] = ['chess', 'football']

Multiple values can also be retrieved and can also be delelted using the same syntax.

d['name', 'age']
# ('Magnus', 31)
d['email'] # predefined value for a missing key is None
# None
del d['skills', 'email'] # 'skills' key will be deleted
# trial.py:23: Warning: Missing keys: email
...
... # execution continues after warning...

hyperdict as a callable instance

One of the most unique things about hyperdict is value-key retrieval. On accepting value(s) as arguments, the hyperdict function return the keys. On calling it without arguments would return a dictionary of all value keys (raises an error if values are not hashable types).

The hashable types are cached along with the keys for quicker retrieval from the hyperdict. The cache is cleared when the hyperdict internal dictionary is changed.

hashable types : Namely int(), bool(), str(), tuple(), these types in python are hashable since they are immutable. They are the types which are allowed to be used as keys in a python dictionary.

d = hd.HyperDict()
d[1, 2, 3] = hd.each(0, 1, 0)
d(0)
# (1, 3)
d(4) # default value for a missing key
# None
d(0, 1)
# ((1, 3), (2,))
d() # return a dict() of all the value-key pairs.
# {0: (1, 3), 1: (2,)}

Attributes and Operators

d.i # same as list(d.items())
d.k # same as list(d.keys())
d.v # same as list(d.values())

inv_d = ~d # Invertor Operation: Returns an Inverts key-values to value-key

# WARNING: This `~` operation works as expected if 
# - values are hashable types (raises an error)
# - values are unique like the keys (overwrites the prev key with a new key.)

cpy_d = +d # Copy Operation: Returns a python dictionary deep-copied from the hyperdict object

-d # Clear Operation: similar to clear() method of python dictionary. Clears the hyperdict dictionary.

Methods and functions.

to_hd(*a) Function

Creates a hyperdict using the variable name as keys.

You need not write the key names along with values anymore!

Warning: This function does not work in python console, since the nodes from AST are taken as a single expression resulting in None for the expression.

name, age, skills = foo_get_data()

h = hd.to_hd(name, age, skills) 
# HyperDict({'name': 'Magnus', 'age': 24, 'skills': ['chess', 'football']})

# without hyperdict
d = {}
d['name'] = name
d['age'] = age
d['skills'] = skills

change_no_value(any) and change_no_key(any): Changes default values for missing key and value(default is None).

d.change_no_key('No key found!')
d['name', 'random key']
# ('Magnus', 'No key found!')

d.change_no_value('')
d(24, 'random value')
# (('age',), '')

hash(): Creates hash of the dictionary exclusively.

d.hash() # hash of the dictionary alone.
# 123...
hash(d) # hash of the whole hyperdict instance.
# 321...

each(*a): Helper function which is used to map the corresponding values to the given keys.

d['name', 'age', 'skill'] = hd.each('Magnus', 31, ['Chess', 'Football'])

Docstrings

import hyperdict as hd
help(hd)

In-built dictionary methods

All the methods of python inbuilt dictionary works just the same in hyperdict.


Meta data

Dependencies

The to_hd() function in hyperdict uses executing by @alexmojaki to retrieve object's name and use it as a corresponding key for the value.

Licence

This project is licensed under the terms of the Apache License 2.0.

Developement

This package is developed using:

  • poetry: package and dependency manager.
  • pytest: tests.
  • pcmd: command line shortener.

The whole wrapper is in a single file hyperdict.py.

hyperdict
├── __init__.py
└── hyperdict.py <---

Tests

The test file is test_hyperdict.py

tests
├── __init__.py
└── test_hyperdict.py <---

pytest

$ pcmd run t

# or 

$ poetry run pytest -v

flake8

$ pcmd run f

# or

$ flake8 hyperdict/ tests/ --ignore=F401,W504

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

hyperdict-1.0.0.tar.gz (11.5 kB view hashes)

Uploaded Source

Built Distribution

hyperdict-1.0.0-py3-none-any.whl (11.2 kB view hashes)

Uploaded Python 3

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