MagicAttr

0.1.6 · active · verified Thu Apr 09

MagicAttr (current version 0.1.6) provides an enhanced `getattr` and `setattr` functionality for Python, capable of navigating and modifying attributes within deeply nested objects, lists, and dictionaries. It stands out by retaining the original cause of failure instead of exclusively raising an `AttributeError`, offering more granular error information. The library sees infrequent but active maintenance, with the latest release in January 2022.

Warnings

Install

Imports

Quickstart

Demonstrates getting, setting, and deleting nested attributes using string paths, including auto-creation of intermediate dictionary structures.

from magicattr import magicattr

class Person:
    def __init__(self, name, age, settings=None, friends=None):
        self.name = name
        self.age = age
        self.settings = settings if settings is not None else {}
        self.friends = friends if friends is not None else []

bob = Person("Bob", 30, {'preferences': {'theme': 'dark'}})
jill = Person("Jill", 25, friends=[Person("Jack", 28), Person("Jane", 27)])

# Get a nested attribute
theme = magicattr.get(bob, 'settings.preferences.theme')
print(f"Bob's theme: {theme}")

# Set a nested attribute
magicattr.set(bob, 'age', 32)
print(f"Bob's new age: {bob.age}")

magicattr.set(jill, 'friends[0].age', 29)
print(f"Jill's first friend's new age: {jill.friends[0].age}")

# Delete a nested attribute
magicattr.delete(jill, 'friends[0]')
print(f"Jill's friends after deletion: {[f.name for f in jill.friends] if jill.friends else 'None'}")

# Demonstrate setting a new deeply nested attribute, creating intermediate dicts
magicattr.set(bob, 'address.city', 'New York')
print(f"Bob's city: {bob.address.city}")

view raw JSON →