Skip to content

Commit

Permalink
[python] Enable chaining on certain write methods. (#123)
Browse files Browse the repository at this point in the history
Returns `self` from a few methods to enable chaining:

- DataFrame/DenseNDArray/SparseNDArray.write
- Collection.set

This enables useful chaining:

    some_coll.set('x', ...).set('y', ...)

    my_df = DataFrame.create(...).write(...)
    do_stuff_with(my_df)
  • Loading branch information
thetorpedodog authored Feb 13, 2023
1 parent 8922de4 commit b010bb4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
3 changes: 2 additions & 1 deletion python-spec/src/somacore/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def __setitem__(self, key: str, value: _Elem) -> None:
@abc.abstractmethod
def set(
self, key: str, value: _Elem, *, use_relative_uri: Optional[bool] = None
) -> None:
) -> Self:
"""Sets an entry of this collection. [lifecycle: experimental]
Important note: Because parent objects may need to share
Expand All @@ -207,6 +207,7 @@ def set(
not share a relative URI base, or use of relative URIs is not
possible at all, the collection should throw an exception.
If ``False``, will always use an absolute URI.
:return: ``self``, to enable method chaining.
"""
raise NotImplementedError()

Expand Down
10 changes: 6 additions & 4 deletions python-spec/src/somacore/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def write(
values: Union[pa.RecordBatch, pa.Table],
*,
platform_config: Optional[options.PlatformConfig] = None,
) -> None:
) -> Self:
"""Writes the data from an Arrow table to the persistent object.
[lifecycle: experimental]
Expand All @@ -148,6 +148,7 @@ def write(
:param values: An Arrow table containing all columns, including
the index columns. The schema for the values must match
the schema for the ``DataFrame``.
:return: ``self``, to enable method chaining.
"""
raise NotImplementedError()

Expand Down Expand Up @@ -288,7 +289,7 @@ def write(
values: pa.Tensor,
*,
platform_config: Optional[options.PlatformConfig] = None,
) -> None:
) -> Self:
"""Writes an Arrow tensor to a subarray of the persistent object.
[lifecycle: experimental]
Expand All @@ -300,6 +301,7 @@ def write(
See :meth:`read` for details about indexing.
:param values: The values to be written to the subarray. Must have
the same shape as ``coords``, and matching type to the array.
:return: ``self``, to enable method chaining.
"""
raise NotImplementedError()

Expand Down Expand Up @@ -382,11 +384,12 @@ def write(
values: SparseArrowData,
*,
platform_config: Optional[options.PlatformConfig] = None,
) -> None:
) -> Self:
"""Writes a Tensor to a subarray of the persistent object.
[lifecycle: experimental]
:param values: The values to write to the array.
:return: ``self``, to enable method chaining.
**Value types:**
Expand All @@ -397,7 +400,6 @@ def write(
Arrow table: a COO table, with columns named ``soma_dim_0``, ...,
``soma_dim_N`` and ``soma_data``, to be written to the array.
"""
raise NotImplementedError()

Expand Down
3 changes: 2 additions & 1 deletion python-spec/src/somacore/ephemeral/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ def mode(self) -> options.OpenMode:

def set(
self, key: str, value: _Elem, *, use_relative_uri: Optional[bool] = None
) -> None:
) -> Self:
del use_relative_uri # Ignored.
self._entries[key] = value
return self

def __getitem__(self, key: str) -> _Elem:
return self._entries[key]
Expand Down

0 comments on commit b010bb4

Please sign in to comment.