Skip to main content

DictKit

A Python package that provides utility data structures with simple but powerful features, with a focus on flexibility and user experience.

UtilDict

A feature-enriched dictionary.

  • Access items with dot notation.
  • Flexible subscripting:
    • Get multiple items at once.
    • Set multiple items or the same value to multiple items at once.
  • Add items without mutating - return an updated copy
  • Drop items without mutating - return a filtered copy
  • Accepts a variety of argument types at creation.
  • Displays in nested format when printed.
  • Easy conversion to json format with .json()

Examples

from dictkit import UtilDict

# Like a dictionary...
ud = UtilDict(a=1, b=2, c=3)
print(ud)  # {'a': 1, 'b': 2, 'c': 3}

# ... but can be initialized from a variety of types
ud = UtilDict({"a": 1}, [("b", 2)], c=3)
print(ud)  # {'a': 1, 'b': 2, 'c': 3}

# Supports dot notation access
print(ud['a'])  # 1
print(ud.a)  # 1

# Get multiple items at once
selected_items = ud[["a", "c"]]
print(selected_items)  # {'a': 1, 'c': 3}

# Set multiple items at once
ud[["a", "c"]] = 10, 30
print(ud)  # {'a': 10, 'b': 2, 'c': 30}

# Set the same value to multiple keys at once
ud[["a", "c"]] = 99
print(ud)  # {'a': 99, 'b': 2, 'c': 99}

# Add items from a variety of types
ud2 = ud.add({"c": 3}, ("d", 4), e=5)
print(ud2)  # {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

# Add items from a 2-column dataframe
import pandas as pd
ud = UtilDict(a=1, b=2)
df = pd.DataFrame({"key": ["c", "d"], "value": [3, 4]})
ud2 = ud.add(df)
print(ud2)  # {'a': 1, 'b': 2, 'c': 3, 'd': 4}

# Drop multiple items at once
>>> ud3 = ud2.drop("a", "c")
>>> print(ud3)  # {'b': 2, 'd': 4}


render()

Somehow, this tool does not yet exist from any popular libraries.

Represents iterables in nested JSON structure, but with Python formatting.

>>> from dictkit.render import render
>>> dct = {'a':1, 'b': {'c':3}, 'list': [int,'b']}
>>> s = render(dct)
>>> s
{
   'a': 1,
   'b': {
      'c': 3
   },
   'list': [
      <class 'int'>,
      'b'
   ]
}

It handles multiline-formatted strings (like dataframes) elegantly, maintaining their original appearance.

>>> from pandas import DataFrame
>>> df = DataFrame([[1, 2, 3], [4, 55, 6]],
...                columns=["ONE", "TWO", "THREE"])
>>> fmt_str = '''|------|
... |      |
... |------|'''
>>> dct = {
...     "key": "value",
...     "formatted string": fmt_str,
...     "nested dct": {
...         "x": "y",
...         "dataframe": df,
...         "a": "b",
...         "formatted string": fmt_str
...     },
...     "lst": ['a', 'b'],
...     "tple": ('a','b')
... }
>>> render(dct, quote=False)
{
   key: value,
   formatted string:
      |------|
      |      |
      |------|,
   nested dct: {
      x: y,
      dataframe:
            ONE  TWO  THREE
         0    1    2      3
         1    4   55      6,
      a: b,
      formatted string:
         |------|
         |      |
         |------|
   },
   lst: [
      a,
      b
   ],
   tple: (
      a,
      b
   )
}

Metadata

Release files for dictkit 0.1.6

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

Source distribution (sdist)

Source distribution for dictkit 0.1.6
File Size Uploaded
dictkit-0.1.6.tar.gz 10.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dictkit 0.1.6
File Interpreter ABI Platform
dictkit-0.1.6-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 21.3 kB

Release files / dictkit-0.1.6.tar.gz

Download URL dictkit-0.1.6.tar.gz
Size 10.4 kB
Tags Source
SHA-256 checksum
How to use checksums
2618c7206309246c8032c80e162a05033bf5394eaaee1b7baa64a10fc8ae3165
BLAKE2b-256 checksum
How to use checksums
38ae3511c8e013d41b1c11e9f575874303142a1d1e162bdb22ecb671dff7163d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.2

Release files / dictkit-0.1.6-py2.py3-none-any.whl

Download URL dictkit-0.1.6-py2.py3-none-any.whl
Size 10.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
813ae3c870603f763fb7059944fd52eb935334b307c6801f69fd3b474aaa85b5
BLAKE2b-256 checksum
How to use checksums
773465df5cf54da5d5cc8912aa2d3cf4600d66ebfbaf3fc795cfd077b5eebd23
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.2

Release history Release notifications | RSS feed

This release

0.1.6 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.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