Replies: 1 comment 5 replies
-
|
Can you please explain to me WHY exactly we need a |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
ModelCollection for List-Based Model Management
Status
Proposed
Context
The EasyScience corelib provides base classes for building scientific models, including
ModelBasewhich extends fromNewBase. However, there was no dedicated collection class that combines the list-like functionality of Python's built-in containers with the EasyScience features such as:Users who needed to manage collections of model objects had to either:
CollectionBasewhich is based onBasedBase(older architecture)This gap in the architecture made it difficult to create clean, maintainable code when working with multiple related model objects that need to share interfaces and participate in the EasyScience ecosystem.
Decision
We have decided to introduce a new
ModelCollectionclass that:ModelBasecollections.abc.MutableSequenceinterfaceTypeVar) for better IDE supportKey Design Decisions
1. Inheritance from ModelBase and MutableSequence
This dual inheritance provides:
2. Interface Propagation
The collection automatically propagates its interface to all contained items:
This ensures that when a collection's interface changes, all items are updated automatically.
3. Graph Edge Management
All collection operations (add, remove, replace) properly maintain graph edges:
4. Multiple Access Patterns
Support for accessing items by:
collection[0]collection[0:2]collection['item_name']collection[unique_id]5. Type Safety with Generics
Using
TypeVarand@overloaddecorators for precise type hints:Consequences
Positive
_add_itemand_remove_itemhelpersNegative
ModelCollectionvsCollectionBaseNeutral
CollectionBaseImplementation Details
File Changes
Based on PR #180, the implementation includes:
New File:
src/easyscience/base_classes/model_collection.py(278 lines)ModelCollectionclass implementationModified:
src/easyscience/base_classes/__init__.pyModelCollectionfrom base_classes moduleModified:
src/easyscience/base_classes/collection_base.pyNewBaseobjects (not justBasedBase)New File:
tests/unit_tests/base_classes/test_model_collection.py(953 lines)Key Methods
__init__: Initialize collection with optional items and interface__getitem__: Support int, slice, and string (name) indexing__setitem__: Replace items while maintaining graph edges__delitem__: Remove items and clean up graph edgesinsert: Add items at specific positionsinterfaceproperty: Get/set interface with propagationget_all_variables: Collect variables from all itemssort: Sort collection by custom mapping function_convert_to_dict: Serialize collection to dictionaryAlternatives Considered
Extending CollectionBase:
Composition over Inheritance:
Using Python's Built-in list:
Generic Container Class:
References
Notes
This ADR documents the design decisions made in PR #180 (New collection base). The implementation is currently in draft/review stage. Once merged, this ADR should be updated to "Accepted" status.
Future Considerations
Beta Was this translation helpful? Give feedback.
All reactions