Skip to main content

A simple yet powerful decorator to create custom classes with dict-like behavior.

Project description

dictclasses

A simple yet powerful decorator to create custom classes with dict-like behavior.

Installation

pip install dictclasses

Description

dictclasses provides the @dictclass decorator that transforms any Python class into a dict-like object. The decorated class accepts a dictionary as its first argument and exposes all standard dictionary methods (keys, values, items, get, pop, update, etc.) while allowing you to define custom methods and properties.

Usage

Basic usage

from dictclasses import dictclass

@dictclass
class Person:
    @property
    def full_name(self):
        return f"{self['first_name']} {self['last_name']}"

person = Person({"first_name": "John", "last_name": "Doe"})
print(person["first_name"])  # John
print(person.full_name)      # John Doe
print(person.keys())         # dict_keys(['first_name', 'last_name'])

From JSON / HJSON files

@dictclass
class Config:
    pass

config = Config.from_json("config.json")
# or
config = Config.from_hjson("config.hjson")

Available methods

The decorator adds the following dict methods (unless already defined in the class):

  • __getitem__, __setitem__
  • __len__, __iter__
  • keys, values, items
  • get, pop, popitem
  • copy, deepcopy
  • update, clear, setdefault
  • fromkeys
  • from_json, from_hjson (class methods)

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

dictclasses-1.0.0.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dictclasses-1.0.0-py3-none-any.whl (2.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dictclasses-1.0.0.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for dictclasses-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ef7a2febf2916fd2533833a890ac3f1f2bb21b671b5ddb0c20b8ad9a7f535646
MD5 022d7185962492401c7e0995d65fb37f
BLAKE2b-256 6c92c3da2694c62e77481f881ab7bdc2054de30d8869915eed0a3c09fe56a854

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dictclasses-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 2.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for dictclasses-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f1ff0a29ced43d73e94fb2d4c9969e7c7a904abc8655d1f9c4b48a06d035e766
MD5 0bced6fc9b9d8cf1c0e4a0a944772737
BLAKE2b-256 561548145f3bc425d35658e8d31f50fed5b86f8017f88e80cfd5c23ca9169508

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page