Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cnseniorious000/literal-dict
Use js-like object definition syntax in Python
https://github.com/cnseniorious000/literal-dict
dict javascript-syntax python3 utilities
Last synced: about 1 month ago
JSON representation
Use js-like object definition syntax in Python
- Host: GitHub
- URL: https://github.com/cnseniorious000/literal-dict
- Owner: CNSeniorious000
- Created: 2024-01-24T14:30:16.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-04-16T11:28:53.000Z (9 months ago)
- Last Synced: 2024-12-17T12:55:57.278Z (about 1 month ago)
- Topics: dict, javascript-syntax, python3, utilities
- Language: Python
- Homepage: https://pypi.org/p/literal-dict
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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
```bash
pip install literal-dict
```## Usage
### Basic Dictionary Creation
Start by importing `DictBuilder` from the package:
```py
from literal_dict import DictBuilder
```Create a `DictBuilder` instance:
```py
d = DictBuilder()
```Now, you can create dictionaries with a simplified syntax:
```py
name = "Muspi Merol"
age = 20user = 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.
```python
from types import SimpleNamespacefrom literal_dict import DictBuilder
d = DictBuilder(lambda dict: SimpleNamespace(**dict))
name = "Muspi Merol"
email = "[email protected]"person = d[name, email]
print(person.name) # Output: Muspi Merol
print(person.email) # Output: [email protected]
```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`
```py
from collections import defaultdict
from functools import partialfrom 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.