Skip to main content

Literal Dict

literal-dict is a Python utility that allows for the creation of dictionary objects with a syntax reminiscent of JavaScript object notation. This tool is particularly useful for developers looking to streamline their code by reducing the verbosity commonly associated with dictionary creation in Python.

Features

  • Intuitive Variable Inclusion: Create Python dictionaries using a syntax similar to JavaScript objects: automatically uses variable names as keys, simplifying the process of dictionary creation.
  • Flexible Dictionary Implementations: Supports custom dictionary implementations, enabling behaviors like ordered dictionaries, default dictionaries, dot dictionaries, and more.
  • Various Python Distribution Supports: This package is tested under almost every popular python implementations.

Installation

pip install literal-dict

Usage

Basic Dictionary Creation

Start by importing DictBuilder from the package:

from literal_dict import DictBuilder

Create a DictBuilder instance:

d = DictBuilder()

Now, you can create dictionaries with a simplified syntax:

name = "Muspi Merol"
age = 20

user = d[name, age, "active": True]
print(user)  # Output: {'name': 'Muspi Merol', 'age': 20, 'active': True}

Using Custom Dictionary Implementations

DictBuilder allows specifying a custom dict-like type:

Example with types.SimpleNamespace

Using SimpleNamespace from the types module allows for attribute-style access to dictionary keys. This can make your code cleaner and more readable in some cases.

from types import SimpleNamespace

from literal_dict import DictBuilder

d = DictBuilder(lambda dict: SimpleNamespace(**dict))

name = "Muspi Merol"
email = "me@promplate.dev"

person = d[name, email]
print(person.name)  # Output: Muspi Merol
print(person.email)  # Output: me@promplate.dev

Note: When using SimpleNamespace, the returned object is not a dictionary but an instance of SimpleNamespace, which allows for dot-notation access to the attributes.

Example with collections.defaultdict

from collections import defaultdict
from functools import partial

from literal_dict import DictBuilder

d = DictBuilder(partial(defaultdict, int))

a = 1

obj = d[a, "b":2]
print(obj["c"])  # Output: 0, since 'c' does not exist, it returns the default int value

Conclusion

The Literal Dict Builder offers a succinct and intuitive way to create dictionaries in Python, drawing inspiration from JavaScript's object notation. Its support for custom dictionary implementations adds a layer of flexibility, allowing developers to tailor their data structures to fit their needs.

Metadata

Release files for literal-dict 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for literal-dict 1.1.0
File Size Uploaded
literal_dict-1.1.0.tar.gz 2.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for literal-dict 1.1.0
File Interpreter ABI Platform
literal_dict-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 5.5 kB

Release files / literal_dict-1.1.0.tar.gz

Download URL literal_dict-1.1.0.tar.gz
Size 2.7 kB
Tags Source
SHA-256 checksum
How to use checksums
64419e8ae85086fec946474139c135fa7cb998d211aabc36f175cf93d731df1e
BLAKE2b-256 checksum
How to use checksums
1be2c32724277dbf1881588ccdb5baccb7f94da3776e5f60435b8628661168af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.14.0 CPython/3.12.0 Windows/11

Release files / literal_dict-1.1.0-py3-none-any.whl

Download URL literal_dict-1.1.0-py3-none-any.whl
Size 2.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7fba6d287052ec33b04824da5d151f2241285f64a23e432597821d234fa025e8
BLAKE2b-256 checksum
How to use checksums
499a2cfd332547decfcd5a79408ac64cd38a4f5b056e0e8e95aa64510965ac17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.14.0 CPython/3.12.0 Windows/11

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page