Repository: TileDB-Inc/TileDB-Py

Issue:
tiledb.open doesn't accept an optional ctx
it accepts a config which creates a ctx, would it make sense to have both?


Discussion:

Resolving patch:
diff --git a/tiledb/highlevel.py b/tiledb/highlevel.py
--- a/tiledb/highlevel.py
+++ b/tiledb/highlevel.py
@@ -3,7 +3,7 @@
 
 import numpy as np
 
-def open(uri, key=None, attr=None, mode='r', config=None):
+def open(uri, key=None, attr=None, mode='r', config=None, ctx=None):
     """
     Open a TileDB array at the given URI
 
@@ -14,10 +14,14 @@ def open(uri, key=None, attr=None, mode='r', config=None):
     :param config: TileDB config dictionary, dict or None
     :return:
     """
+    if ctx and config:
+      raise ValueError("Received extra Ctx or Config argument: either one may be provided, but not both")
+
     if config:
         cfg = tiledb.Config(config)
         ctx = tiledb.Ctx(cfg)
-    else:
+
+    if ctx is None:
         ctx = default_ctx()
 
     schema = ArraySchema.load(uri, ctx=ctx)
@@ -88,3 +92,23 @@ def from_numpy(uri, array, ctx=default_ctx(), **kw):
 
     return DenseArray.from_numpy(uri, array, ctx=ctx, **kw)
 
+def array_exists(uri, isdense=False, issparse=False):
+    """
+    Check if arrays exists and is open-able at the given URI
+
+    Optionally restrict to `isdense` or `issparse` array types.
+    """
+    try:
+        a = tiledb.open(uri)
+    except TileDBError as exc:
+        return False
+
+    if isdense:
+        rval = not a.schema.sparse
+    elif issparse:
+        rval = a.schema.sparse
+    else:
+        rval = True
+
+    a.close()
+    return rval


Test patch:
diff --git a/tiledb/tests/test_libtiledb.py b/tiledb/tests/test_libtiledb.py
--- a/tiledb/tests/test_libtiledb.py
+++ b/tiledb/tests/test_libtiledb.py
@@ -1165,16 +1165,16 @@ class SparseArray(DiskTestCase):
     @unittest.expectedFailure
     def test_simple_1d_sparse_vector(self):
         ctx = tiledb.Ctx()
-        dom = tiledb.Domain(ctx, tiledb.Dim(ctx, domain=(0, 3), tile=4, dtype=int))
-        att = tiledb.Attr(ctx, dtype=int)
-        schema = tiledb.ArraySchema(ctx, domain=dom, attrs=(att,), sparse=True)
+        dom = tiledb.Domain(tiledb.Dim(domain=(0, 3), tile=4, dtype=int, ctx=ctx), ctx=ctx)
+        att = tiledb.Attr(dtype=int, ctx=ctx)
+        schema = tiledb.ArraySchema(domain=dom, attrs=(att,), sparse=True, ctx=ctx)
         tiledb.SparseArray.create(self.path("foo"), schema)
 
         values = np.array([3, 4])
-        with tiledb.SparseArray(ctx, self.path("foo"), mode='w') as T:
+        with tiledb.SparseArray(self.path("foo"), mode='w', ctx=ctx) as T:
             T[[1, 2]] = values
 
-        with tiledb.SparseArray(ctx, self.path("foo"), mode='r') as T:
+        with tiledb.SparseArray(self.path("foo"), mode='r', ctx=ctx) as T:
             assert_array_equal(T[[1, 2]], values)
 
     @unittest.expectedFailure
diff --git a/tiledb/tests/test_util.py b/tiledb/tests/test_util.py
--- a/tiledb/tests/test_util.py
+++ b/tiledb/tests/test_util.py
@@ -73,3 +73,28 @@ def test_save(self):
         with tiledb.save(uri, arr) as tmp:
             with tiledb.open(uri) as T:
                 assert_array_equal(arr, T)
+
+    def test_array_exists(self):
+        import tempfile
+        with tempfile.NamedTemporaryFile() as tmpfn:
+            self.assertFalse(tiledb.array_exists(tmpfn.name))
+
+        uri = self.path("test_array_exists_dense")
+        with tiledb.from_numpy(uri, np.arange(0,5)) as T:
+            self.assertTrue(tiledb.array_exists(uri))
+            self.assertTrue(tiledb.array_exists(uri, isdense=True))
+            self.assertFalse(tiledb.array_exists(uri, issparse=True))
+
+        uri = self.path("test_array_exists_sparse")
+        ctx = tiledb.Ctx()
+        dom = tiledb.Domain(tiledb.Dim(domain=(0, 3), tile=4, dtype=int, ctx=ctx), ctx=ctx)
+        att = tiledb.Attr(dtype=int, ctx=ctx)
+        schema = tiledb.ArraySchema(domain=dom, attrs=(att,), sparse=True, ctx=ctx)
+        tiledb.Array.create(uri, schema)
+
+        with tiledb.SparseArray(uri, mode='w') as T:
+            T[[0,1]] = np.array([0,1])
+
+        self.assertTrue(tiledb.array_exists(uri))
+        self.assertTrue(tiledb.array_exists(uri, issparse=True))
+        self.assertFalse(tiledb.array_exists(uri, isdense=True))