Skip to main content

a dict with key attributes

Project description

a dict with key attributes

pip install punktdict

# Example usage:
from punktdict import PunktDict
PunktDict._check_forbidden_key = (
    lambda self, x: x.startswith("_ipytho") or x == "_repr_mimebundle_"
)  # To avoid accidental creation of certain keys when allow_nested_creation is True

dd = PunktDict(
    {
        "school": {
            "room1": {
                "student1": "Mario",
                "student2": "Paulo",
                "student3": "Bernd",
                "student4": "Richard",
                "student5": "Mairo",
            }
        }
    },
    allow_nested_creation=True,
)
print(f"--------------------\n{dd}")
dd["school"]["room1"]["student6"] = "Antonio"
print(f"--------------------\n{dd}")
dd["school"]["room2"]["student1"] = "Bruno"
print(f"--------------------\n{dd}")
dd.school.room3.student1 = "Bruno"
print(f"--------------------\n{dd}")
del dd["school"]["room2"]["student1"]
print(f"--------------------\n{dd}")
dd.university.all_rooms = []
print(f"--------------------\n{dd}")
dd.university.all_rooms.append(1)
dd["university"]["all_rooms"].append(2)
dd.university["all_rooms"].append(3)
print(f"--------------------\n{dd}")
print(type(dd))
print(type(dd.convert_to_regular_dict()))

print(dd.random_new_key)  # creates a new key

try:
    print(dd._repr_mimebundle_)  # forbidden
except AttributeError as e:
    print(e)

try:
    dd.keys = 3333
except KeyError as e:
    print(e)

# {'school': {'room1': {'student1': 'Mario', 'student2': 'Paulo', 'student3': 'Bernd', 'student4': 'Richard', 'student5': 'Mairo'}}}
# --------------------
# {'school': {'room1': {'student1': 'Mario', 'student2': 'Paulo', 'student3': 'Bernd', 'student4': 'Richard', 'student5': 'Mairo', 'student6': 'Antonio'}}}
# --------------------
# {'school': {'room1': {'student1': 'Mario', 'student2': 'Paulo', 'student3': 'Bernd', 'student4': 'Richard', 'student5': 'Mairo', 'student6': 'Antonio'}, 'room2': {'student1': 'Bruno'}}}
# --------------------
# {'school': {'room1': {'student1': 'Mario', 'student2': 'Paulo', 'student3': 'Bernd', 'student4': 'Richard', 'student5': 'Mairo', 'student6': 'Antonio'}, 'room2': {'student1': 'Bruno'}, 'room3': {'student1': 'Bruno'}}}
# --------------------
# {'school': {'room1': {'student1': 'Mario', 'student2': 'Paulo', 'student3': 'Bernd', 'student4': 'Richard', 'student5': 'Mairo', 'student6': 'Antonio'}, 'room2': {}, 'room3': {'student1': 'Bruno'}}}
# --------------------
# {'school': {'room1': {'student1': 'Mario', 'student2': 'Paulo', 'student3': 'Bernd', 'student4': 'Richard', 'student5': 'Mairo', 'student6': 'Antonio'}, 'room2': {}, 'room3': {'student1': 'Bruno'}}, 'university': {'all_rooms': []}}
# --------------------
# {'school': {'room1': {'student1': 'Mario', 'student2': 'Paulo', 'student3': 'Bernd', 'student4': 'Richard', 'student5': 'Mairo', 'student6': 'Antonio'}, 'room2': {}, 'room3': {'student1': 'Bruno'}}, 'university': {'all_rooms': [1, 2, 3]}}
# <class '__main__.PunktDict'>
# <class 'dict'>

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

punktdict-0.10.tar.gz (5.0 kB view hashes)

Uploaded Source

Built Distribution

punktdict-0.10-py3-none-any.whl (6.1 kB view hashes)

Uploaded Python 3

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