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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file hyperdict-1.0.0.tar.gz.

File metadata

  • Download URL: hyperdict-1.0.0.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.8.4 Windows/10

File hashes

Hashes for hyperdict-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2f0aab651da25db263782a2aad2297662c38022b4d8e04c7e5dda6011324faa7
MD5 c1e8062d2416a3e3fb6becbe43486865
BLAKE2b-256 20506541d781fba85f2b487be77dc17bca8a8095c8c8fc9e3cb1331b8398c93d

See more details on using hashes here.

File details

Details for the file hyperdict-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: hyperdict-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.8.4 Windows/10

File hashes

Hashes for hyperdict-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f74ae472a77850acf3d990b7fac69aa7e5183b6c4b9d4912ddfee3a37fa9bb2e
MD5 72cee65e1323fbe2e7f401b5ba7f667e
BLAKE2b-256 3c58e9a477e3732e4d1616c8b0037afd9991f417a0af07263961458321371fb2

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