Registry / serialization / magicattr

magicattr

JSON →
library0.1.6pypypi✓ verified 24d ago

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.

pip install magicattr
INSTALL
IMPORT
SIG · MAGICATTR
M
magicattr
serializationpythonv0.1.6
Install
1.5s avg
Import
10ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.6 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.006s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.006s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

get
from magicattr import get
from magicattr import magicattr
set
from magicattr import set
delete
from magicattr import delete

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}")
Debug
Known issues
gotchaMagicAttr's path parsing does not support advanced Python syntax such as slicing (e.g., `list[1:3]`), arbitrary expressions (e.g., `list[0+1]`), function calls (e.g., `list.pop(0)`), or `eval` related constructs within the attribute string. Attempting to use these will result in `NotImplementedError` or `ValueError`.
fix
Ensure attribute paths are simple dot-separated or bracket-indexed strings. Perform complex logic outside the `magicattr` call.
affects: All versions
gotchaUnlike standard `getattr`/`setattr` which consistently raise `AttributeError` for missing or invalid attributes, `magicattr` aims to return the underlying exception (e.g., `KeyError` for dicts, `IndexError` for lists) or `None` for missing attributes/indices. This can lead to different error handling patterns than typically expected from Python's built-in attribute access.
fix
Inspect the return value carefully for `None` or specific exceptions like `KeyError` or `IndexError` when an attribute might not exist, rather than solely catching `AttributeError`.
affects: All versions
gotchaWhen using `magicattr.set()` to create new, deeply nested structures (e.g., `magicattr.set(obj, 'new_attr.nested_key', 'value')`), the library automatically infers and creates intermediate `dict` or `list` types as needed. While convenient, this might not always align with expectations if a different custom object type was desired for those intermediate steps.
fix
Explicitly initialize intermediate objects with the desired types before using `magicattr.set()` if `dict` or `list` are not appropriate defaults.
affects: All versions
Upgrade
Version history
0.1.6latest on PyPI · released Jan 25, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
10
Amazon
1
Resources
magicattr — pip install magicattr · libregistry