Add item access support for non-identifier subject/surface/transform names - #709
Open
alexhuth wants to merge 1 commit into
Open
Add item access support for non-identifier subject/surface/transform names#709alexhuth wants to merge 1 commit into
alexhuth wants to merge 1 commit into
Conversation
cortex.db.<subject> only works when the subject name happens to be a valid
python identifier, so a subject named e.g. "S1-test" was unreachable through
the tab interface. Add item access alongside attribute access at every level:
cortex.db['S1-test'].surfaces['flat-orig'].get()
cortex.db['S1-test'].transforms['full-head']['coord']
Database also gains __contains__ and __iter__, XfmDB gains __getattr__/__dir__
so attribute access works there too, and the bare `raise AttributeError` calls
now carry messages listing what is actually available. Dunder attributes are no
longer looked up in the filestore, which otherwise recursed through `subjects`
while the object was being copied or pickled.
Closes #699
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XvzkAD2Fu6irNYq4uKyfRT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds
__getitem__support throughout the database access hierarchy, enabling users to access subjects, surfaces, and transforms by name using bracket notation (e.g.,db['S1-test']). This is essential for names that are not valid Python identifiers and cannot be accessed via attribute notation.Key Changes
Database class: Added
__getitem__,__contains__, and__iter__methods to support item access for subjects. Improved__getattr__to provide helpful error messages directing users to use item access for non-identifier names.SubjectDB.surfaces (Surfaces class): Added
__getitem__method with informative KeyError messages listing available surfaces. Updated__getattr__to avoid recursion issues with dunder attributes.SubjectDB.transforms (Transforms class): Added
__getitem__method,__dir__method, and improved__getattr__to support both item and attribute access patterns.XfmSet class: Added
__getitem__method for accessing transform types (e.g., 'coord', 'magnet'),__dir__method, and improved__getattr__with better error handling.Error handling: All
__getitem__implementations now raiseKeyError(notAttributeError) with helpful messages listing available options. Dunder attributes are explicitly excluded from filestore lookups to prevent recursion during object construction, copying, or pickling.Documentation: Updated
docs/database.rstwith examples and explanation of item access for non-identifier names, including usage ofinoperator and iteration.Tests: Added comprehensive test suite (
cortex/tests/test_database.py) covering item access, attribute access, error messages, dunder attribute handling, and warning behavior.Notable Implementation Details
__*__) are explicitly checked and raiseAttributeErrorbefore attempting filestore lookups, preventing infinite recursion during object operations.https://claude.ai/code/session_01XvzkAD2Fu6irNYq4uKyfRT