Skip to content

Conversation

@bossbeagle1509
Copy link

Support special stores in blosc2.open() and add context managers

This PR enhances blosc2.open() to support opening DictStore and EmbedStore files directly via file extensions. It also implements context manager support for these stores and refactors the open() function to improve code maintainability.

Key Changes:

  1. Unified blosc2.open() API:

    • Added support for detecting and opening DictStore files with extensions .b2z and .b2d
    • Added support for detecting and opening EmbedStore files with extension .b2e
    • Users can now use blosc2.open("data.b2z") instead of importing specific store classes
  2. Refactoring (Code Quality):

    • Refactored src/blosc2/schunk.py:open() to address high cyclomatic complexity (Ruff C901) triggered by changes introduced by this commit
    • Extracted logic into private helper functions:
      • _open_special_store(): Handles extension-based dispatch for special stores
      • _set_default_dparams(): Centralizes default decompression parameter logic
      • _process_opened_object(): Handles post-open logic for Proxy and LazyArray
  3. Context Manager Support:

    • Implemented __enter__(), __exit__(), and close() methods for DictStore and EmbedStore
    • Enables usage of the with statement for safer resource management (e.g., with blosc2.open("file.b2z") as store:)
  4. Bug Fixes:

    • Fixed an infinite recursion bug where DictStore and EmbedStore internally called blosc2.open(), creating a loop. These now use the lower-level blosc2_ext.open()
  5. Testing:

    • Added test_open_context_manager() to tests/test_dict_store.py and tests/test_embed_store.py to verify the new functionality
    • All existing tests continue to pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant