Repository: SINTEF/dlite (language: python)

Issue:
Clean up the Python interface to collections
Remove unlogical optional arguments to convenience functions and revise the function documentation.


Discussion / hints:

Fix patch:
diff --git a/bindings/python/dlite-collection-python.i b/bindings/python/dlite-collection-python.i
index 85740a1cf..1f5d99b4e 100644
--- a/bindings/python/dlite-collection-python.i
+++ b/bindings/python/dlite-collection-python.i
@@ -166,7 +166,6 @@ class Collection(Instance):
     def get_id(self, id):
         """Return instance with given id."""
         inst = _collection_get_id(self._coll, id)
-        inst.incref()
         return inst
 
     def has(self, label):
@@ -195,12 +194,12 @@ class Collection(Instance):
         None is returned if there are no matching relations."""
         return _collection_find_first(self._coll, s, p, o)
 
-    def get_instances(self, s=None, p=None, o=None):
-        """Returns a generator over all relations matching the given
-        values of `s`, `p` and `o`."""
-        return _CollectionIter(self, s=s, p=p, o=o, rettype='I')
+    def get_instances(self):
+        """Returns a generator over all instances in collection."""
+        return _CollectionIter(self, s=None, p=None, o=None, rettype='I')
 
     def get_labels(self):
+        """Returns a generator over all labels."""
         return self.get_subjects(p='_is-a', o='Instance')
 
     def get_relations(self, s=None, p=None, o=None, rettype='T'):
@@ -208,20 +207,20 @@ class Collection(Instance):
         values of `s`, `p` and `o`."""
         return _CollectionIter(self, s=s, p=p, o=o, rettype=rettype)
 
-    def get_subjects(self, s=None, p=None, o=None):
+    def get_subjects(self, p=None, o=None):
         """Returns a generator over all subjects of relations matching the
-        given values of `s`, `p` and `o`."""
-        return _CollectionIter(self, s=s, p=p, o=o, rettype='s')
+        given values of `p` and `o`."""
+        return _CollectionIter(self, s=None, p=p, o=o, rettype='s')
 
-    def get_predicates(self, s=None, p=None, o=None):
-        """Returns a generator over all subjects of relations matching the
-        given values of `s`, `p` and `o`."""
-        return _CollectionIter(self, s=s, p=p, o=o, rettype='p')
+    def get_predicates(self, s=None, o=None):
+        """Returns a generator over all predicates of relations matching the
+        given values of `s` and `o`."""
+        return _CollectionIter(self, s=s, p=None, o=o, rettype='p')
 
-    def get_objects(self, s=None, p=None, o=None):
+    def get_objects(self, s=None, p=None):
         """Returns a generator over all subjects of relations matching the
-        given values of `s`, `p` and `o`."""
-        return _CollectionIter(self, s=s, p=p, o=o, rettype='o')
+        given values of `s` and `p`."""
+        return _CollectionIter(self, s=s, p=p, o=None, rettype='o')
 
 
 def get_collection(id):
diff --git a/bindings/python/dlite-storage.i b/bindings/python/dlite-storage.i
index 0e03f0564..c43ffc78c 100644
--- a/bindings/python/dlite-storage.i
+++ b/bindings/python/dlite-storage.i
@@ -135,7 +135,7 @@ matches `pattern`.  If `pattern` is None, all UUIDs will be returned.
   }
 
   %feature("docstring", "Set storage readability.") _set_readable;
-  void _set_writable(bool readable) {
+  void _set_readable(bool readable) {
     if (readable)
       $self->flags |= dliteReadable;
     else


Test patch:
diff --git a/bindings/python/tests/test_collection.py b/bindings/python/tests/test_collection.py
index 762452e1b..a75cc1661 100755
--- a/bindings/python/tests/test_collection.py
+++ b/bindings/python/tests/test_collection.py
@@ -89,6 +89,49 @@
     raise RuntimeError('should not be able to replace an existing instance')
 
 coll.add('inst1', inst2, force=True)  # forced replacement
+assert coll.get('inst1') == inst2
+coll.add('inst1', inst1, force=True)  # revert
+assert coll.get('inst1') == inst1
 
+
+# Test convinience functions
+i1 = coll.get_id(inst1.uuid)
+assert i1 == inst1
+
+assert coll.has('inst1') is True
+assert coll.has('inst2') is False
+assert coll.has('animal') is False
+
+rel = coll.get_first_relation()
+assert rel.s == 'dog'
+assert rel.p == 'is-a'
+assert rel.o == 'animal'
+rel = coll.get_first_relation(p='_has-meta')
+assert rel.s == 'inst1'
+assert rel.p == '_has-meta'
+assert rel.o == 'http://onto-ns.com/meta/0.1/MyEntity'
+
+i1, = coll.get_instances()
+assert i1 == inst1
+
+label1, = coll.get_labels()
+assert label1 == 'inst1'
+
+rels = list(coll.get_relations())
+assert len(rels) == 4
+rels = list(coll.get_relations(p='_is-a'))
+assert len(rels) == 1
+rels = list(coll.get_relations(p='_xxx'))
+assert len(rels) == 0
+
+assert list(coll.get_subjects()) == ['dog', 'inst1', 'inst1', 'inst1']
+assert list(coll.get_predicates()) == [
+    'is-a', '_is-a', '_has-uuid', '_has-meta']
+assert list(coll.get_objects()) == [
+    'animal', 'Instance', inst1.uuid, inst1.meta.uri]
+
+
+
+# String representation
 s = str(coll)
-print(coll)
+print(s)