Skip to content

Commit

Permalink
Rename sync to copy.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielballan committed May 7, 2024
1 parent 096ee4c commit b268c37
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
22 changes: 11 additions & 11 deletions tiled/_tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from tiled.client import Context, from_context
from tiled.client.register import register
from tiled.client.smoke import read
from tiled.client.sync import sync
from tiled.client.sync import copy
from tiled.queries import Key
from tiled.server.app import build_app

Expand Down Expand Up @@ -61,48 +61,48 @@ def populate_internal(client):
container.write_dataframe(df, key="B", metadata={"color": "green"}, specs=["beta"])


def test_sync_internal():
def test_copy_internal():
with client_factory() as dest:
with client_factory() as source:
populate_internal(source)
sync(source, dest)
copy(source, dest)
assert list(source) == list(dest)
assert list(source["c"]) == list(dest["c"])
read(dest, strict=True)


def test_sync_external(tmp_path):
def test_copy_external(tmp_path):
with client_factory(readable_storage=[tmp_path]) as dest:
with client_factory() as source:
populate_external(source, tmp_path)
sync(source, dest)
copy(source, dest)
assert list(source) == list(dest)
assert list(source["subdir"]) == list(dest["subdir"])
read(dest, strict=True)


def test_sync_search_results():
def test_copy_search_results():
with client_factory() as dest:
with client_factory() as source:
populate_internal(source)
results = source.search(Key("color") == "red")
sync(results, dest)
copy(results, dest)
assert list(results) == list(dest)


def test_sync_items():
def test_copy_items():
with client_factory() as dest:
with client_factory() as source:
populate_internal(source)
select_items = source.items()[:2]
sync(select_items, dest)
copy(select_items, dest)
assert [key for key, _ in select_items] == list(dest)


def test_sync_dict():
def test_copy_dict():
with client_factory() as dest:
with client_factory() as source:
populate_internal(source)
select_dict = dict(source.items()[:2])
sync(select_dict, dest)
copy(select_dict, dest)
assert list(select_dict) == list(dest)
21 changes: 11 additions & 10 deletions tiled/client/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .base import BaseClient


def sync(
def copy(
source: BaseClient,
dest: BaseClient,
):
Expand All @@ -24,15 +24,16 @@ def sync(
Connect to two instances and copy data.
>>> from tiled.client import from_uri
>>> from tiled.client.sync import copy
>>> a = from_uri("http://localhost:8000", api_key="secret")
>>> b = from_uri("http://localhost:9000", api_key="secret")
>>> sync(a, b)
>>> copy(a, b)
Copy select data.
>>> sync(a.items().head(), b)
>>> sync(a.search(...), b)
>>> copy(a.items().head(), b)
>>> copy(a.search(...), b)
"""
if hasattr(source, "structure_family"):
Expand All @@ -45,21 +46,21 @@ def sync(
_DISPATCH[StructureFamily.container](source, dest)


def _sync_array(source, dest):
def _copy_array(source, dest):
num_blocks = (range(len(n)) for n in source.chunks)
# Loop over each block index --- e.g. (0, 0), (0, 1), (0, 2) ....
for block in itertools.product(*num_blocks):
array = source.read_block(block)
dest.write_block(array, block)


def _sync_table(source, dest):
def _copy_table(source, dest):
for partition in range(source.structure().npartitions):
df = source.read_partition(partition)
dest.write_partition(df, partition)


def _sync_container(source, dest):
def _copy_container(source, dest):
for key, child_node in source.items():
original_data_sources = child_node.include_data_sources().data_sources()
if not original_data_sources:
Expand Down Expand Up @@ -106,7 +107,7 @@ def _sync_container(source, dest):


_DISPATCH = {
StructureFamily.array: _sync_array,
StructureFamily.container: _sync_container,
StructureFamily.table: _sync_table,
StructureFamily.array: _copy_array,
StructureFamily.container: _copy_container,
StructureFamily.table: _copy_table,
}

0 comments on commit b268c37

Please sign in to comment.