Super Collections
Super Collections is a Python library (current version 0.6.2) that provides enhanced data structures, `SuperList` and `SuperDict`, which are designed for deeply nested data, similar to JSON or YAML structures. It offers features like recursive conversion of Python data structures, convenient dot-based access to nested elements, and a `super_collect()` factory function for easy instantiation. The library aims to simplify working with complex, nested collections and is actively maintained.
Warnings
- gotcha The `SuperCollection` class is an abstract base class and cannot be instantiated directly. `SuperList` and `SuperDict` are the concrete, instantiable collection types that are instances of `SuperCollection`.
- gotcha The `super_collect()` factory function performs recursive conversion of Python lists and dictionaries into `SuperList` and `SuperDict` instances. This might implicitly change the type of nested standard Python collections, which could be unexpected if strict preservation of original container types (e.g., `list` instead of `SuperList`) is critical for certain custom objects or behaviors.
- gotcha When using dot notation (e.g., `my_superdict.non_existent_key`) for accessing elements, attempting to access a non-existent key will raise an `AttributeError`, not a `KeyError` as with standard Python dictionaries. This distinction is important for error handling.
Install
-
pip install super-collections
Imports
- super_collect
from super_collections import super_collect
- SuperList
from super_collections import SuperList
- SuperDict
from super_collections import SuperDict
- SuperCollection
from super_collections import SuperCollection
Quickstart
from super_collections import super_collect, SuperList, SuperDict
# Create a SuperDict from a regular Python dictionary
data = {'user': {'name': 'Alice', 'details': {'age': 30, 'city': 'NY'}} }
sd = super_collect(data)
# Access nested elements using dot notation
print(f"User name: {sd.user.name}")
print(f"User city: {sd.user.details.city}")
# Create a SuperList from a regular Python list
items = [1, {'product': 'Laptop', 'price': 1200}, 3]
sl = super_collect(items)
# Access elements in a SuperList
print(f"First item: {sl[0]}")
print(f"Product name: {sl[1].product}")
# Modify elements
sd.user.details.age = 31
print(f"Updated age: {sd.user.details.age}")