Skip to content

Commit

Permalink
[python] Add replace method to SOMATileDBContext (#908)
Browse files Browse the repository at this point in the history
* add context evolve

* lint

* more lint

* PR review feedback
  • Loading branch information
Bruce Martin authored Feb 9, 2023
1 parent 5caafcf commit 7384dcf
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
27 changes: 27 additions & 0 deletions apis/python/src/tiledbsoma/options/soma_tiledb_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import attrs
import tiledb
from typing_extensions import Self


def _build_default_tiledb_ctx() -> tiledb.Ctx:
Expand Down Expand Up @@ -96,3 +97,29 @@ def __attrs_post_init__(self) -> None:
else:
object.__setattr__(self, "_group_write_tiledb_ctx", self.tiledb_ctx)
assert isinstance(self._group_write_tiledb_ctx, tiledb.Ctx)

def replace(
self, *, tiledb_config: Optional[Dict[str, Any]] = None, **changes: Any
) -> Self:
"""
Create a copy of the context, merging changes.
Parameters
----------
tiledb_config - Dict[str, Any]
a dictionary of parameters for tiledb.Config()
changes - Any
Any other parameters will be passed to the class __init__.
Examples
--------
>>> context.replace(read_timestamp=0)
>>> context.replace(tiledb_config={"vfs.s3.region": "us-east-2"})
"""
if tiledb_config:
new_config = self.tiledb_ctx.config()
new_config.update(tiledb_config)
changes["tiledb_ctx"] = tiledb.Ctx(config=new_config)
return attrs.evolve(self, **changes)
19 changes: 19 additions & 0 deletions apis/python/tests/test_platform_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,22 @@ def test__from_platform_config__admits_tiledb_create_options_object():
}
)
assert tdb_create_options.dim_tile("soma_dim_0") == 6


def test_SOMATileDBContext_evolve():
context = tiledbsoma.options.SOMATileDBContext()

# verify defaults expected by subsequent tests
assert context.read_timestamp_start == 0
assert context.tiledb_ctx.config()["vfs.s3.region"] == "us-east-1"

# verify read_timestamp_start
assert context.replace(read_timestamp_start=1).read_timestamp_start == 1

# veirfy write_timestamp
assert context.replace(write_timestamp=1).write_timestamp == 1

# verify tiledb_ctx
context.replace(tiledb_config={"vfs.s3.region": "us-west-2"}).tiledb_ctx.config()[
"vfs.s3.region"
] == "us-west-2"

0 comments on commit 7384dcf

Please sign in to comment.