Skip to main content

Use JavaScript-like object definition syntax in Python

Project description

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.

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

literal_dict-1.0.1.1.tar.gz (2.6 kB view details)

Uploaded Source

Built Distribution

literal_dict-1.0.1.1-py3-none-any.whl (2.7 kB view details)

Uploaded Python 3

File details

Details for the file literal_dict-1.0.1.1.tar.gz.

File metadata

  • Download URL: literal_dict-1.0.1.1.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.14.0 CPython/3.12.0 Windows/11

File hashes

Hashes for literal_dict-1.0.1.1.tar.gz
Algorithm Hash digest
SHA256 10af5a9798c21a2b55ef58663901544f1775fbc311600f8943e1096492ef396f
MD5 8d5d83ec0af1feb16b345ec6214ac11b
BLAKE2b-256 86cba71347c8b52cff789edaf5862b344cb2c6302596c8539f386a70226a2209

See more details on using hashes here.

File details

Details for the file literal_dict-1.0.1.1-py3-none-any.whl.

File metadata

  • Download URL: literal_dict-1.0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 2.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.14.0 CPython/3.12.0 Windows/11

File hashes

Hashes for literal_dict-1.0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fceeda776f58537d972bf2368460bc37e1be06597192b7c642227b429ab8a81b
MD5 3b9e9106a15c45a7bf49ec05b3cde10d
BLAKE2b-256 a2e8cdd8dff7b07bf317629b4e8ba0d158239b5f7520b18e697f557891dd9d5c

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